-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem004.py
More file actions
34 lines (28 loc) · 963 Bytes
/
problem004.py
File metadata and controls
34 lines (28 loc) · 963 Bytes
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
largest = 0
#for i in range(1000):
# for j in range(1000):
# product = i * j
# text = str(product)
# if(text == text[::-1]):
# # it's a palindrome, check if it's bigger
# if product > largest:
# largest = product
# print(i, "*", j, "=", text)
# with optimizations
# - only multiply 3 digit numbers
# - go backwards and exit inner loop early
# - avoid duplicates (i.e. i*j = j*i)
for i in range(1000, 99, -1):
for j in range(i, 99, -1):
#print(i, "*", j)
product = i * j
text = str(product)
if product < largest:
#print(i, "*", j, "< largest (", largest, ") BREAK inner loop!")
break
if(text == text[::-1]):
#it's a palindrome, check if it's bigger
if product > largest:
largest = product
print(i, "*", j, "=", text)
print("Largest palindrome:", largest)