# Generate cyclic numbers in decimal

The formula on the wiki article for cyclic numbers can be used to generate cyclic numbers in any base. Using this formula, or any other appropriate formula, compute the first ten cyclic numbers in base 10. (The numbers cannot simply be stored and read out, even if the encoding is obscure or novel.)

Repeated cyclic numbers, for example 142857142857, should be avoided if possible.

Output can include 0. if desired, but it is not required. Each number to be delimited by line, space, comma, etc.

The program which is the shortest and which follows the rules as closely as possible will be declared the winner.

## GolfScript (30 29 chars)

As xxd output:

0000000: 2207 1113 171d 2f3b 3d61 6d22 7b2e 2831  "...../;=am"{.(1
0000010: 305c 3f2e 402f 2b60 283b 6e7d 25         0\?.@/+(;n}%


This illustrates the importance of making codegolf problems take input so that the results can't be effectively hard-coded.

• It is noted in the question that the answer should be computed, though congratulations on finding a loop hole I forgot to mention -- I suppose this is one of the points of code golf... – Thomas O Jul 17 '13 at 22:24
• @ThomasO, it is computed using the formula you specify - it's just that I've hard-coded the values of the primes rather than looping over all candidates and filtering to the ones which work. – Peter Taylor Jul 17 '13 at 22:28

## GolfScript (45 44 43 chars but less hard-coded)

110,{:^(.[-1{.10*^%}^*]?={10^(?.^/+(;n}*}/


This implementation just uses a bound on the possible primes and performs a naïve (but golfed) primitive root check to filter.

Online demo

• Nice idea for padding zeros with (;. I'd take this also for my solution ;-) – Howard Jul 18 '13 at 4:15

### GolfScript, 6053 50 characters

107,{))9)1$?.(@)/+(;.,,{)1$~*1\$.+\?0<}%0-!\n+*}%


A plain loop, checking strings for cyclical identity.

Output:

142857
0588235294117647
052631578947368421
0434782608695652173913
0344827586206896551724137931
0212765957446808510638297872340425531914893617
0169491525423728813559322033898305084745762711864406779661
016393442622950819672131147540983606557377049180327868852459
010309278350515463917525773195876288659793814432989690721649484536082474226804123711340206185567
009174311926605504587155963302752293577981651376146788990825688073394495412844036697247706422018348623853211


## Python 77 bytes

p=0;exec"p+=2\nif len(set(10**i%~p for i in range(p)))/p:print-10**p/~p\n"*54


Output:

142857
588235294117647
52631578947368421
434782608695652173913
344827586206896551724137931
212765957446808510638297872340425531914893617
169491525423728813559322033898305084745762711864406779661
16393442622950819672131147540983606557377049180327868852459
10309278350515463917525773195876288659793814432989690721649484536082474226804123711340206185567
9174311926605504587155963302752293577981651376146788990825688073394495412844036697247706422018348623853211


# Mathematica 51 67

Cases[Range@110,p_/;10~MultiplicativeOrder~p==p-1:>Round[10^(p-1)/p]]


{142857, 588235294117647, 52631578947368421, 434782608695652173913, 344827586206896551724137931, 212765957446808510638297872340425531914893617, 169491525423728813559322033898305084745762711864406779661, 16393442622950819672131147540983606557377049180327868852459, 10309278350515463917525773195876288659793814432989690721649484536082474226804123711340206185567, 9174311926605504587155963302752293577981651376146788990825688073394495412844036697247706422018348623853211}

• Correct. It searches through all numbers in the range, but only primes satisfy the desired constraint. It would be more efficient to search only among primes, but that would cost some extra characters. – DavidC Jul 18 '13 at 10:27
• Hmmm. Ok. I'll correct the code in a bit. – DavidC Jul 18 '13 at 12:26