Largest Palindrome value made from the product of 2 3-digit numbers -


public static boolean ispal(int value) {     string temp = string.valueof(value);     int begin=0;     int end=(temp.length())-1;     int middle=(begin+end)/2;     for(int count=begin; count<=middle; count++)     {         if(temp.charat(count)!=temp.charat(end))         {             return false;         }         else if(temp.charat(count)==temp.charat(end))         {             end--;         }     }     return true; }  public static int largestpalindromenumber() {     int largestnumber=0;     int num1=999;     int num2=999;     while(num1>=100 && num2>=100)     {         if(ispal(num1*num2))         {             largestnumber=num1*num2;             break;         }         else         {             if(num1==num2-1)             {                 num2--;             }             else             {                 num1--;             }         }     }     return largestnumber; } 

when run this, returns palindrome 698896, palindrome, not highest palindrome made product of 2 3 digit numbers. else statement in largestpalindrome method decrements in style in order maintain checking highest number because 998*998 greater 999*997 , on.

999*999 -> check: num1 decrement

998*999 -> check: num2 decrement

998*998 -> check

unfortunately, unable find correct highest palindrome.

your decrement logic never check values of num1 , num2 differ more 1. example, you'll never check 999*997. need like:

int greatestpal = 0; for(int num1 = 999; num1 > 99; num1--){   for(int num2 = 999; num2 > 99; num2--){     int product = num1 * num2;     if(ispal(product)){       greatestpal = math.max(product, greatestpal);       break;     }   } } 

in effect, each value of num1, find largest num2 forms palendrome. that's largest palendrome iteration of num1. keep track of greatest 1 find overall.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -