-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp4-largestPalindrome.py
executable file
·48 lines (34 loc) · 1.16 KB
/
p4-largestPalindrome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python
# Find the largest palindrome made from the product of two 3-digit
# numbers.
#
# A palindromic number reads the same both ways. The largest
# palindrome made from the product of two 2-digit numbers is 9009 =
# 91x99.
def isPalindrome(num):
if num == int(str(num)[::-1]):
return True
return False
def testIsPalindrome():
nums=[1,10,11,101,110,1001,1010]
for n in nums:
print "%d palindrome %s"%(n, str(isPalindrome(n)))
palindromes=set()
def largestPalindrome():
lpal=0
for i in range(999,100,-1):
for j in range(999,100,-1):
if isPalindrome(i*j):
#print "%d = %d * %d PALINDROME"%(i*j, i, j)
if i*j >= lpal:
print "%d = %d * %d LARGEST PALINDROME"%(i*j, i, j)
lpal=i*j
#palindromes.update((str(i+j)))
palindromes.add(str(i*j))
#print sorted(list(palindromes))
#print list(palindromes)
def solveIt():
largestPalindrome()
if __name__ == '__main__':
import timeit
print(timeit.timeit("solveIt()", setup="from __main__ import solveIt", number=1))