Posted By

mju4t on 02/06/11


Tagged


Versions (?)

Project Euler Problem 4


 / Published in: C++
 

  1. /*
  2. Alexander DeTrano
  3. 2/1/2010
  4. Project Euler - Problem 4
  5.  
  6. A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
  7.  
  8. Find the largest palindrome made from the product of two 3-digit numbers.
  9. */
  10. #include <iostream>
  11. #include <vector>
  12. #include <algorithm>
  13. using namespace std;
  14. int palindromecheck(int n);
  15.  
  16. int main(){
  17. vector<int> myArray;
  18. int num=0; int count=0;
  19. for(int i=1;i<=999;i++){
  20. for(int j=1;j<=999;j++){
  21. num=i*j;
  22. if(palindromecheck(num)==1){
  23. myArray.push_back(count); //expand size of array by 1
  24. myArray[count]=num;
  25. count+=1;
  26. }
  27.  
  28. }
  29. }
  30. cout<<myArray.size(); //#of elements before removing duplicats
  31. sort(myArray.begin(),myArray.end()); //sort vector from small to large
  32. myArray.erase(unique(myArray.begin(),myArray.end()),myArray.end()); //erase duplicates
  33. cout<<myArray.size()<<endl; //print new size of array
  34. cout<<"Max Palindrome is: "<<*(max_element( myArray.begin(), myArray.end() ) ); //find max element in array
  35.  
  36. return 0;
  37. }
  38.  
  39.  
  40. int palindromecheck(int n)
  41. {
  42. int r,sum=0,temp,flag;
  43. temp=n;
  44. while(n){
  45. r=n%10;
  46. n=n/10;
  47. sum=sum*10+r;
  48. }
  49.  
  50. if(temp==sum)
  51. flag=1;
  52. else
  53. flag=0;
  54. return flag;
  55. }

Report this snippet  

You need to login to post a comment.