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

前端 未结 17 990
天涯浪人
天涯浪人 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:18

    You can actually do it with Python, it's easy just take a look:

    actualProduct = 0
    highestPalindrome = 0
    
    # Setting the numbers. In case it's two digit 10 and 99, in case is three digit 100 and 999, etc.
    num1 = 100
    num2 = 999
    
    def isPalindrome(number):
            number = str(number)
            reversed = number[::-1]
            if number==reversed:
                    return True
            else:
                    return False
    
    a = 0
    b = 0
    
    for i in range(num1,num2+1):
            for j in range(num1,num2+1):
                    actualProduct = i * j
                    if (isPalindrome(actualProduct) and (highestPalindrome < actualProduct)):
                            highestPalindrome = actualProduct
                            a = i
                            b = j
    
    
    print "Largest palindrome made from the product of two %d-digit numbers is [ %d ] made of %d * %d" % (len(str(num1)), highestPalindrome, a, b)
    

提交回复
热议问题