Colorful numbers: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: use 'hyper' not 'race') |
(→{{header|Ruby}}: Add Ruby) |
||
Line 1,178: | Line 1,178: | ||
Total colorful numbers: 57256</pre> |
Total colorful numbers: 57256</pre> |
||
=={{header|Ruby}}== |
|||
All colorful candidates larger than 1 digit must be a permutation of digits [2,3,4,5,6,7,8,9], so test only those: |
|||
<syntaxhighlight lang="ruby">def colorful?(ar) |
|||
prods = [] |
|||
(1..ar.size).all? do |chunk_size| |
|||
ar.each_cons(chunk_size) do |chunk| |
|||
product = chunk.inject(&:*) |
|||
return false if prods.include?(product) |
|||
prods << product |
|||
end |
|||
end |
|||
end |
|||
less100 = (0..100).select{|n| colorful?(n.digits)} |
|||
puts "The colorful numbers less than 100 are:\n #{less100.join(" ")}" |
|||
puts "\nLargest colorful number: #{(98765432.downto(1).detect{|n| colorful?(n.digits) })}" |
|||
puts |
|||
total = 0 |
|||
(1..8).each do |numdigs| |
|||
digits = (numdigs == 1 ? (0..9).to_a : (2..9).to_a) |
|||
count = digits.permutation(numdigs).count{|perm| colorful?(perm)} |
|||
puts "#{numdigs} digit colorful numbers count: #{count}" |
|||
total += count |
|||
end |
|||
puts "\nTotal colorful numbers: #{total}" |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The colorful numbers less than 100 are: |
|||
0 1 2 3 4 5 6 7 8 9 23 24 25 26 27 28 29 32 34 35 36 37 38 39 42 43 45 46 47 48 49 52 53 54 56 57 58 59 62 63 64 65 67 68 69 72 73 74 75 76 78 79 82 83 84 85 86 87 89 92 93 94 95 96 97 98 |
|||
Largest colorful number: 98746253 |
|||
1 digit colorful numbers count: 10 |
|||
2 digit colorful numbers count: 56 |
|||
3 digit colorful numbers count: 328 |
|||
4 digit colorful numbers count: 1540 |
|||
5 digit colorful numbers count: 5514 |
|||
6 digit colorful numbers count: 13956 |
|||
7 digit colorful numbers count: 21596 |
|||
8 digit colorful numbers count: 14256 |
|||
Total colorful numbers: 57256 |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |