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

前端 未结 21 1441
感情败类
感情败类 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: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");
            }
        }
    }
    

提交回复
热议问题