Results 1 to 13 of 13
  1. #1
    Awk34 is offline Member
    Join Date
    Nov 2010
    Location
    Kentucky, US
    Posts
    2
    Rep Power
    0

    Default 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 2-digit numbers is 9009 = 91  99.
     *
     * Find the largest palindrome made from the product of two 3-digit numbers.
     */

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    10

    Default

    What is it that you would like it to do, and what is it doing instead?

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

    Default

    /*
    * 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.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    10

    Default

    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?

  5. #5
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    10

    Default

    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;
            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);
        }
    
    }

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

    Default

    Reading input as a String would simplify things

  7. #7
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    10

    Default

    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.

  8. #8
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    10

    Default

    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);
        }
    
    }

  9. #9
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    10

    Default

    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.

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

    Default

    For future reference
    StringBuilder reverse() ;

  11. #11
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default

    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. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default

    Quote Originally Posted by AcousticBruce View Post
    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
    Build a wall around Donald Trump; I'll pay for it.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default

    Quote Originally Posted by JosAH View Post
    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
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Finding out number of Palindromes in a Sentence
    By rameshiit19 in forum New To Java
    Replies: 1
    Last Post: 09-13-2010, 03:44 AM
  2. Adding a project to an existing project
    By Seijuro in forum NetBeans
    Replies: 4
    Last Post: 08-08-2010, 10:15 AM
  3. Find All Palindromes
    By spleenlol in forum New To Java
    Replies: 6
    Last Post: 02-04-2010, 02:33 AM
  4. [SOLVED] project euler #19
    By matzahboy in forum New To Java
    Replies: 14
    Last Post: 12-14-2008, 06:53 AM

Posting Permissions

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