Results 1 to 3 of 3
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Prob 4 ProjectEuler. It does find the correct answer, but I also want the multiples.

    I have probably been looking at this code too long and now I am confused. This is Question 4 with ProjectEuler.

    I am trying to figure out why minFinal and maxFinal are wrong. The palindromic is correct but I was curious as to what the products were.

    Also I do not mind and would very much appreciate if you critique my code and let me know of some annoyances that I can fix.

    Java Code:
    /**
     *Find the largest palindrome made from the product of two 3-digit numbers.
     */
    
    public class Prob4_LargestPal {
    
        public static void main(String[] args) {
    		PalFinder pf = new PalFinder();
    		pf.findLargestPal(100, 999);
        }
    }
    
    class PalFinder {
    
    	String testString;
    	String LargestPal;
    
    	void findLargestPal(int min, int max) {
    
    		int minFinal = 0, maxFinal = 0;
    		int testNum = 0, highest = 0;
    
    		for(int i = max; i >= min; i--) {
    			for(int ii = max; ii >= min; ii--) {
    				testNum = (ii*i);
    				if(isThisPalindromic(testNum)) {
    					if(testNum > highest) {
    						highest = testNum;
    						minFinal = min;
    						maxFinal = max;
    					}
    				}
    			}
    		}
    		System.out.printf("The largest palindromic # is \n%s with the product of \n%s and %s.", highest, minFinal, maxFinal);
    	}
    
    	// Method for determining if a number is palindromic with boolean
    	boolean isThisPalindromic(int test) {
    
    		testString = Integer.toString(test);
    
    		for(int i = 0; i < (testString.length()/2);i++) {
    			if(testString.charAt(i) != testString.charAt(testString.length() - i - 1)) {
    				return false;
    			}
    		}
    		return true;
    	}
    }

  2. #2
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Prob 4 ProjectEuler. It does find the correct answer, but I also want the multipl

    minFinal = min;
    maxFinal = max;

    minFinal + maxFinal will always be min + max.
    you need to use i + ii

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

    Default Re: Prob 4 ProjectEuler. It does find the correct answer, but I also want the multipl

    LOL. I knew I was coding too long. Sheesh. Thanks.

Similar Threads

  1. JOptionPane.showInputDialog with only 1 correct answer
    By Gladiat0r in forum New To Java
    Replies: 1
    Last Post: 03-18-2012, 10:42 PM
  2. Replies: 3
    Last Post: 02-27-2011, 01:03 AM
  3. couldnt find the answer..
    By amaliutz in forum New To Java
    Replies: 12
    Last Post: 02-03-2011, 05:08 PM
  4. why my coding cannot give the correct answer?
    By qema in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 06:52 PM
  5. guss which is correct answer
    By abhinav_jain09 in forum Java Applets
    Replies: 4
    Last Post: 09-19-2008, 03:39 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
  •