Sisyphus sequence: Difference between revisions

Content added Content deleted
(Created Nim solution.)
m (julia example)
Line 309: Line 309:
These numbers under 250 occur the most in the first 10000000 terms:
These numbers under 250 occur the most in the first 10000000 terms:
[3,57,65,85,114,125,130,170,228] all occur 6 times.
[3,57,65,85,114,125,130,170,228] all occur 6 times.
</pre>

=={{header|Julia}}==
<syntaxhighlight lang="julia">using Counters, Formatting, Primes

struct Sisyphus end

function Base.iterate(s::Sisyphus, state = (0, 0))
n, p = state
if n == 0
return (1, 0), (1, 0)
else
if isodd(n)
p = nextprime(p + 1)
n += p
else
n ÷= 2
end
return (n, p), (n, p)
end
end

function test_sisyphus()
coun = Counter{Int}()
println("The first 100 members of the Sisyphus sequence are:")
for (i, (n, p)) in enumerate(Sisyphus())
if n < 250
coun[n] += 1
end
if i < 101
print(rpad(n, 4), i % 10 == 0 ? "\n" : "")
elseif i in [1000, 10000, 100_000, 1_000_000, 10_000_000, 100_000_000]
print(
rpad("\n$(format(i, commas = true))th number:", 22),
rpad(format(n, commas = true), 14),
"Highest prime needed: ",
format(p, commas = true),
)
end
if i == 100_000_000
println(
"\n\nThese numbers under 250 do not occur in the first 100,000,000 terms:",
)
println(" ", filter(j -> !haskey(coun, j), 1:249), "\n")
sorteds = sort!([(p, coun[p]) for p in coun], by = last)
maxtimes = sorteds[end][2]
println(
"These numbers under 250 occur the most ($maxtimes times) in the first 100,000,000 terms:",
)
println(" ", map(first, filter(p -> p[2] == maxtimes, sorteds)))
elseif n == 36
println("\nLocating the first entry in the sequence with value 36:")
println(
" The sequence position: ",
format(i, commas = true),
" has value $n using prime ",
format(p, commas = true),
)
break
end
end
end

test_sisyphus()
</syntaxhighlight>{{out}}
<pre>
The first 100 members of the Sisyphus sequence are:
1 3 6 3 8 4 2 1 8 4
2 1 12 6 3 16 8 4 2 1
18 9 28 14 7 30 15 44 22 11
42 21 58 29 70 35 78 39 86 43
96 48 24 12 6 3 62 31 92 46
23 90 45 116 58 29 102 51 130 65
148 74 37 126 63 160 80 40 20 10
5 106 53 156 78 39 146 73 182 91
204 102 51 178 89 220 110 55 192 96
48 24 12 6 3 142 71 220 110 55

1,000th number: 990 Highest prime needed: 2,273
10,000th number: 24,975 Highest prime needed: 30,713
100,000th number: 265,781 Highest prime needed: 392,111
1,000,000th number: 8,820,834 Highest prime needed: 4,761,697
10,000,000th number: 41,369,713 Highest prime needed: 55,900,829
100,000,000th number: 1,179,614,168 Highest prime needed: 640,692,323

These numbers under 250 do not occur in the first 100,000,000 terms:
[36, 72, 97, 107, 115, 127, 144, 167, 194, 211, 214, 230, 232]

These numbers under 250 occur the most (7 times) in the first 100,000,000 terms:
[28, 7, 14]

Locating the first entry in the sequence with value 36:
The sequence position: 77,534,485,877 has value 36 using prime 677,121,348,413
</pre>
</pre>