Pan base non-primes: Difference between revisions
Content added Content deleted
(Added Java solution) |
(→{{header|Ruby}}: Add Ruby) |
||
Line 939: | Line 939: | ||
Percent even up to and including 2500: 83.105981</pre> |
Percent even up to and including 2500: 83.105981</pre> |
||
=={{header|Ruby}}== |
|||
<syntaxhighlight lang="ruby">require 'prime' |
|||
def int_from_digits(ar, base=10) |
|||
# expects array from digits method, which gives least significant digit first. |
|||
raise ArgumentError, "#{ar.max} not valid in base #{base}. " if ar.max > base-1 |
|||
ar.each_with_index.sum {|d, i| d*base**i } |
|||
end |
|||
limit = 2500 |
|||
a121719 = (2..limit).lazy.select do |n| |
|||
next false if (n < 10 && n.prime?) |
|||
digits = n.digits |
|||
from = digits.max + 1 |
|||
(from..n).none?{|base| int_from_digits(digits, base).prime? } |
|||
end |
|||
n = 50 |
|||
puts "First #{n} pan-base composites:" |
|||
a121719.take(n).each_slice(10){|s| puts "%4s"*s.size % s} |
|||
n = 20 |
|||
puts "\nFirst #{n} odd pan-base composites:" |
|||
a121719.select(&:odd?).take(n).each_slice(10){|s| puts "%4s"*s.size % s } |
|||
tally = a121719.map(&:odd?).tally |
|||
total = tally.values.sum |
|||
puts "\nCount of pan-base composites up to and including #{limit}: #{total}" |
|||
puts "Number of odds is #{tally[true ]}, proportion #{tally[true ].fdiv(total) }%" |
|||
puts "Number of evens is #{tally[false]}, proportion #{tally[false].fdiv(total) }%"</syntaxhighlight> |
|||
{{out}} |
|||
<pre>First 50 pan-base composites: |
|||
4 6 8 9 20 22 24 26 28 30 |
|||
33 36 39 40 42 44 46 48 50 55 |
|||
60 62 63 64 66 68 69 70 77 80 |
|||
82 84 86 88 90 93 96 99 100 110 |
|||
112 114 116 118 120 121 130 132 134 136 |
|||
First 20 odd pan-base composites: |
|||
9 33 39 55 63 69 77 93 99 121 |
|||
143 165 169 187 231 253 273 275 297 299 |
|||
Count of pan-base composites up to and including 2500: 953 |
|||
Number of odds is 161, proportion 0.1689401888772298% |
|||
Number of evens is 792, proportion 0.8310598111227702% |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |