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

前端 未结 21 1435
感情败类
感情败类 2020-12-28 17:16

Can anyone tell me what\'s wrong with the code. Find the largest palindrome made from the product of two 3-digit numbers.

function largestPalind         


        
21条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-28 17:35

    This is better because its using O(N) time complexity to find all the palindrome (As calculating palindrome of a six digit no is constant) and O(N2) nearly to find the actual palindrome that too worst case the moment its finding its first no we don't have to do any more calculation and here we are actually using the worst case on possible palindromic no. So I think its better

    package ProjectEuler;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    
    public class Largest_Palindrome_Product {
    
        public static void main(String[] args) {
            int count=0;
            for(int i=10000;i<998002;i++) {
                int x=i,c=0;
                while(x!=0) {
                    c=c*10+x%10;
                    x/=10;
                }
                if(c==i) {
                count++;
            }
            }
            int a[]=new int[count],count1=0;
    
        for(int i=10000;i<998002;i++) {
            int x=i,c=0;
            while(x!=0) {
                c=c*10+x%10;
                x/=10;
            }
            if(c==i) {
                a[count1]=i;
                count1++;
        }
        }
        Arrays.sort(a);
        tp:for(int i=count-1;i>=0;i--)
        {
            for(int j=999;j>100;j--)
                if(a[i]%j==0&&a[i]/j<=999) {
            System.out.println(a[i]+" "+j+" "+a[i]/j);
            break tp;
                }
        }
        }
    }
    

提交回复
热议问题