2 digit solution question


  • 0
    J

    Ok so it says that the max palindrome for a 2 digit number is 9991=9009 and 9009%1337=987
    However, 44
    91 = 4004 and 4004% 1337 = 1330 which is larger than 987
    is it because that is an error on the program output or im not understanding the problem?
    any explanations will be very helpful
    also here is my code (Not as good as some other people but it is understandable)

    	public int findPalindrome(int digit) {
    		int largest = 0;
    		int smallest = (int) Math.pow(10.0, (digit - 1));
    		int max = (smallest * 10) - 1;
    
    		for (int i = max; i >= smallest; i--) {
    			for (int x = max; x >= smallest; x--) {
    				try {
    					if (isPalindrome(i * x)) {
    						if (((i * x) % 1337) > largest) {
    							largest = (i * x) % 1337;
    							System.out.println("X: " + x + " i: " + i);
    						} else
    							continue;
    					} else
    						continue;
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    		return largest;
    	}
    
    	public boolean isPalindrome(int product) throws InterruptedException {
    		int first = 0;
    		String palindrome = "" + product;
    		int last = palindrome.length() - 1;
    		for (first = 0; first <= (palindrome.length() - 1) / 2; first++, last--)
    			if (palindrome.charAt(first) != palindrome.charAt(last))
    				return false;
    		return true;
    	}
    

    Thank you


  • 0
    J

    its 44X91 = 4004 and 4004 % 1337 = 1330
    and
    99X91 = 9009 and 9009 % 1337 = 987


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.