-
Notifications
You must be signed in to change notification settings - Fork 0
/
e4.py
36 lines (30 loc) · 910 Bytes
/
e4.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
#!/usr/bin/env python3
'''
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.
'''
import itertools
def is_palindrome(n):
word = str(n)
half = int(len(word)/2)
a = itertools.islice(word, 0, half)
b = itertools.islice(reversed(word), 0, half)
return all(x == y for x, y in zip(a, b))
def main():
high = 999
low = 99
result = 0
for x, y in itertools.product(range(high, low, -1), range(high, low, -1)):
# Ignore already seen pairs
if x < y:
continue
mul = x * y
# Skip smaller products
if mul < result:
continue
if is_palindrome(mul):
result = max(mul, result)
print('Result: ', result)
if __name__ == '__main__':
main()