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

前端 未结 21 1223
感情败类
感情败类 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:31

    A bit more optimized version with comments included. Notice, there is no need of fast return, just store the max and optimize the cycles to not recalculate j*i if i*j has already been checked.

    function largestPalindrome() {
    
        var max = 0;
    
        // not using i >= 100 since 100*100 is not palindrome! :)
        for (var i = 999; i > 100; i--) {
            // because i * j === j * i, no need of both i and j
            // to count down from 999
            for (var j = i; j > 100; j--) {
                var mul = j * i;
                if (isPalin(mul) && mul > max) {
                    max = i * j;
                }
            }
        }
    
        return max;
    
    }
    
    function isPalin(i) {
    
        // adding empty string to i instead using of .toString
        // avoids unnecessary wrapping in String object on the left side
        i = '' + i;
    
        // don't rely on ==,  use === instead
        return i === i.split("").reverse().join("");
    
    }
    
    console.log(largestPalindrome());
    
    0 讨论(0)
  • 2020-12-28 17:34

    I check it some times with random.randint. In python 3.7.1, you should run it with CMD and after 20 sec you will get the right answer.

    import random
    x,y,z,a,b=100,100,' ','',0
    while 100<=x<=999 and 100<=y<=999:
        a=x*y
        x=random.randint(900,999)
        y=random.randint(900,999)
        print(x,' x ',y,'=')
        z=len(str(a))
        if z==6:
            if str(a)[0] == str(a)[5]:
                if str(a)[1] == str(a)[4]:
                    if str(a)[2] == str(a)[3]:
                        print(a,'yes')
                        exit(a)
        else:
            pass
    #906609
    
    0 讨论(0)
  • 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;
                }
        }
        }
    }
    
    0 讨论(0)
  • 2020-12-28 17:37
    #define MAX(a, b) ((a) > (b) ? (a) : (b))
    int largestPalindrome()
    {
        int ret = 0;
        for (int i = 999; i > 100; --i)
        {
            int jLimit = MAX(ret / i, 100);
            for (int j = i; j > jLimit; --j)
            {
                int ans = i * j;
                if (isPalin(ans))
                {
                    ret = MAX(ans, ret);
                }
            }
        }
    
        return ret;
    }
    

    Reasons explained above.

    We can recompute the range of j when we find a palindrome product.This should be faster.

    0 讨论(0)
  • 2020-12-28 17:37

    Most of the answers here are correct. If you want to save going through 900*900 loops, you can just loop through all palindromes between 10000 and 998001 and find if they are divisible by 3 digit number.

     static void largestpalindromeproduct(){
       int a=999,b=999,c=a*b,loopcounter=0;
       while(c>10000){
           loopcounter++;
           c--;
           if(isPalindrome(c))
               if(isDivisible(c))
               break;
       }
    
       System.out.println(" largest : " + c+ "\nloops:"+ loopcounter);       
    }
    static boolean isDivisible(int n){
        int a=999;
        while(a>=100){
            if(n%a==0){
                if(secondDividerIs3Digit(n,a))
                return true;
            }
            a--;
        }
        return false;
    }
    static boolean secondDividerIs3Digit(int n, int a){
        Integer b=n/a;
        if(b.toString().length()==3)
            return true;
        return false;
    }
    
    static boolean isPalindrome(int n){
        Integer i=new Integer(n);
        String p=i.toString();
        StringBuffer s=new StringBuffer(i.toString());
        s.reverse();
        if(p.equals(s.toString()))
            return true;
        return false;
    }
    
    0 讨论(0)
  • 2020-12-28 17:40

    I have seen a lot of posts for this question, this is the solution that i have come up with:

    • Smallest number that is multiple of two 3 digits number is 10000(100*100)
    • Largest number that is multiple of two 3 digits number is 998001(999*999)

    Our palindrome lies between these two number, write a program to loop through these number and whenever you get a palindrome check whether its perfectly divisible by a 3 digit number and quotient is also a 3 digit number.

    Below is my program in C#, the last number that it prints is our required answer, enjoy.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Collections;
    
    namespace E
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                //Your code goes here
                for(int i=10000;i<=998001;i++)
                {
                    string s1 = i.ToString();
                    char[] array = s1.ToCharArray();
                    Array.Reverse(array);
                    string s2 = new String(array);
                    if(s1==s2)
                    {
    
                        for(int j=100;j<=999;j++)
                        {
                            if(i%j==0 && i/j <= 999)
                            {
                                System.Console.WriteLine(i);        
                                continue;
                            }
                        }
                    }
                }
                System.Console.WriteLine("done");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题