Skip to content

Instantly share code, notes, and snippets.

@homelinen
Created April 13, 2013 16:03
Show Gist options
  • Save homelinen/5378973 to your computer and use it in GitHub Desktop.
Save homelinen/5378973 to your computer and use it in GitHub Desktop.
Ruby implementation for Part C of the Google Code Jam 2013
#!/usr/bin/env ruby
def is_palindrome?(num)
num == num.to_s.reverse.to_i
end
def is_square_of_palindrome?(num)
root = Math.sqrt(num)
if root == root.floor
is_palindrome? root.to_i
else
false
end
end
# Main
#
#
file = File.new("input.txt", "r")
# Skip the first line
line = file.gets
cur_case = 0
while (line = file.gets)
p line
cur_case += 1
result = 0
barrier = line.strip.split(" ")
n = barrier[0].to_i
while n < barrier[1].to_i
if is_palindrome? n and is_square_of_palindrome? n
result += 1
end
n += 1
end
puts "Case \##{cur_case}: #{result}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment