-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-004.py
More file actions
47 lines (36 loc) · 1.14 KB
/
problem-004.py
File metadata and controls
47 lines (36 loc) · 1.14 KB
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
"""
Problem 4 - Largest Palindrome Product
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 is_palindrome(n: int) -> bool:
"""
Parameters
n (int): number to test if palindrome
Returns
boolean
"""
if str(n) == str(n)[::-1]:
return True
return False
def largest_palindrome(low: int, high: int) -> int:
"""
Parameters
low (int): low integer of range
high (int): high integer of range
Returns
largest_palindrome (int): largest palindrome product of
values in [low, high]
"""
largest_palindrome = 0
for i in range(low, high):
for j in range(low, high):
if is_palindrome(i * j) and (i * j) > largest_palindrome:
largest_palindrome = i * j
return largest_palindrome
if __name__ == "__main__":
print(
"The largest palindrome product of three-digit numbers is: "
+ str(largest_palindrome(100, 1000))
)