Created
November 27, 2014 14:14
-
-
Save shelling/bb5f3aed33dce6ceaa26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
def integer?(number) | |
(number % 1) == 0 | |
end | |
def double_square_number?(number) | |
sqrt = Math.sqrt(number) | |
if integer?(sqrt) | |
[sqrt.to_i, 0] | |
else | |
sqrt.floor.downto(1) do |n| | |
if integer?(m = Math.sqrt(number - n ** 2)) | |
return [n, m.to_i] | |
end | |
end | |
return false | |
end | |
end | |
for i in 1..104 do | |
puts "#{i} => #{double_square_number?(i).inspect}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment