-
Notifications
You must be signed in to change notification settings - Fork 16
/
0004-palindrome.py
41 lines (37 loc) · 1.31 KB
/
0004-palindrome.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
"""
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
"""
def traditional():
"""
Nothing fancy in this function, just checks for palindromes
"""
palindromes = []
for i in range(999, 100, -1):
for j in range(i, 100, -1):
product = i * j
product_str = str(product)
if product_str == product_str[::-1]:
palindromes.append(product)
return max(palindromes)
def a_little_step_up():
"""
Tiny optimizations over the previous approach
"""
palindromes = []
for i in range(999, 100, -1):
if i % 11 == 0:
# If i is divisible by 11, check for all j's
x = range(i, 100, -1)
else:
# If i is not divisible by 11, check for all j's divisible by 11
x = range(i - (i % 11), 100, -11)
for j in x:
product = i * j
product_str = str(product)
if product_str == product_str[::-1]:
palindromes.append(product)
return max(palindromes)
print "Answer by traditional method: ", traditional()
print "Answer by optimized method: ", a_little_step_up()