Python Implementation of Problem 4

View source code here on GitHub!

Includes

Problem Solution

Project Euler Problem 4

I couldn't figure out how to do this as efficiently as I would have liked. I am SURE that there is a better way to check if a number is a palindrome, but I could not think of one.

Revision 1:

I changed is_palindrome to take in any repr function that you care to give it

Problem:

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.

python.src.p0004.main() int
 1"""
 2Project Euler Problem 4
 3
 4I couldn't figure out how to do this as efficiently as I would have liked. I am
 5SURE that there is a better way to check if a number is a palindrome, but I
 6could not think of one.
 7
 8Revision 1:
 9
10I changed is_palindrome to take in any repr function that you care to give it
11
12Problem:
13
14A palindromic number reads the same both ways. The largest palindrome made from
15the product of two 2-digit numbers is 9009 = 91 × 99.
16
17Find the largest palindrome made from the product of two 3-digit numbers.
18
19"""
20from itertools import combinations
21
22from .lib.utils import is_palindrome
23
24
25def main() -> int:
26    result = 0
27    for x, y in combinations(range(100, 1000), 2):
28        num = x * y
29        if num > result and is_palindrome(num):
30            result = num
31    return result

Tags: palindrome