Skip to content

Instantly share code, notes, and snippets.

@aausch
Last active December 24, 2015 00:59
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 aausch/6720461 to your computer and use it in GitHub Desktop.
Save aausch/6720461 to your computer and use it in GitHub Desktop.
http://projecteuler.net/problem=4 A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.
# Copyright 2013, Alex Ausch
# Free to use under attribution license: http://creativecommons.org/licenses/by/2.0/ca/
def is_palindrome(num):
palindrome = str(num)
return palindrome == palindrome[::-1]
def fn(n):
max_palindrome = 1
for x in range(n,1,-1):
if x*n < max_palindrome:
break
for y in range(n,x-1,-1):
if is_palindrome(x*y) and x*y > max_palindrome:
max_palindrome = x*y
elif x * y < max_palindrome:
break
return max_palindrome
print fn(999)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment