3
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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... \$\endgroup\$ – Thomas O Jul 17 '13 at 22:24
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Peter Taylor Jul 17 '13 at 22:28
4
\$\begingroup\$

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

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

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
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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}

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – DavidC Jul 18 '13 at 10:27
  • \$\begingroup\$ Hmmm. Ok. I'll correct the code in a bit. \$\endgroup\$ – DavidC Jul 18 '13 at 12:26

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.