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

前端 未结 17 979
天涯浪人
天涯浪人 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 Pin
    {
        public static boolean isPalin(int num)
        {
            char[] val = (""+num).toCharArray();
            for(int i=0;i<val.length;i++)
            {
                if(val[i] != val[val.length - i - 1])
                {
                    return false;
                }
            }
            return true;
        }
    
        public static void main(String[] args)
        {
            for(int i=999;i>100;i--)
                for(int j=999;j>100;j--)
                {
                    int mul = j*i;
                    if(isPalin(mul))
                    {
                        System.out.printf("%d * %d = %d",i,j,mul);
                        return;
                    }
                }
        }
    }
    
    0 讨论(0)
  • 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);
    }
    }
    
    0 讨论(0)
  • 2021-02-02 05:22

    I tried the solution by Tobin joy and vickyhacks and both of them produce the result 580085 which is wrong here is my solution, though very clumsy:

    import java.util.*;
    class ProjEu4
    {
    
    public static void main(String [] args) throws Exception
    {
        int n=997;
        ArrayList<Integer> al=new ArrayList<Integer>();
        outerloop:
        while(n>100){
        int k=reverse(n);
        int fin=n*1000+k;
                al=findfactors(fin);
        if(al.size()>=2)
            {
                for(int i=0;i<al.size();i++)
                {
                    if(al.contains(fin/al.get(i))){
                        System.out.println(fin+" factors are:"+al.get(i)+","+fin/al.get(i));
                        break outerloop;}
                }
    
            }
            n--;
        }
    }
    private static ArrayList<Integer> findfactors(int fin)
    {
        ArrayList<Integer> al=new ArrayList<Integer>();
        for(int i=100;i<=999;i++)
        {
            if(fin%i==0)
                al.add(i);
        }
        return al;
    }
    private static int reverse(int number)
    {
        int reverse = 0;
        while(number != 0){
            reverse = (reverse*10)+(number%10);
            number = number/10;
        }
        return reverse;
    }
    }
    
    0 讨论(0)
  • 2021-02-02 05:24

    Most probably it is replication of one of the other solution but it looks simple owing to pythonified code ,even it is a bit brute-force.

    def largest_palindrome():
        largest_palindrome = 0;
        for i in reversed(range(1,1000,1)):
            for j in reversed(range(1, i+1, 1)):
                num = i*j
                if check_palindrome(str(num)) and  num > largest_palindrome :
                    largest_palindrome = num 
        print "largest palindrome ", largest_palindrome
    
    def check_palindrome(term):
        rev_term = term[::-1]
        return rev_term == term
    
    0 讨论(0)
  • 2021-02-02 05:26

    You can try the following which prints

    999 * 979 * 989 = 967262769
    largest palindrome= 967262769 took 0.015
    

    public static void main(String... args) throws IOException, ParseException {
      long start = System.nanoTime();
      int largestPalindrome = 0;
      for (int i = 999; i > 100; i--) {
        LOOP:
        for (int j = i; j > 100; j--) {
          for (int k = j; k > 100; k++) {
            int n = i * j * k;
            if (n < largestPalindrome) continue LOOP;
            if (isPalindrome(n)) {
              System.out.println(i + " * " + j + " * " + k + " = " + n);
              largestPalindrome = n;
            }
          }
        }
      }
      long time = System.nanoTime() - start;
      System.out.printf("largest palindrome= %d took %.3f seconds%n", largestPalindrome, time / 1e9);
    }
    
    private static boolean isPalindrome(int n) {
      if (n >= 100 * 1000 * 1000) {
        // 9 digits
        return n % 10 == n / (100 * 1000 * 1000)
            && (n / 10 % 10) == (n / (10 * 1000 * 1000) % 10)
            && (n / 100 % 10) == (n / (1000 * 1000) % 10)
            && (n / 1000 % 10) == (n / (100 * 1000) % 10);
      } else if (n >= 10 * 1000 * 1000) {
        // 8 digits
        return n % 10 == n / (10 * 1000 * 1000)
            && (n / 10 % 10) == (n / (1000 * 1000) % 10)
            && (n / 100 % 10) == (n / (100 * 1000) % 10)
            && (n / 1000 % 10) == (n / (10 * 1000) % 10);
      } else if (n >= 1000 * 1000) {
        // 7 digits
        return n % 10 == n / (1000 * 1000)
            && (n / 10 % 10) == (n / (100 * 1000) % 10)
            && (n / 100 % 10) == (n / (10 * 1000) % 10);
      } else throw new AssertionError();
    }
    
    0 讨论(0)
提交回复
热议问题