Reputation: 25
Solution as suggested by MrSmith42: Error in the p for loop. Fixing that corrected it.
I tried writing the code for question four of ProjectEuler.net archives. Need to find the largest palindrome which is a product of two 3-digit numbers.
I understand that this code is not the most efficient. For one, it goes upto 999,999. When the max should not exceed 999*999 = 998,001. I want to just get done with the problem. But I don't know if I can use Strings or String Tokenizer for this problem.
public class Main {
public static void main(String[] args) {
List<Integer> arr = new ArrayList<>();
int a = 100000, b = 10000, c =1000, d = 100, e = 10, f = 1;
int m, n, p, q, s, t;
//The plan is to increment the numbers by one, starting from 100,000 upto 999,999.
for (m = 1; m <= 9; m++) {
for (n = 0; n <= 9; n++) {
for (p = 0; n <= 9; n++) {
for (q = 0; q <= 9; q++) {
for (s = 0; s <= 9; s++) {
for (t = 0; t <= 9; t++) {
if (t*a + s*b + q*c + p*d + n*e + m*f == m*a + n*b + p*c + q*d + s*e + t*f) {
arr.add(m*a + n*b + p*c + q*d + s*e + f*t); }}}}}
}
}
for (int x: arr)
System.out.println(x);
}
}
I am getting numbers with the format ST00TS. So, not all the palindromes are covered. I can't figure out where I messed up.
Upvotes: 0
Views: 99
Reputation: 10151
There is a copy and paste errors in the loop-header:
for (p = 0; p <= 9; p++) { // not (p = 0; n <= 9; n++)
Upvotes: 2