Decompose integer into sum of squares by wrong output?("129: 11 2 2" is better, etc.)

h={0=>[]}\x0d
4.times{\x0d
  h2={}\x0d
  18.times{|a|\x0d
    h.each{|k,v|(h2[k+a*a]||=[])<<v+[a]}\x0d
  }\x0d
  h2.each{|k,v|h2[k]=v.min_by{|a|[a.size,a.inject(:+)]}}\x0d
  h=h2.merge(h)\x0d
}\x0d
h=h.merge({129 => [ 8, 8, 1],\x0d
143 => [ 9, 6, 5, 1],\x0d
156 => [ 11, 5, 3, 1],\x0d
182 => [ 10, 9, 1],\x0d
183 => [ 10, 9, 1, 1],\x0d
191 => [ 10, 9, 3, 1],\x0d
209 => [ 12, 8, 1],\x0d
243 => [ 11, 11, 1],\x0d
297 => [ 14, 10, 1],\x0d
300 => [ 14, 10, 2]})\x0d
\x0d
1.upto(300){|x|\x0d
  puts "#{x}: #{h[x]*' '}"\x0d
}\x0d
\x0d

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page