# Project Euler #4 - Palindromes

• 11-23-2010, 10:51 PM
Awk34
Project Euler #4 - Palindromes
Compiles, yet doesn't do what I want. Help?

Code:

```/*  * Andrew Koroluk  */ class Palindrome {         private char[] a1;         public void setArray (char[] array){                 a1=array;         }         public boolean test() {                 int a = a1[0];                 int b = a1[1];                 int c = a1[2];                 int d = a1[3];                 int e = a1[4];                 int f = a1[5];                 if (a==f && b==e && c==d) {return true;}                 else {return false;}         } } public class euler4{         public static void main (String[] args){                 for(int i=998001; i>=10000;i--) {                         String n = Integer.toString(i);                         char[] array = new char[] {' ', ' ', ' ', ' ', ' ', ' '};                         n.getChars(0, 5, array, 1);                         Palindrome p1 = new Palindrome();                         p1.setArray(array);                         if (p1.test()) {                                 System.out.println(i);                         }                 }         } } /*  * 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.  */```
• 11-23-2010, 11:42 PM
Zack
What is it that you would like it to do, and what is it doing instead?
• 11-24-2010, 01:34 AM
Awk34
/*
* 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.
*/

last part of the code. run it for yourself and see what I mean.
• 11-24-2010, 02:55 AM
Zack
Why don't you just run two loops starting at 999 and work towards 100, then check if the product of those two works in your .test() function?
• 12-16-2010, 08:07 AM
AcousticBruce
I am also doing the same problem. My code seems right to me (meaning I just do not see it).

I am hoping someone can help "nudge" me without giving too much away.

Here is my project...
Quote:

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.

My code returns the answer 997799. But Project Euler says it is wrong.

Code:

```public class Palindrome {     public static void main(String[] args) {         int trial = (999 * 999);         char[] cNum;         String sNum;         int answer = 0;         for (int i = trial; i > 10000; i--) {             cNum = Integer.toString(i).toCharArray();             if (cNum[0] == cNum[cNum.length - 1]                     & cNum[1] == cNum[cNum.length - 2]                     & cNum[2] == cNum[cNum.length - 3]) {                 sNum = new String(cNum);                 answer = Integer.parseInt(sNum);                 break;             }         }         System.out.println(answer);     } }```
• 12-16-2010, 08:18 AM
al_Marshy_1981
Reading input as a String would simplify things
• 12-16-2010, 08:50 AM
AcousticBruce
I would think indexing strings would be just as much as complex or even more.
Perhaps I do not understand your theory?

I do know my Palindromes char machine does work-at least at finding palindromes. Just not the highest one.
• 12-16-2010, 08:57 AM
AcousticBruce
Well folks. I ended up solving it.

Code:

```public class Palindrome {     public static void main(String[] args) {         char[] cNum;         String sNum;         int answer = 0;         int test;       [COLOR="Red"]  for (int x = 100; x < 1000; x++) {             for (int y = 100; y < 1000; y++) {                 cNum = Integer.toString(x * y).toCharArray();[/COLOR]                 if (cNum[0] == cNum[cNum.length - 1]                         & cNum[1] == cNum[cNum.length - 2]                         & cNum[2] == cNum[cNum.length - 3]) {                     sNum = new String(cNum);                     [COLOR="Red"]test = Integer.parseInt(sNum);                     if (test > answer) {                         answer = test;[/COLOR]                     }                 }             }         }         System.out.println(answer);     } }```
• 12-17-2010, 12:08 AM
Iron Lion
It's worth keeping in mind that, although & works for logical AND, you're almost always better off using &&, which doesn't bother to evaluate the right-hand side if the left-hand side is false, and therefore speeds things up.
• 12-19-2010, 03:27 AM
al_Marshy_1981
For future reference
StringBuilder reverse() ;
• 12-19-2010, 08:12 AM
hei1233212000
I think it is better to start at 999*999 rather than 100*100 as you are going to find out the greatest palindrome.
And, after you got a palindrome, you have also to check if both factors of the palindrome are in range 100 to 999.
• 12-19-2010, 11:22 AM
JosAH
Quote:

Originally Posted by AcousticBruce
Well folks. I ended up solving it.

I would've done it the other way around: I'd start with a number 999*999 (the largest possible number being the product of two 3 digit numbers) and see if it's a palindrome. If it isn't decrement the number and see if it's a palindrome; if it is so check if it is the product of two 3 digit numbers. At the first hit I've found the largest number.

kind regards,

Jos
• 12-19-2010, 11:28 AM
JosAH
Quote:

Originally Posted by JosAH
I would've done it the other way around: I'd start with a number 999*999 (the largest possible number being the product of two 3 digit numbers) and see if it's a palindrome. If it isn't decrement the number and see if it's a palindrome; if it is so check if it is the product of two 3 digit numbers. At the first hit I've found the largest number.

kind regards,

Jos

edit: I just implemented it:

Code:

```public class T {         public static void main(String args[]) {                 for (int x= 999*999; x >= 100*100; x--) {                         StringBuffer sb= new StringBuffer(""+x);                         String sx= sb.toString();                         if (sx.equals(sb.reverse().toString()))                                 for (int d= 100; d*d <= x; d++)                                         if (x%d == 0 && x/d > 99 && x/d < 1000) {                                                 System.out.println(x+"= "+d+"*"+(x/d));                                                 System.exit(0);                                         }                 }         } }```
The result is: 906609= 913*993