Skip to content

Instantly share code, notes, and snippets.

@mythicalprogrammer
Created June 20, 2013 03:50
Show Gist options
  • Save mythicalprogrammer/5820186 to your computer and use it in GitHub Desktop.
Save mythicalprogrammer/5820186 to your computer and use it in GitHub Desktop.
Given an integer input, return the total number of all perfect square square. Note 2^2+3^2 and 3^2+2^2 is counted as one.
require 'set'
number = gets
number = number.to_i
max = Math.sqrt(number).to_i
set = Set.new
n_perf = 0
for i in 0..max
for j in 0..max
#puts "#{i}^2 + #{j}^2"
#puts i*i + j*j
if number == (i*i+j*j)
set.add(Set.new [i,j])
n_perf = n_perf+1
end
end
end
puts set.length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment