20
\$\begingroup\$

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 because, 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!

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – FryAmTheEggman May 29 '17 at 0:06
3
\$\begingroup\$

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ What is the reason for ¦‚˜? \$\endgroup\$ – kalsowerus May 29 '17 at 9:34
  • 1
    \$\begingroup\$ @kalsowerus If the input has a leading zero, multiplying by 1 would make it disappear, which makes it not work for 0588235294117647. \$\endgroup\$ – Adnan May 29 '17 at 9:42
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Adnan May 29 '17 at 13:20
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Emigna May 29 '17 at 14:16
  • 2
    \$\begingroup\$ @tfbninja Is the output for Emigna's test case truthy or falsy? \$\endgroup\$ – Adnan May 29 '17 at 14:25
4
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Mego May 29 '17 at 0:44
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Pseudonym May 29 '17 at 2:30
3
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ @tfbninja should work on any python (2 and 3) \$\endgroup\$ – Uriel May 29 '17 at 0:39
  • 1
    \$\begingroup\$ why does it fail with 0212765957446808510638297872340425531914893617 ? \$\endgroup\$ – J42161217 May 29 '17 at 1:50
  • \$\begingroup\$ @Jenny_mathy now it doesn't. \$\endgroup\$ – Uriel May 29 '17 at 14:58
2
\$\begingroup\$

PHP, 64 Bytes

for(;$i++<9;)$r+=($c=count_chars)($argn)==$c($argn*$i);echo$r>1;

Online Version

\$\endgroup\$
2
\$\begingroup\$

Haskell, 36 33 32 45 bytes

c n=let l=length n in(10^l-1)`div`read 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".

\$\endgroup\$
  • 1
    \$\begingroup\$ does it work for 052631578947368421 ? \$\endgroup\$ – J42161217 May 29 '17 at 1:55
  • \$\begingroup\$ Yes, it returns True in that case. \$\endgroup\$ – Pseudonym May 29 '17 at 2:23
  • 2
    \$\begingroup\$ Save some bytes by replacing ns with n \$\endgroup\$ – Generic Display Name May 29 '17 at 2:26
  • 1
    \$\begingroup\$ Can you use <1 instead of ==0? Also here is a TIO link: Try it online! \$\endgroup\$ – Laikoni May 29 '17 at 8:35
  • \$\begingroup\$ How about 111111? \$\endgroup\$ – Antony Hatchkins May 29 '17 at 15:11
2
\$\begingroup\$

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".

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 81 bytes

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

Try it online!

input string

Input

"010309278350515463917525773195876288659793814432989690721649484536082474226804123711340206185567"

Output

True

\$\endgroup\$
  • \$\begingroup\$ FromDigits is shorter than ToExpression \$\endgroup\$ – JungHwan Min May 29 '17 at 2:37
  • 1
    \$\begingroup\$ because in this challenge you need to work with inputs like 034324... \$\endgroup\$ – J42161217 May 29 '17 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.