Humble numbers: Difference between revisions

Line 2,644:
1767 have 9 digits
2381 have 10 digits</pre>
 
===Orders of magnitude faster===
{{trans|Go}}
<lang ruby>def humble(digits)
h = Array.new(digits, 1)
x2, x3, x5, x7 = 2, 3, 5, 7
i, j, k, l = 0, 0, 0, 0
(1..).each do |n|
x = [x2, [x3, [x5, x7].min].min].min
break if x.to_s.size > digits
h[n] = x
x2 = 2 * h[i += 1] if x2 == h[n]
x3 = 3 * h[j += 1] if x3 == h[n]
x5 = 5 * h[k += 1] if x5 == h[n]
x7 = 7 * h[l += 1] if x7 == h[n]
end
h
end
 
digits = 30 # max digits for humble numbers
h = humble(digits) # humble numbers <= digits size
count = h.size # the total humble numbers count
counts = h.map { |n| n.to_s.size }.tally # hash of digits counts 1..digits
print "First 50 Humble Numbers: \n"; (0...50).each { |i| print "#{h[i]} " }
print "\n\nOf the first #{count} humble numbers:\n"
(1..digits).each { |num| printf("%5d have %2d digits\n", counts[num], num) }</lang>
{{out}}
<pre>First 50 Humble Numbers:
1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
 
Of the first 462691 humble numbers:
9 have 1 digits
36 have 2 digits
95 have 3 digits
197 have 4 digits
356 have 5 digits
579 have 6 digits
882 have 7 digits
1272 have 8 digits
1767 have 9 digits
2381 have 10 digits
3113 have 11 digits
3984 have 12 digits
5002 have 13 digits
6187 have 14 digits
7545 have 15 digits
9081 have 16 digits
10815 have 17 digits
12759 have 18 digits
14927 have 19 digits
17323 have 20 digits
19960 have 21 digits
22853 have 22 digits
26015 have 23 digits
29458 have 24 digits
33188 have 25 digits
37222 have 26 digits
41568 have 27 digits
46245 have 28 digits
51254 have 29 digits
56618 have 30 digits</pre>
 
=={{header|Sidef}}==
Anonymous user