Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # http://projecteuler.net/index.php?section=problems&id=4
- # Find the largest numerical palindrome, made from the product of 2 3-digit numbers
- # Presumably, we start with the maximum and go down until we reach a palindrome
- def isPalindrome(n):
- """Function that returns whether the given number is a palindrome"""
- n = str(n)
- length = len(n)
- for i in range(length / 2):
- if (n[i] != n[-i-1]):
- #print n[i] + ' is not equal to ' + n[-i-1]
- return False
- return True
- #print isPalindrome(123454321)
- largest = 0
- for i in range(999, 100, -1):
- for j in range(i, 100, -1):
- if isPalindrome(i*j):
- if i*j > largest:
- largest = i*j
- else:
- break
- print `largest` + ' is the largest palindrome!'
- # Hurrah! By starting j at i each time, and going to the
- # next value of i when the number isn't larger, I decreased
- # the execution time to nearly a third! :D
Advertisement
Add Comment
Please, Sign In to add comment