Solution to Project Euler 4

Here’s the description for Problem 4 on Project Euler:

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.

As usual the brute force approach was my first guess, and it worked pretty well. I just implemented a small function to test whether the number is palindrome (by inverting it and comparing the result with itself), and then I tested all the multiples of number with 3 digits. Code is below:

``````
#include <stdio.h>
#include <math.h>

int isPali(n){
int copy = n;
int complement = 0;

while (copy>0){
complement = complement * 10 + copy % 10;
copy/=10;
}

if (complement==n)
return 1;
return 0;
}

int main(){
int i,j,ans,x;
ans=0;

for (i=1;i<1000;i++){
for (j=1;j<1000;j++){
x=i*j;
if (isPali(x)&&x>ans)
ans=x;
}
}

printf("%dn",ans);
return 0;
}``````

One thought on “Solution to Project Euler 4”

1. kishore

Why are you looping from 1 ? as the answer you are looking for is a three digit product ? .. it can save the extra unnecessary loops !