📌  相关文章
📜  用数字1到9构成的所有N个数字回文数的总和可被9整除

📅  最后修改于: 2021-06-26 11:13:41             🧑  作者: Mango

给定数字N ,任务是找到可被9整除的所有N个数字回文数字(由1到9的数字组成)的总和。

例子:

方法:该问题的主要观察结果是,如果一个数字可以被9整除,那么该数字的数字总和也可以被9整除。另一个观察结果是,如果我们使用来自的数字来计算N位数的回文数。 1到9,那么可以观察到

所以,

  1. 首先找到可以被9整除的N位回文数的计数,如下所示:
    \text{Count of N-digit Palindromic numbers divisible by 9} = \begin{cases} 9^{\frac{N-1}{2}} & \text{ if N is odd} \\ 9^{\frac{N-2}{2}} & \text{ if N is even} \end{cases}
  2. 然后,如果N为1或2,则总和将分别为9和99,因为它们是唯一的1位和2位数字。
  3. 如果N> 2,则第N个位数回文数的总和可被9整除为
    \text{Sum of Nth digit palindromic numbers divisible by 9 }= (\text{sum of }(N-1)^{th}\text{ digit } * 10) + (5*\text{ count of N digit palindromic numbers divisible by 9})

    下面是上述方法的实现:

    C++
    // C++ implementation to find the sum
    // of all the N digit palindromic
    // numbers divisible by 9
      
    #include 
      
    using namespace std;
      
    // Function for finding count of
    // N digits palindrome which
    // are divisible by 9
    int countPalindrome(int n)
    {
        int count;
      
        // if N is odd
        if (n % 2 == 1) {
            count = pow(9, (n - 1) / 2);
        }
        // if N is even
        else {
            count = pow(9, (n - 2) / 2);
        }
        return count;
    }
      
    // Function for finding sum of N
    // digits palindrome which are
    // divisible by 9
    int sumPalindrome(int n)
    {
        // count the possible
        // number of palindrome
        int count = countPalindrome(n);
      
        int res = 0;
      
        if (n == 1)
            return 9;
        if (n == 2)
            return 99;
      
        for (int i = 0; i < n; i++) {
            res = res * 10 + count * 5;
        }
      
        return res;
    }
      
    // Driver Code
    int main()
    {
        int n = 3;
        cout << sumPalindrome(n);
        return 0;
    }


    Java
    // Java implementation to find the sum
    // of all the N digit palindromic
    // numbers divisible by 9
    import java.util.*;
      
    class GFG{
          
    // Function for finding count of
    // N digits palindrome which
    // are divisible by 9
    static int countPalindrome(int n)
    {
        int count;
      
        // If N is odd
        if (n % 2 == 1)
        {
            count = (int)Math.pow(9, (n - 1) / 2);
        }
          
        // If N is even
        else
        {
            count = (int)Math.pow(9, (n - 2) / 2);
        }
        return count;
    }
      
    // Function for finding sum of N
    // digits palindrome which are
    // divisible by 9
    static int sumPalindrome(int n)
    {
          
        // Count the possible
        // number of palindrome
        int count = countPalindrome(n);
      
        int res = 0;
      
        if (n == 1)
            return 9;
        if (n == 2)
            return 99;
      
        for(int i = 0; i < n; i++)
        {
           res = res * 10 + count * 5;
        }
      
        return res;
    }
      
    // Driver Code
    public static void main(String[] args)
    {
        int n = 3;
          
        System.out.println(sumPalindrome(n));
    }
    }
      
    // This code is contributed by ANKITKUMAR34


    Python3
    # Python3 implementation to find the 
    # sum of all the N digit palindromic
    # numbers divisible by 9
      
    # Function for finding count of
    # N digits palindrome which
    # are divisible by 9
    def countPalindrome(n):
      
        count = 0
          
        # If N is odd
        if (n % 2 == 1):
            count = pow(9, (n - 1) // 2)
              
        # If N is even
        else:
            count = pow(9, (n - 2) // 2)
              
        return count
      
    # Function for finding sum of N
    # digits palindrome which are
    # divisible by 9
    def sumPalindrome(n):
      
        # Count the possible
        # number of palindrome
        count = countPalindrome(n)
      
        res = 0
      
        if (n == 1):
            return 9
        if (n == 2):
            return 99
      
        for i in range(n):
            res = res * 10 + count * 5
      
        return res
      
    # Driver Code
    n = 3
      
    print(sumPalindrome(n))
      
    # This code is contributed by ANKITKUMAR34


    C#
    // C# implementation to find the sum
    // of all the N digit palindromic
    // numbers divisible by 9
    using System;
      
    class GFG{
           
    // Function for finding count of
    // N digits palindrome which
    // are divisible by 9
    static int countPalindrome(int n)
    {
        int count;
       
        // If N is odd
        if (n % 2 == 1)
        {
            count = (int)Math.Pow(9, (n - 1) / 2);
        }
           
        // If N is even
        else
        {
            count = (int)Math.Pow(9, (n - 2) / 2);
        }
        return count;
    }
       
    // Function for finding sum of N
    // digits palindrome which are
    // divisible by 9
    static int sumPalindrome(int n)
    {
           
        // Count the possible
        // number of palindrome
        int count = countPalindrome(n);
       
        int res = 0;
       
        if (n == 1)
            return 9;
        if (n == 2)
            return 99;
       
        for(int i = 0; i < n; i++)
        {
           res = res * 10 + count * 5;
        }
       
        return res;
    }
       
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 3;
           
        Console.WriteLine(sumPalindrome(n));
    }
    }
      
    // This code is contributed by Amit Katiyar


    输出:
    4995