Colorful numbers: Difference between revisions
Content added Content deleted
m (→{{header|Ruby}}: mostly indentation) |
|||
Line 1,182: | Line 1,182: | ||
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: |
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) |
<syntaxhighlight lang="ruby">def colorful?(ar) |
||
products = [] |
|||
(1..ar.size).all? do |chunk_size| |
|||
ar.each_cons(chunk_size) do |chunk| |
|||
product = chunk.inject(&:*) |
|||
return false if products.include?(product) |
|||
products << product |
|||
⚫ | |||
end |
end |
||
⚫ | |||
end |
end |
||
below100 = (0..100).select{|n| colorful?(n.digits)} |
|||
puts "The colorful numbers less than 100 are:\n #{ |
puts "The colorful numbers less than 100 are:\n #{below100.join(" ")}", "" |
||
puts " |
puts "Largest colorful number: #{(98765432.downto(1).detect{|n| colorful?(n.digits) })}", "" |
||
total = 0 |
total = 0 |