Created
July 20, 2013 04:46
-
-
Save rfaisal/6043910 to your computer and use it in GitHub Desktop.
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 n-digit numbers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<math.h> | |
| #include<stdlib.h> | |
| int is_palindrome(int n){ | |
| int len=0; | |
| while(n/(int)pow(10,len)) len++; | |
| while(len>1){ | |
| int mul=pow(10,len-1); | |
| int d1=n/mul; | |
| int d2=n%10; | |
| if(d1!=d2) return 0; | |
| n-=d1*mul; | |
| n/=10; | |
| len-=2; | |
| } | |
| return 1; | |
| } | |
| int largest_palendrome_product(int digit){ | |
| int max=(int)pow(10,digit)-1; | |
| int min=(int)pow(10,digit-1); | |
| int ret=-1; | |
| for(int i=max;i>=min;i--){ | |
| for(int j=max;j>=min;j--){ | |
| int num=i*j; | |
| if(num>ret && is_palindrome(num)) ret=num; | |
| } | |
| } | |
| return ret; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment