Skip to content

Instantly share code, notes, and snippets.

@johnrees
Last active August 29, 2015 14:05
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 johnrees/e9beede3740487b9616a to your computer and use it in GitHub Desktop.
Save johnrees/e9beede3740487b9616a to your computer and use it in GitHub Desktop.
problem 88
min = 2
max = 100
min.upto(max) do |count|
puts "k=#{count}"
a = (1..count).to_a
options = a.repeated_combination(count).to_a
for option in options.reverse
addition_total = 0
multiplication_total = 1
for number in option do
addition_total += number
multiplication_total *= number
end
if addition_total == multiplication_total
puts "#{addition_total} = #{option}"
break
end
end
end
➜ Desktop ruby 88.rb
k=2
4 = [2, 2]
k=3
6 = [1, 2, 3]
k=4
8 = [1, 1, 2, 4]
k=5
8 = [1, 1, 2, 2, 2]
k=6
12 = [1, 1, 1, 1, 2, 6]
k=7
12 = [1, 1, 1, 1, 1, 3, 4]
k=8
12 = [1, 1, 1, 1, 1, 2, 2, 3]
k=9
15 = [1, 1, 1, 1, 1, 1, 1, 3, 5]
k=10
16 = [1, 1, 1, 1, 1, 1, 1, 1, 4, 4]
k=11
16 = [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 4]
k=12
16 = [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2]
k=13
18 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 3]
k=14
20 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 5]
k=15
24 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 8]
k=16
24 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 6]
k=17
24 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 6]
k=18
24 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4]
k=19
24 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3]
k=20
28 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment