Skip to main content
Code Review

Return to Revisions

2 of 2
added 3 characters in body
user avatar
user avatar

Project Euler 4 - Largest palindrome product

I submitted a solution to Project Euler 4 exercise: Largest palindrome product in Java. The content of following exercise:

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.

I wrote a method called isPalindrome that checks is reversing of string the same as output string. I have to obviously convert int to String to check is given number is palindrome. I created palindromes list and I loop for all possible three-digits numbers that would be multiplied and I check are these products palindromes. If yes, then I add it product to list. Next, I select only maximum palindrome from list and I print it to console.

Main.java:

package pl.hubot.projecteuler.problem4;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
 public static void main(String[] args) {
 List<Integer> palindromes = new ArrayList<>();
 for (int i = 100; i <= 999; i++) {
 for (int j = 100; j <= 999; j++) {
 if (isPalindrome(Integer.toString(i * j)))
 palindromes.add(i * j);
 }
 }
 System.out.print(Collections.max(palindromes));
 }
 private static boolean isPalindrome(String str) {
 return str.equals(new StringBuilder(str).reverse().toString());
 }
}
user120313
lang-java

AltStyle によって変換されたページ (->オリジナル) /