Results 1 to 13 of 13
Thread: Project Euler #4  Palindromes
 11232010, 10:51 PM #1Member
 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[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 2digit numbers is 9009 = 91 99. * * Find the largest palindrome made from the product of two 3digit numbers. */
 11232010, 11:42 PM #2
What is it that you would like it to do, and what is it doing instead?
 11242010, 01:34 AM #3Member
 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 2digit numbers is 9009 = 91 99.
*
* Find the largest palindrome made from the product of two 3digit numbers.
*/
last part of the code. run it for yourself and see what I mean.
 11242010, 02:55 AM #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?
 12162010, 08:07 AM #5Senior Member
 Join Date
 Dec 2010
 Location
 Indiana
 Posts
 202
 Rep Power
 8
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 2digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3digit 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; 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); } }
 12162010, 08:18 AM #6Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 9
Reading input as a String would simplify things
 12162010, 08:50 AM #7Senior Member
 Join Date
 Dec 2010
 Location
 Indiana
 Posts
 202
 Rep Power
 8
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 workat least at finding palindromes. Just not the highest one.
 12162010, 08:57 AM #8Senior Member
 Join Date
 Dec 2010
 Location
 Indiana
 Posts
 202
 Rep Power
 8
Well folks. I ended up solving it.
Java 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); } }
 12172010, 12:08 AM #9Senior Member
 Join Date
 Nov 2010
 Posts
 210
 Rep Power
 8
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 righthand side if the lefthand side is false, and therefore speeds things up.
 12192010, 03:27 AM #10Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 9
For future reference
StringBuilder reverse() ;
 12192010, 08:12 AM #11Member
 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.
 12192010, 11:22 AM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,421
 Blog Entries
 7
 Rep Power
 26
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 12192010, 11:28 AM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,421
 Blog Entries
 7
 Rep Power
 26
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); } } } }
Build a wall around Donald Trump; I'll pay for it.
Similar Threads

Finding out number of Palindromes in a Sentence
By rameshiit19 in forum New To JavaReplies: 1Last Post: 09132010, 04:44 AM 
Adding a project to an existing project
By Seijuro in forum NetBeansReplies: 4Last Post: 08082010, 11:15 AM 
Find All Palindromes
By spleenlol in forum New To JavaReplies: 6Last Post: 02042010, 03:33 AM 
[SOLVED] project euler #19
By matzahboy in forum New To JavaReplies: 14Last Post: 12142008, 07:53 AM
Bookmarks