/ Published in: C++
                    
                                        
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
/*
Alexander DeTrano
2/1/2010
Project 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.
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int palindromecheck(int n);
int main(){
vector<int> myArray;
int num=0; int count=0;
for(int i=1;i<=999;i++){
for(int j=1;j<=999;j++){
num=i*j;
if(palindromecheck(num)==1){
myArray.push_back(count); //expand size of array by 1
myArray[count]=num;
count+=1;
}
}
}
cout<<myArray.size(); //#of elements before removing duplicats
sort(myArray.begin(),myArray.end()); //sort vector from small to large
myArray.erase(unique(myArray.begin(),myArray.end()),myArray.end()); //erase duplicates
cout<<myArray.size()<<endl; //print new size of array
cout<<"Max Palindrome is: "<<*(max_element( myArray.begin(), myArray.end() ) ); //find max element in array
return 0;
}
int palindromecheck(int n)
{
int r,sum=0,temp,flag;
temp=n;
while(n){
r=n%10;
n=n/10;
sum=sum*10+r;
}
if(temp==sum)
flag=1;
else
flag=0;
return flag;
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                