Skip to content

Instantly share code, notes, and snippets.

@benmmurphy
Created April 21, 2013 18:29
Show Gist options
  • Save benmmurphy/5430539 to your computer and use it in GitHub Desktop.
Save benmmurphy/5430539 to your computer and use it in GitHub Desktop.
1st Large
#!/usr/bin/env ruby
def is_palindrome(n)
s = n.to_s
s.reverse == s
end
def all
result = []
(1..10000000).each do |sqrt|
n = sqrt * sqrt
if is_palindrome(sqrt) && is_palindrome(n)
result << n
end
end
result
end
def count(all, a, b)
total = 0
all.each do |n|
if n >= a && n <= b
total = total + 1
end
end
total
end
def run(f)
lines = File.readlines(f)
cases = lines[0].to_i
cached = all
(1..cases).each do |c|
a, b = lines[c].split(" ").map(&:to_i)
result = count(cached, a, b)
puts "Case ##{c}: #{result}"
end
end
run(ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment