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)
prods = []
products = []
(1..ar.size).all? do |chunk_size|
(1..ar.size).all? do |chunk_size|
ar.each_cons(chunk_size) do |chunk|
ar.each_cons(chunk_size) do |chunk|
product = chunk.inject(&:*)
product = chunk.inject(&:*)
return false if prods.include?(product)
return false if products.include?(product)
prods << product
products << product
end
end
end
end
end
end


less100 = (0..100).select{|n| colorful?(n.digits)}
below100 = (0..100).select{|n| colorful?(n.digits)}
puts "The colorful numbers less than 100 are:\n #{less100.join(" ")}"
puts "The colorful numbers less than 100 are:\n #{below100.join(" ")}", ""
puts "\nLargest colorful number: #{(98765432.downto(1).detect{|n| colorful?(n.digits) })}", ""
puts "Largest colorful number: #{(98765432.downto(1).detect{|n| colorful?(n.digits) })}", ""


total = 0
total = 0