Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created March 10, 2011 10:31
Show Gist options
  • Select an option

  • Save mizchi/863898 to your computer and use it in GitHub Desktop.

Select an option

Save mizchi/863898 to your computer and use it in GitHub Desktop.
euler 4
#!/usr/bin/env python
#-*- encoding:utf-8-*-
"""
左右どちらから読んでも同じ値になる数を回文数という。 2桁の数の積で表される回文数のうち、最大のものは 9009 = 91 × 99 である。
では、3桁の数の積で表される回文数のうち最大のものはいくらになるか。
"""
def is_reversible(n,side=0):
if n[side] == n[-1-side]:
if len(n)/2 == side:
return True
return is_reversible(n,side+1)
return False
def max_reversible_num(keta):
n1 = n2 = sum([ 9 * pow(10,i) for i in range(keta)])
hit = []
for i in range(n1)[::-1]:
for j in range(n2)[::-1]:
if is_reversible(str(i*j)):
hit.append( i*j )
return max(hit)
def main():
print max_reversible_num(3)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment