Project Euler | Problem 4 | Largest palindrome product



Problem Description :


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 3-digit numbers?

Concept :


A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed. For example 99 is a palindrome but 89 is not.


Java Program :


package com.javamultiplex.projecteuler;

/**
 * 
 * @author Rohit Agarwal
 * @category Project Euler Problems
 * @problem Largest palindrome product
 * 
 */
public class Problem4 {

 public static void main(String[] args) {

  int maxThreeDigit = 999;
  int minThreeDigit = 100;
  int multInteger = 0;
  // lowest multiple of 2 three digit numbers
  int max = 100 * 100;
  for (int i = maxThreeDigit; i >= minThreeDigit; i--) {
   for (int j = i; j >= minThreeDigit; j--) {
    multInteger = i * j;

    // Checking multiplication is palindrome or not
    if (isPalindrome(multInteger)) {
     if (multInteger > max) {
      max = multInteger;
      break;
     }
    }
   }
  }
  System.out.println("Largest palindrom made from the product of two 3-digit numbers is: "
      + max);
 }

 private static boolean isPalindrome(int multInteger) {

  boolean result = false;
  String multString = null;
  StringBuffer multStringBuffer = null;
  // Converting Integer to String
  multString = String.valueOf(multInteger);
  /*
   * Converting String to StringBuffer, We want to reverse the string
   * thats why we have to use StringBuffer.
   */
  multStringBuffer = new StringBuffer(multString);
  if (multString.equals(multStringBuffer.reverse().toString())) {
   result = true;
  } else {
   result = false;
  }
  return result;
 }

}


Output :


Output of Project Euler Problem 4 - Largest Palindrome Product in Java
Output - Project Euler Problem 4

References :


https://projecteuler.net/problem=4

https://en.wikipedia.org/wiki/Palindromic_number

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html

Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

Tags : Project Euler Problem 4, Solution in Java, Mathematics problems, Largest palindrome product, if else statement, for loop, String, StringBuffer.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.


Let's Get Connected: Linkedin | Facebook |

Project Euler | Problem 4 | Largest palindrome product Project Euler | Problem 4 | Largest palindrome product Reviewed by Rohit Agarwal on 12/15/2016 Rating: 5

No comments:

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.

Powered by Blogger.