# Is it a Cyclic number?

A cyclic number is a number of "n" digits that when multiplied by 1, 2, 3,...n, results in the same digits but in a different order.

For example, the number 142,857 is a cyclic number since 142,857 x 2 = 285,714, 142,857 x 3 = 428,571, 142,857 x 4 = 571,428, and so on. Given an integer input, determine if it is a cyclic number by outputting a truthy value if it is, and a falsy value if not.

Also, to be clear, the input can contain leading 0's: e.g. 0344827586206896551724137931

This is due to the fact that if leading zeros are not permitted on numerals, then 142857 is the only cyclic number in decimal.

Since it is code-golf, shortest answer in bytes wins!

• Hi and welcome to PPCG. This is not a bad question, but if you take a look at some of the recently posted questions I think that you will see that it could be better. Specifically, it would be very beneficial for the community if you provided more test cases to work with. When posting future challenges, please consider using the sandbox. Commented May 29, 2017 at 0:06

# Actually, 18 bytes

;;ru@≈*♂$♂S♂≈╔@S≈=  Try it online! (expects quoted input) Explanation: ;;ru@≈*♂$♂S♂≈╔@S≈=
;;                  duplicate input twice
ru                range(1, len(input)+1)
@≈              convert input to an integer
*             multiply input by each element in range
♂$♂S♂≈ convert each product to a string, sort the digits, and convert back to int ╔ uniquify: remove duplicate elements @S≈ sort input and convert to int = compare equality  • @tfbninja I posted this before the bit about leading zeroes. I have another 15-byte solution that will work with leading zeroes that I will edit in soon. – user45941 Commented May 29, 2017 at 0:44 • What character encoding do you use to achieve 18 bytes? I tried UTF-8 and it weighed in at 32 bytes. EDIT: Oh, I see, it's code page 437. Commented May 29, 2017 at 2:30 # 05AB1E, 9 6 bytes Thanks to Emigna for saving 3 bytes! ā*€{ïË  Explanation: ā # Push range(1, len(input) + 1) * # Multiply by the input €{ # Sort each element ï # Convert to int to remove leading zeros Ë # Check if all elements are equal  Uses the 05AB1E encoding. Try it online! • What is the reason for ¦‚˜? Commented May 29, 2017 at 9:34 • @kalsowerus If the input has a leading zero, multiplying by 1 would make it disappear, which makes it not work for 0588235294117647. Commented May 29, 2017 at 9:42 • @tfbninja Oh okay, is adding leading zeroes after the multiplication also something to take into account? These are the individual sorted results I get after multiplying, with some missing leading zeroes, which would probably indicate the problem here. Commented May 29, 2017 at 13:20 • Consider the number 0212765957446808510638297872340425531914893617 as mentioned in the comments of another answer. Looking at the sorted numbers I would assume it to return false, but when removing zeroes it becomes true. Commented May 29, 2017 at 14:16 • @tfbninja Is the output for Emigna's test case truthy or falsy? Commented May 29, 2017 at 14:25 # Python, 86 bytes lambda n:all(sorted(n)==sorted(str(int(n)*i).zfill(len(n)))for i in range(2,len(n)+1))  Try it online! Input numbers as strings. • @tfbninja should work on any python (2 and 3) Commented May 29, 2017 at 0:39 • why does it fail with 0212765957446808510638297872340425531914893617 ? Commented May 29, 2017 at 1:50 • @Jenny_mathy now it doesn't. Commented May 29, 2017 at 14:58 # PHP, 64 Bytes for(;$i++<9;)$r+=($c=count_chars)($argn)==$c($argn*$i);echo$r>1;  Online Version # Haskell, 363332 45 bytes c n=let l=length n in(10^l-1)divread n==l+1  Example usage: *Main> c "142857" True  I don't think this algorithm needs any explanation. TOL Thanks for suggestions: Generic Display Name, Laikoni. Thanks for correction: Antony Hatchkins. EDIT Nope, fails on "33". • does it work for 052631578947368421 ? Commented May 29, 2017 at 1:55 • Yes, it returns True in that case. Commented May 29, 2017 at 2:23 • Save some bytes by replacing ns with n Commented May 29, 2017 at 2:26 • Can you use <1 instead of ==0? Also here is a TIO link: Try it online! Commented May 29, 2017 at 8:35 • How about 111111? Commented May 29, 2017 at 15:11 # dc, 24 25 bytes [1]sa0?dZd10r^1-r1+/rx=ap  Prints "0" if the number is not cyclic, otherwise "1". Requires the number to be entered as a string. Example usage: $ echo "[052631578947368421]" | dc -e '[1]sa0?dZd10r^1-r1+/rx=ap'
1
\$ echo "[052631578947368422]" | dc -e '[1]sa0?dZd10r^1-r1+/rx=ap'
0


TOL

Explanation: Same algorithm as my Haskell submission.

EDIT Nope, fails on "33".

## Mathematica, 81 bytes

Length@Union@PadLeft[Sort/@IntegerDigits[ToExpression@#*Range@StringLength@#]]<2&


Try it online!

input string

Input

"010309278350515463917525773195876288659793814432989690721649484536082474226804123711340206185567"

Output

True

• FromDigits is shorter than ToExpression Commented May 29, 2017 at 2:37
• because in this challenge you need to work with inputs like 034324... Commented May 29, 2017 at 2:39

# Scala 3, 85 bytes

n=>(2 to n.size).forall(i=>n.sorted==(BigInt(n)*i).toString.padTo(n.size,'0').sorted)


Attempt This Online!