Skip to content

Instantly share code, notes, and snippets.

@iamkevinlowe
Last active December 30, 2015 17:38
Show Gist options
  • Select an option

  • Save iamkevinlowe/23688f2223def21c7ec6 to your computer and use it in GitHub Desktop.

Select an option

Save iamkevinlowe/23688f2223def21c7ec6 to your computer and use it in GitHub Desktop.
def decompose(n, area = n**2, squares = "", results = [])
if area == 0
results << squares.strip.split(' ').map(&:to_i)
elsif
x = Math.sqrt(area).floor
x -= 1 while x >= n
x.downto(1) do |i|
next if squares.strip.split(' ').map(&:to_i).include? i
decompose(i, area - i**2, "#{i} #{squares}", results)
end
end
results
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment