I have come across many many solutions in StackOverflow for problem 4 in project Euler. My question is not about asking again a solution to problem 4 from project Euler which is already implemented in StackOverflow. Instead, i have come across an improved solution Improved solution by ROMANIA_engineer. I have two questions against the improved solution. Anyways I have posted the solution below, for people to look into it.
public static void main(String[] args) {
int max = -1;
for ( int i = 999 ; i >= 100 ; i--) {
if ( max >= i*999 ) {
break;
}
for (int j = 999 ; j >= i ; j-- ) {
int p = i * j;
if ( max < p && isPalindrome(p) ) {
max = p;
}
}
}
System.out.println(max > -1? max : "No palindrome found");
}
Questions
- Why there is a condition (max >= i * 999) ?. What are we going to achieve, by including such an inexpensive operation?
From the below snippet,
for (int j = 999 ; j >= i ; j-- ) {
int p = i * j;
if ( max < p && isPalindrome(p) ) {
max = p;
}
}
- Instead of
j >= 100
, it is givenj >= i
. I can see a lot of time is saved, however, I wanted to know the intention behind it.