# Thread: Project Euler #4 - Palindromes

1. Member Join Date
Nov 2010
Location
Kentucky, US
Posts
2
Rep Power
0

## Project Euler #4 - Palindromes

Compiles, yet doesn't do what I want. Help?

Java Code:
```/*
* Andrew Koroluk
*/

class Palindrome {
private char[] a1;

public void setArray (char[] array){
a1=array;
}

public boolean test() {
int a = a1;
int b = a1;
int c = a1;
int d = a1;
int e = a1;
int f = a1;
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.
*/```  Reply With Quote

2. ## What is it that you would like it to do, and what is it doing instead?  Reply With Quote

3. Member Join Date
Nov 2010
Location
Kentucky, US
Posts
2
Rep Power
0

## /*
* 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.  Reply With Quote

4. ## 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?  Reply With Quote

5. Senior Member Join Date
Dec 2010
Location
Indiana
Posts
202
Rep Power
10

## 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...
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.

Java Code:
```public class Palindrome {

public static void main(String[] args) {

int trial = (999 * 999);
char[] cNum;
String sNum;

for (int i = trial; i > 10000; i--) {
cNum = Integer.toString(i).toCharArray();
if (cNum == cNum[cNum.length - 1]
& cNum == cNum[cNum.length - 2]
& cNum == cNum[cNum.length - 3]) {
sNum = new String(cNum);
break;
}

}
}

}```  Reply With Quote

6. Senior Member Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11

## Reading input as a String would simplify things  Reply With Quote

7. Senior Member Join Date
Dec 2010
Location
Indiana
Posts
202
Rep Power
10

## 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.  Reply With Quote

8. Senior Member Join Date
Dec 2010
Location
Indiana
Posts
202
Rep Power
10

## Well folks. I ended up solving it.

Java Code:
```public class Palindrome {

public static void main(String[] args) {

char[] cNum;
String sNum;
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 == cNum[cNum.length - 1]
& cNum == cNum[cNum.length - 2]
& cNum == cNum[cNum.length - 3]) {
sNum = new String(cNum);
[COLOR="Red"]test = Integer.parseInt(sNum);
}
}
}
}
}

}```  Reply With Quote

9. Senior Member Join Date
Nov 2010
Posts
210
Rep Power
10

## 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.  Reply With Quote

10. Senior Member Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
11

## For future reference
StringBuilder reverse() ;  Reply With Quote

11. Member Join Date
Jul 2010
Posts
36
Rep Power
0

## 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.  Reply With Quote

12. ##  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  Reply With Quote

13. ##  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:

Java 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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•