Find the largest palindrome made from the product of two 3-digit numbers

前端 未结 17 1224
天涯浪人
天涯浪人 2021-02-02 04:27
package testing.project;

public class PalindromeThreeDigits {

    public static void main(String[] args) {
        int value = 0;
        for(int i = 100;i <=999;i+         


        
17条回答
  •  不要未来只要你来
    2021-02-02 05:20

    public class ProjectEuler4 {
    
    public static void main(String[] args) {
    
        int x = 999; // largest 3-digit number
        int largestProduct = 0;
    
        for(int y=x; y>99; y--){
            int product = x*y;
    
            if(isPalindormic(x*y)){
                if(product>largestProduct){
                    largestProduct = product;
                    System.out.println("3-digit numbers product palindormic number : " + x + " * " + y + " : " + product);
                }
            }
    
            if(y==100 || product < largestProduct){y=x;x--;}
        }
    
    
    }
    
    public static boolean isPalindormic(int n){
    
        int palindormic = n;
        int reverse = 0;
    
        while(n>9){
            reverse = (reverse*10) + n%10;
            n=n/10;
        }
        reverse = (reverse*10) + n;     
        return (reverse == palindormic);
    }
    }
    

提交回复
热议问题