Problem :
The largest palindrome number 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.
Answer :906609
This Is very easy ProjectEuler Problem. but it worth trying. First of all Try it by yourself then back to us even if you succeed because getting answer should not be our only objective. How efficient is our approach also matters.
First Approach People try is find largest palindrome in range 100*100 to 999*999 this is a very obvious but wrong approach because all numbers in this range are not multiple of 2 3 digit number .
Try this Python Code to check it out
- #Python 2.7
- #beginner to computer Science
- limit=999*999
- base=100
- def ispalindrom(n):
- st=str(n)
- if(st==st[::-1]):
- return True
- else:
- return False
- for i in range(limit,base,-1):
- if ispalindrom(i):
- print i
- break
Now try Another approach :> Check all number that is multiple of two 3 digit number. and find largest palindrome number among them.
It will give correct result but at what cost
We know multiplication is very costly in computation & this approach takes about =806907 multiplication of two three digit number
Check it out by yourself
- //C-Program
- //Very costly with respect to calculation required
- #include<stdio.h>
- #include<string.h>
- int isPalindrome_num(long long int n){
- /*
- ****<string.h>
- Used to check number is palindrome or not
- """
- isPalindrome_num(n)
- return 1 if palindrome
- return 0 if not palindrome
- """
- */
- char a[30];
- char b[30];
- sprintf(a,"%d",n); //convert number in string & store it in array a
- strcpy(b,a); //copy b<-a
- _strrev(b); //reverse b
- if(strcmp(a,b)==0) //compare
- return 1;
- else
- return 0;
- }
- int main(){
- int i,j,p,count=0,t1,t2;
- int max=10001; //least 4 digit number
- for(i=999;i>100;i--){
- for(j=999;j>100;j--){
- p=i*j;
- count++;
- //printf("%d, ",p);
- if(p>max && isPalindrome_num(p)==1){
- max=p;
- t1=i;
- t2=j;
- break;
- }
- }
- }
- printf("largest required palindrome =%d * %d = %d",t1,t2,max);
- printf("\nnumber of multplication required = %d",count);
- return 0;
- }
YOu Can See number of multiplication required |
let we find an palindrome X then reduce all possible multiplication which produce result less than X
Then you can see how efficient your code become .
Check it Out.
- //C-Program
- //Optimize number of multiplication required
- #include<stdio.h>
- #include<string.h>
- int isPalindrome_num(long long int n){
- /*
- ****<string.h>
- Used to check number is palindrome or not
- """
- isPalindrome_num(n)
- return 1 if palindrome
- return 0 if not palindrome
- """
- */
- char a[30];
- char b[30];
- sprintf(a,"%d",n); //convert number in string & store it in array a
- strcpy(b,a); //copy b<-a
- _strrev(b); //reverse b
- if(strcmp(a,b)==0) //compare
- return 1;
- else
- return 0;
- }
- int main(){
- int i,j,p,count=0,t1,t2;
- int max=10001; //least 4 digit number
- for(i=999;i>100;i--){
- for(j=999;j>100;j--){
- p=i*j;
- count++;
- if(p<max) //stop mult when p<max it is worthless as we want largest
- break;
- else
- if(isPalindrome_num(p)==1){ //if p>max & it is a palindrom
- max=p; //update max
- t1=i; //just for printing result
- t2=j;
- break;
- }
- }
- }
- printf("largest required palindrome =%d * %d = %d",t1,t2,max);
- printf("\nnumber of multplication required = %d",count);
- return 0;
- }
Optimize code >largest palindrome |
Please Suggest If you found Any Bug or you know better approach to solve it .
Thank You
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteMagicjack Support 1-800-653-4096,
ReplyDeleteCustomer Support For Magicjack
COMPLETEPCSOLUTION is one of the best way to resolve
all problems like magicjack technical support,s
magicjack customer suppport and installation,
magicjack contact number