Skip to content

Instantly share code, notes, and snippets.

@dustMason
Created August 14, 2017 22:27
Show Gist options
  • Save dustMason/487eabfa2cafd2e2b0c4814903c21772 to your computer and use it in GitHub Desktop.
Save dustMason/487eabfa2cafd2e2b0c4814903c21772 to your computer and use it in GitHub Desktop.
def optimal_move state
return state if Math.sqrt(state) % 1 == 0 # its a perfect square
(1..Math.sqrt(state)).each { |n| return n*n if optimal_move(state - (n*n)) == -1 }
return -1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment