Guest User

Untitled

a guest
Jul 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. class Euler{
  2.  
  3.     public static void main(String[] args){
  4.    
  5.         int currentA = 100;
  6.         int currentB = 100;
  7.         int product = 0;
  8.         int finalProduct = 0;
  9.  
  10.         //loops through currentA and currentB for values 100-999. I wanted to use currentA and currentB in the for loops, but I got a "duplicate local variable" exception
  11.         for(int currentA1 = 100; currentA1 <= 999; currentA1++){
  12.             for(int currentB1 = 100; currentB1 <= 999; currentB1++){
  13.  
  14.                 //Calculates the product, passes the product to inPalindrome, and records the largest palindrome product
  15.                 product = currentA * currentB;
  16.                 if(isPalindrome(product) && product > finalProduct){
  17.                         finalProduct = product;
  18.                 }
  19.                 currentB++;
  20.             }
  21.             currentA++;
  22.         }
  23.         //Prints the final answer.
  24.         System.out.printf("The answer is %d.", finalProduct);
  25.     }
  26.  
  27.     //Checks if an integer is a palindrome
  28.     public static boolean isPalindrome(int candidate){
  29.    
  30.         int hunThous = 0;
  31.         int tenThous = 0;
  32.         int thous = 0;
  33.         int huns = 0;
  34.         int tens = 0;
  35.         int ones = 0;
  36.         int which = 0;
  37.  
  38.         //Sets variables equal to individual digits for 5-digit numbers.
  39.         if(candidate >= 10_000 && candidate <= 99_999){
  40.             tenThous = candidate / 10000;
  41.             thous = candidate % 10000 / 1000;
  42.             huns = candidate % 1000 / 100;
  43.             tens = candidate % 100 / 10;
  44.             ones = candidate % 10;
  45.                
  46.             which = 1;
  47.  
  48.         //Sets variables equal to individual digits for 6-digit numbers.
  49.         }else if(candidate >= 100_000 && candidate <= 999_999){
  50.             hunThous = candidate / 100_000;
  51.             tenThous = candidate % 100_000 / 10000;
  52.             thous = candidate % 10000 / 1000;
  53.             huns = candidate % 1000 / 100;
  54.             tens = candidate % 100 / 10;
  55.             ones = candidate % 10;
  56.            
  57.             which = 2;
  58.            
  59.  
  60.         }else{
  61.             System.out.println("Error. Product not within range: 10,000 - 999,999.");
  62.         }
  63.        
  64.         //Checks to see if opposite digits match. Palindromes will have matching opposite digits.
  65.         if(which == 1 && tenThous == ones && thous == tens){
  66.             return true;
  67.         }else if(which == 2 && hunThous == ones && tenThous == tens && thous == huns){
  68.             return true;
  69.         }else{
  70.             return false;
  71.         }
  72.         }
  73.     }
Add Comment
Please, Sign In to add comment