P.E.4: Largest Palindrome Number

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.


The answer is 906609.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Brock Brown
Jan 4, 2015
1
2
3
4
5
6
7
8
9
def palindrome(x):
    return str(x) == str(x)[::-1]
biggest = 0
for a in xrange(100,1000):
    for b in xrange(100,1000):
        if palindrome(a*b):
            if a*b > biggest:
                biggest = a*b
print "Answer:", biggest

Since, we are just interested in largest number, you could have safely searched for a , b a, b in range say 900 , 999 900,999 . This would decrease execution time.

Rushikesh Jogdand - 5 years ago
Swapnil Bhargava
Sep 22, 2014

Here's my C++ code

 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
48
49
#include<iostream>
#include<cstdio>
#include<cmath>
#define llimit 100
#define ulimit 999
using namespace std;
bool checkPalindrome(long long int n)
{
    int i;
    if(n/100000)//6 digits
    {
        for(i=1;i<=3;i++)
        {
            if(((n%(long long int)pow(10,i))/(long long int)pow(10,i-1))==((n%(long long int)pow(10,7-i))/(long long int)pow(10,7-i-1)))
                continue;
            else 
                return false;
        }
    }
    else//5 digits
    {
        for(i=1;i<=2;i++)
        {
            if(((n%(long long int)pow(10,i))/(long long int)pow(10,i-1))==((n%(long long int)pow(10,6-i))/(long long int)pow(10,6-i-1)))
                continue;
            else
                return false;
        }
    }
    return true;
}
int main()
{
    long long int i,j,num,mnum=-1;
    for(i=ulimit;i>=llimit;i--)
    {
        for(j=ulimit;j>=llimit;j--)
        {
            num=i*j;
            if(checkPalindrome(num))
            {
                if(num>mnum)
                    mnum=num;
            }
        }
    }
    printf("%lld",mnum);
    return 0;
}

Arvind Srinivasan
Sep 19, 2014

My python code is as follows:

# -*- coding: utf-8 -*-
"""
Euler Problem #4:
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."""
for i in range(99,1000):
    largest = None
    for j in range(999, 0, -1): # decreasing from 999
        if j * 999 < largest: # if True, impossible for higher product
            break 
        for k in range(999, i-1, -1): # decrease from 999 and exclude i-1
            product = k*j
            if str(product) == str(product)[::-1]:#[::-1] reverses the string
                if product > largest:
                    largest = product
    print " The largest palindrome made from the product of two 3-digit numbers is: "
    print largest
    break

Hope this helps!

Isn't this problem from Project Euler?

Edward Jiang - 6 years, 8 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...