Created
March 23, 2017 16:35
-
-
Save leihuagh/0d004ca0d5ade98e69791bafc11bbc4b to your computer and use it in GitHub Desktop.
Project Euler Problem 4: Largest palindrome product
This file contains 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
/* | |
Project Euler Problem 4 | |
Given: | |
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. | |
*/ | |
package problem0004; | |
public class Palindrom { | |
public static void main(String[] args) { | |
int palindrom = -1; | |
boolean found = false; | |
for (int i=999; i>100; i--) { | |
for (int j=i; j>100; j--) { | |
int product = j*i; | |
if ((isPalindrom(product) == true) && (product > palindrom)) { | |
palindrom = product; | |
found = true; | |
System.out.print("The largest palindrom number for 3-digit numbers is: " + j + " x " + i + " = "); | |
break; | |
} | |
} | |
if (found == true) { | |
System.out.println(palindrom); | |
break; | |
} | |
} | |
} | |
private static boolean isPalindrom(int product) { | |
String pd = String.valueOf(product); | |
int n = pd.length(); | |
for (int i = 0; i < (n/2); ++i) { | |
if (pd.charAt(i) != pd.charAt(n - i - 1)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment