Is it a Cyclic number?

20

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!

FantaC

Posted 2017-05-28T23:57:33.987

Reputation: 1 425

1

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.

– FryAmTheEggman – 2017-05-29T00:06:42.067

Answers

3

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!

Adnan

Posted 2017-05-28T23:57:33.987

Reputation: 41 965

1What is the reason for ¦‚˜? – kalsowerus – 2017-05-29T09:34:18.673

1@kalsowerus If the input has a leading zero, multiplying by 1 would make it disappear, which makes it not work for 0588235294117647. – Adnan – 2017-05-29T09:42:01.207

@Adnan why didn't 0169491525423728813559322033898305084745762711864406779661 work? – FantaC – 2017-05-29T13:15:34.607

2

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

– Adnan – 2017-05-29T13:20:26.780

@Adnan Yup, that's the problem! Definitely take the leading zeroes into account. Good Luck! – FantaC – 2017-05-29T13:24:01.040

@tfbninja Thanks for the notification, fixed. – Adnan – 2017-05-29T13:25:40.847

@Adnan, Nice Job! Works for everything I tried. – FantaC – 2017-05-29T13:35:03.673

@Emigna Actually, I can leave the entire flatten out, since the leading zeros will still be removed by the int haha. – Adnan – 2017-05-29T14:03:52.133

Actually, is it really OK to remove all zeroes? I don't have an example but couldn't one number end up with the same digits as the other numbers apart from an extra zero? – Emigna – 2017-05-29T14:08:22.813

1Consider 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. – Emigna – 2017-05-29T14:16:22.680

@Emigna Hm, that's weird, because you can always add extra zeros in front of the number to equalize the number of zeros. At least, that's how I understood the question. – Adnan – 2017-05-29T14:24:13.043

2@tfbninja Is the output for Emigna's test case truthy or falsy? – Adnan – 2017-05-29T14:25:04.070

1@Emigna, and Adnan I checked, and it is a cyclic number, at least, according to Wikipedia, which I know isn't the best source but it's one of the only options. So, truthy – FantaC – 2017-05-29T14:34:12.907

@Adnan: Ah yes, I didn't consider that :) – Emigna – 2017-05-29T17:10:08.937

4

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

Mego

Posted 2017-05-28T23:57:33.987

Reputation: 32 998

1@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. – Mego – 2017-05-29T00:44:06.850

1What 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. – Pseudonym – 2017-05-29T02:30:35.317

3

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.

Uriel

Posted 2017-05-28T23:57:33.987

Reputation: 11 708

1@tfbninja should work on any python (2 and 3) – Uriel – 2017-05-29T00:39:03.713

1why does it fail with 0212765957446808510638297872340425531914893617 ? – J42161217 – 2017-05-29T01:50:29.653

@Jenny_mathy now it doesn't. – Uriel – 2017-05-29T14:58:24.140

2

PHP, 64 Bytes

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

Online Version

Jörg Hülsermann

Posted 2017-05-28T23:57:33.987

Reputation: 13 026

2

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

Pseudonym

Posted 2017-05-28T23:57:33.987

Reputation: 651

1does it work for 052631578947368421 ? – J42161217 – 2017-05-29T01:55:53.597

Yes, it returns True in that case. – Pseudonym – 2017-05-29T02:23:43.257

2Save some bytes by replacing ns with n – Generic Display Name – 2017-05-29T02:26:27.443

1

Can you use <1 instead of ==0? Also here is a TIO link: Try it online!

– Laikoni – 2017-05-29T08:35:21.393

How about 111111? – Antony Hatchkins – 2017-05-29T15:11:54.800

Ah, good point. It needs to be the minimum period, not the length of the input number. – Pseudonym – 2017-05-30T03:16:41.490

2

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

Pseudonym

Posted 2017-05-28T23:57:33.987

Reputation: 651

1

Mathematica, 81 bytes

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

Try it online!

input string

Input

"010309278350515463917525773195876288659793814432989690721649484536082474226804123711340206185567"

Output

True

J42161217

Posted 2017-05-28T23:57:33.987

Reputation: 15 931

FromDigits is shorter than ToExpression – JungHwan Min – 2017-05-29T02:37:50.617

1because in this challenge you need to work with inputs like 034324... – J42161217 – 2017-05-29T02:39:38.790