📜  最大的回文数,是两个N位数的乘积:第2组

📅  最后修改于: 2021-04-22 06:16:23             🧑  作者: Mango

给定值N ,找出最大回文数,该数是两个N位数字的乘积。
例子:

观察:对于上述问题,可以进行以下观察:
令N = 2,则乘积将包含4位数字。由于乘积将是回文,因此将采用“ abba ”的形式,其中a,b是其各自位置值的数字。
所以,

方法:从以上观察结果可以看出,每个回文产品总是有11因子。

  1. 对于任何N位数字P和Q,如果P和Q的乘积是回文,则P或Q可以被11整除,但不能被两者整除。
  2. 因此,代替检查所有可能的P和Q对的P和Q的乘积是否是回文,我们可以通过仅检查一个数字的11的倍数来减少计算次数。

下面是上述方法的实现:

Java
// Java implementation of the above approach
 
public class GFG {
 
    // Function to check if a number is a
    // Palindrome or not
    static boolean isPalindrome(long x)
    {
        // Taking the string value
        // of the number
        String num = String.valueOf(x);
        boolean result = true;
        int i = 0;
        int j = num.length() - 1;
 
        // Loop to check if every i-th
        // character from beginning is
        // equal to every (N - i)th char
        while (i < j && result) {
            result = num.charAt(i++)
                     == num.charAt(j--);
        }
 
        return result;
    }
 
    // Function to find the largest palindrome
    // which is a product of two N digited numbers
    public static void find(final int nDigits)
    {
 
        // Find lowerBound, upperBound for
        // a given nDigits. for n=2; [10, 99]
        final long lowerBound
            = (long)Math.pow(10, nDigits - 1);
 
        final long upperBound
            = (lowerBound * 10) - 1;
 
        // Result variables
        long resultP = 0, resultQ = 0,
             resultR = 0;
 
        // Keep p decrementing by 11
        for (long p = upperBound;
             p > lowerBound;
             p -= 11) {
 
            // Find the nearest number
            // divisible by 11
            while (p % 11 != 0) {
                p--;
            }
 
            // Keep decrementing q by 1
            for (long q = upperBound;
                 q > lowerBound;
                 q--) {
                long t = p * q;
 
                // Update the result if
                // t > r and is a palindrome
                if (t > resultR
                    && isPalindrome(t)) {
                    resultP = p;
                    resultQ = q;
                    resultR = t;
                    break;
                }
            }
        }
 
        // Printing the final result
        System.out.println(resultR);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 2;
        find(N);
    }
}


Python3
# Python 3 implementation of
# the above approach
 
# Function to check if a
# number is a Palindrome
# or not
def isPalindrome(x):
 
    # Taking the string value
    # of the number
    num = str(x)
    result = True
    i = 0
    j = len(num) - 1
 
    # Loop to check if every i-th
    # character from beginning is
    # equal to every(N - i)th char
    while (i < j and result):
        result = num[i] == num[j]
        i += 1
        j -= 1
 
    return result
 
# Function to find the largest
# palindrome which is a product
# of two N digited numbers
def find(nDigits):
 
    # Find lowerBound, upperBound
    # for a given nDigits. for n = 2
    # [10, 99]
    lowerBound = pow(10, nDigits - 1)
 
    upperBound = (lowerBound * 10) - 1
 
    # Result variables
    resultP = 0
    resultQ = 0
    resultR = 0
 
    # Keep p decrementing by 11
    for p in range(upperBound,
                   lowerBound, -11):
 
        # Find the nearest number
        # divisible by 11
        while (p % 11 != 0):
            p -= 1
 
        # Keep decrementing q by 1
        for q in range(upperBound,
                       lowerBound, -1):
            t = p * q
 
            # Update the result if
            # t > r and is a palindrome
            if (t > resultR and
                isPalindrome(t)):
                resultP = p
                resultQ = q
                resultR = t
                break
 
    # Printing the final result
    print(resultR)
 
# Driver code
if __name__ == "__main__":
 
    N = 2
    find(N)
 
# This code is contributed by Chitranayal


C#
// C# implementation of the above approach
using System;
 
class GFG {
  
    // Function to check if a number is a
    // Palindrome or not
    static bool isPalindrome(long x)
    {
        // Taking the string value
        // of the number
        String num = String.Join("",x);
        bool result = true;
        int i = 0;
        int j = num.Length - 1;
  
        // Loop to check if every i-th
        // character from beginning is
        // equal to every (N - i)th char
        while (i < j && result) {
            result = num[i++]
                     == num[j--];
        }
  
        return result;
    }
  
    // Function to find the largest palindrome
    // which is a product of two N digited numbers
    public static void find(int nDigits)
    {
  
        // Find lowerBound, upperBound for
        // a given nDigits. for n=2; [10, 99]
        long lowerBound
            = (long)Math.Pow(10, nDigits - 1);
  
        long upperBound
            = (lowerBound * 10) - 1;
  
        // Result variables
        long resultP = 0, resultQ = 0,
             resultR = 0;
  
        // Keep p decrementing by 11
        for (long p = upperBound;
             p > lowerBound;
             p -= 11) {
  
            // Find the nearest number
            // divisible by 11
            while (p % 11 != 0) {
                p--;
            }
  
            // Keep decrementing q by 1
            for (long q = upperBound;
                 q > lowerBound;
                 q--) {
                long t = p * q;
  
                // Update the result if
                // t > r and is a palindrome
                if (t > resultR
                    && isPalindrome(t)) {
                    resultP = p;
                    resultQ = q;
                    resultR = t;
                    break;
                }
            }
        }
  
        // Printing the readonly result
        Console.WriteLine(resultR);
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int N = 2;
        find(N);
    }
}
 
// This code is contributed by sapnasingh4991


输出:
9009


相关文章:最大回文集是两个n位数字的乘积