Generate cyclic numbers in decimal

3

1

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.

Thomas O

Posted 2013-07-17T20:15:55.187

Reputation: 3 044

Answers

6

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.

Peter Taylor

Posted 2013-07-17T20:15:55.187

Reputation: 41 901

1It 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 – 2013-07-17T22:24:06.877

1@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 – 2013-07-17T22:28:40.537

4

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

Peter Taylor

Posted 2013-07-17T20:15:55.187

Reputation: 41 901

Nice idea for padding zeros with (;. I'd take this also for my solution ;-) – Howard – 2013-07-18T04:15:29.637

2

GolfScript, 60 53 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

Howard

Posted 2013-07-17T20:15:55.187

Reputation: 23 109

2

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

primo

Posted 2013-07-17T20:15:55.187

Reputation: 30 891

1

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}

DavidC

Posted 2013-07-17T20:15:55.187

Reputation: 24 524

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 – 2013-07-18T10:27:03.210

Hmmm. Ok. I'll correct the code in a bit. – DavidC – 2013-07-18T12:26:54.480