Skip to content

Instantly share code, notes, and snippets.

@andersonvom
Created April 16, 2012 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonvom/2395906 to your computer and use it in GitHub Desktop.
Save andersonvom/2395906 to your computer and use it in GitHub Desktop.
Recycled Numbers
# Google Code Jam: http://code.google.com/codejam/contest/1460488/dashboard#s=p2
def rotate(n, digits, size)
magnitude = (10 ** (size-digits))
first = n / magnitude
last = n % magnitude
result = last * (10**digits) + first
end
def num_pairs(s, e)
count = 0
size = s.to_s.size
rotations = size - 1
(s..(e-1)).each do |i|
n = m = i
rotations.times do |r|
m = rotate n, (r+1), size
break if m == n
count += 1 if n < m and m <= e
end
end
count
end
# Run all test cases
if __FILE__ == $0
while tests = gets
tests = tests.to_i # <= 50
tests.times do |i|
num = gets # <= 2_000_000
s, e = num.rstrip!.split ' '
recycled_nums = num_pairs s.to_i, e.to_i
print "Case ##{i+1}: #{recycled_nums}\n"
end
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment