Skip to content

Instantly share code, notes, and snippets.

@joegiralt
Created October 8, 2014 21:40
Show Gist options
  • Save joegiralt/191f39262ee379ab91da to your computer and use it in GitHub Desktop.
Save joegiralt/191f39262ee379ab91da to your computer and use it in GitHub Desktop.
scribbles magic squares
hash = {}
perfect_sqrs = []
(1..300).each do |num|
perfect_sqrs << (num**2)
end
find_partition = lambda do |elts|
p "ELTS: #{elts}"
if elts.empty?
[[]]
else
highest = elts.pop
elts.combination(2).map do |others|
# p "ELTS: #{elts} OTHERS: #{others}"
find_partition[elts - others].map { |part| part << [highest,*others] }
end.inject(:+)
end
end
find_partition[perfect_sqrs].first.each do |set|
p set
if hash["#{set.inject(:+)}"].nil?
hash["#{set.inject(:+)}"] = []
end
hash["#{set.inject(:+)}"].push( set.sort!) unless hash["#{set.inject(:+)}"].include?(set.sort!)
p hash["#{set.inject(:+)}"]
end
p hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment