Sisyphus sequence: Difference between revisions

(Promoted to 'full' task.)
Line 949:
2: (990.,2273.)
1: (24975.,30713.)
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
prime_gen = Prime.each
cur_prime = nil
sisyphi = Enumerator.produce(1) {|n| n.even? ? n/2: n += (cur_prime = prime_gen.next)}
 
sisyphi.first(100).each_slice(10){|s| puts "%4d"*s.size % s }
 
puts
prime_gen.rewind
counter = Hash.new(0)
count_until = 250
idx = 1000
limit = 100_000_000
sisyphi.with_index(1) do |n, i|
counter[n] += 1 if n < count_until
if i == idx then
puts "element %11d is %11d, with prime %11d" % [i, n, cur_prime]
break if idx >= limit
idx *= 10
end
end
 
puts "\nThese numbers under #{count_until} do not occur in the first #{limit} terms:"
puts ((1..count_until).to_a - counter.keys).join ", "
 
freq, nums = counter.group_by{|k, v| v}.max
puts "\nThese numbers under #{count_until} occur most frequent (#{freq} times) in the first #{limit} terms:"
puts nums.map(&:first).sort.join(", ")</syntaxhighlight>
{{out}}
<pre> 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
 
element 1000 is 990, with prime 2273
element 10000 is 24975, with prime 30713
element 100000 is 265781, with prime 392111
element 1000000 is 8820834, with prime 4761697
element 10000000 is 41369713, with prime 55900829
element 100000000 is 1179614168, with prime 640692323
 
These numbers under 250 do not occur in the first 100000000 terms:
36, 72, 97, 107, 115, 127, 144, 167, 194, 211, 214, 230, 232, 250
 
These numbers under 250 occur most frequent (7 times) in the first 100000000 terms:
7, 14, 28
</pre>
 
1,149

edits