top of page

Project Euler Problem 4

7 days ago

1 min read

0

5

0


Finding the Largest Palindrome Product with Python

Project Euler Problem 4 asks us to find the largest palindrome made from the product of two 3-digit numbers. Let's solve it with Python.


The Problem

A palindromic number reads the same both ways - like 9009 or 906609. We need to multiply all combinations of 3-digit numbers and identify the largest palindrome in those products.


The Solution

python

palindrome_product = []
for i in range(0,999):
    for j in range(0,999):
        g = str(i*j)
        g1 = g[::-1]
        if g == g1:
            palindrome_product.append(int(g))

print(max(palindrome_product))

How It Works

  1. Nested loops iterate through numbers from 0 to 998

  2. String conversion converts each product to a string g

  3. String reversal using Python's slice notation [::-1] creates the reversed version g1

  4. Palindrome check compares g with g1

  5. Track results by appending palindromes to the list and finding the maximum


The Result

The largest palindrome from the product of two numbers in this range is 906609.

7 days ago

1 min read

0

5

0

Related Posts

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page