Skip to content

Instantly share code, notes, and snippets.

@Surgo
Created February 16, 2011 02:48
Show Gist options
  • Save Surgo/828768 to your computer and use it in GitHub Desktop.
Save Surgo/828768 to your computer and use it in GitHub Desktop.
SRM484 - div2 - level two
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""SRM484 - div2 - level two
When cat Taro and rabbit Hanako were practicing hard for SRM 484,
they noticed an interesting property of 484.
They called it Rabbit Number.
Let S(n) be the sum of the digits of n. For example,
S(484) = 4+8+4 = 16 and S(22) = 2+2 = 4. A positive integer x is
called a Rabbit Number if S(x*x) = S(x)*S(x). For example,
22 is a Rabbit Number because S(484) = S(22)*S(22).
Return the number of Rabbit Numbers between low and high, inclusive.
Definition:
Class: RabbitNumber
Method: theCount
Parameters: int, int
Returns: int
Method signature: int theCount(int low, int high)
"""
class RabbitNumber(object):
"""Afraid Of Even
Constraints:
- low will be between 1 and 1,000,000,000, inclusive.
- high will be between low and 1,000,000,000, inclusive.
>>> rabbit = RabbitNumber()
>>> print rabbit.theCount(22, 22)
1
>>> print rabbit.theCount(484, 484)
0
>>> print rabbit.theCount(1, 58)
12
>>> print rabbit.theCount(58, 484)
24
>>> print rabbit.theCount(1000000000, 1000000000)
1
"""
def __init__(self):
pass
def theCount(self, low, high):
def s(n):
return sum([int(i) for i in str(n)])
r = 0
for i in range(low, high+1):
if s(i*i) == s(i)*s(i):
r += 1
return r
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment