Skip to content

Instantly share code, notes, and snippets.

Created March 29, 2010 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/348416 to your computer and use it in GitHub Desktop.
Save anonymous/348416 to your computer and use it in GitHub Desktop.
answers = []
sums = []
72.downto(1).each do |h1|
72.downto(1).each do |h2|
72.downto(1).each do |h3|
if h1 * h2 * h3 == 72
a = [ h1, h2, h3 ].sort.reverse
if not answers.include?(a)
answers << a
sums << a[0] + a[1] + a[2]
end
end
end
end
end
sums.each_index { |i|
s = sums[i]
cs = sums.count(s)
if cs > 1
a = answers[i]
if a.count(a[0]) == 1
puts "Atilio's daughters ages are #{a[0]}, #{a[1]} and #{a[2]}"
puts "Because from all the possibilities which multiply 72: #{answers.to_s}"
puts "And having a look to the sums (e.g.: #{a[0]} + #{a[1]} + #{a[2]} = #{s}) array: #{sums.to_s}"
puts "Only #{s} is repeated #{cs} times, and in this case the elder daughter doesn't share its age with another"
end
end
}
# --- RESULT: ---
# Atilio's daughters ages are 8, 3 and 3
# Because from all the possibilities which multiply 72: [[72, 1, 1], [36, 2, 1], [24, 3, 1], [18, 4, 1], [18, 2, 2], [12, 6, 1], [12, 3, 2], [9, 8, 1], [9, 4, 2], [8, 3, 3], [6, 6, 2], [6, 4, 3]]
# And having a look to the sums (e.g.: 8 + 3 + 3 = 14) array: [74, 39, 28, 23, 22, 19, 17, 18, 15, 14, 14, 13]
# Only 14 is repeated 2 times, and in this case the elder daughter doesn't share its age with another
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment