Open In App

Sum of all N-digit palindromic numbers which doesn’t contains 0 and are divisible by 9

Given a number N, the task is to find the sum of all N-digit palindromic numbers which are divisible by 9 and the number doesn’t contain 0 in it.
Note: The sum can be very large, take modulo with 109+7.

Example: 



Input: N = 2 
Output: 99 
Explanation: 
There is only one 2-digit palindromic number divisible by 9 is 99.

Input: N = 3 
Output: 4995  



 

Naive Approach: The idea is to iterate over all the N-digit numbers and check whether the numbers are palindromic and divisible by 9 and doesn’t contain zero in it. If yes then the summation of all the numbers given the required result.
Efficient Approach: Below are the some observation for this problem statement: 

  1. If N is odd, then the number can be of the form “ab..x..ba” and if N is even then the number can be “ab..xx..ba”, where ‘a’, ‘b’ and ‘x’ can be replaced by digit from 1 to 9.
  2. If number “ab..x..ba” divisible by 9, (2*(a+b) + x) must be divisible by 9.
  3. For every possible pair of (a, b) there always exist one such ‘x’ so that the number is divisible by 9.
  4. Then the count of N-digit palindromic numbers which are divisible by 9 can be calculated from below formula: 
Since we have 9 digits to fill at the position a and b.
Therefore, total possible combinations will be:
if N is Odd then, count = pow(9, N/2)
if N is Even then, count = pow(9, N/2 - 1)
as N/2 digits are repeated at the end to get the 
number palindromic.

Proof of Uniqueness of ‘x’: 

sum = 2*(a+b) + x;
sum    one of the possible value of x    sum + x
 2                   7                      9
 4                   5                      9
 6                   3                      9
 8                   1                      9
 .                   .                      . 
 .                   .                      . 
 .                   .                      . 
 36                  9                      45

Below are the steps: 

sum at kth position = 45*(number of combinations)/9

Below is the implementation of the above approach: 
 




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
long long int MOD = 1000000007;
 
// Function to find a^b efficiently
long long int power(long long int a,
                    long long int b)
{
 
    // Base Case
    if (b == 0)
        return 1;
 
    // To store the value of a^b
    long long int temp = power(a, b / 2);
 
    temp = (temp * temp) % MOD;
 
    // Multiply temp by a until b is not 0
    if (b % 2 != 0) {
        temp = (temp * a) % MOD;
    }
 
    // Return the final ans a^b
    return temp;
}
 
// Function to find sum of all N-digit
// palindromic number divisible by 9
void palindromicSum(int N)
{
 
    long long int sum = 0, res, ways;
 
    // Base Case
    if (N == 1) {
        cout << "9" << endl;
        return;
    }
 
    if (N == 2) {
        cout << "99" << endl;
        return;
    }
 
    ways = N / 2;
 
    // If N is even, decrease ways by 1
    if (N % 2 == 0)
        ways--;
 
    // Find the total number of ways
 
    res = power(9, ways - 1);
 
    // Iterate over [1, N] and find the
    // sum at each index
    for (int i = 0; i < N; i++) {
        sum = sum * 10 + 45 * res;
        sum %= MOD;
    }
 
    // Print the final Sum
    cout << sum << endl;
}
 
// Driver Code
int main()
{
    int N = 3;
    palindromicSum(N);
    return 0;
}




// Java program for the above approach
class GFG{
 
static int MOD = 1000000007;
 
// Function to find a^b efficiently
static int power(int a, int b)
{
 
    // Base Case
    if (b == 0)
        return 1;
 
    // To store the value of a^b
    int temp = power(a, b / 2);
    temp = (temp * temp) % MOD;
 
    // Multiply temp by a until b is not 0
    if (b % 2 != 0)
    {
        temp = (temp * a) % MOD;
    }
 
    // Return the final ans a^b
    return temp;
}
 
// Function to find sum of all N-digit
// palindromic number divisible by 9
static void palindromicSum(int N)
{
    int sum = 0, res, ways;
 
    // Base Case
    if (N == 1)
    {
        System.out.print("9" + "\n");
        return;
    }
 
    if (N == 2)
    {
        System.out.print("99" + "\n");
        return;
    }
    ways = N / 2;
 
    // If N is even, decrease ways by 1
    if (N % 2 == 0)
        ways--;
 
    // Find the total number of ways
    res = power(9, ways - 1);
 
    // Iterate over [1, N] and find the
    // sum at each index
    for (int i = 0; i < N; i++)
    {
        sum = sum * 10 + 45 * res;
        sum %= MOD;
    }
 
    // Print the final Sum
    System.out.print(sum + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 3;
    palindromicSum(N);
}
}
 
// This code is contributed by Amit Katiyar




# Python 3 program for the above approach
MOD = 1000000007
 
# Function to find a^b efficiently
def power(a, b):
 
    # Base Case
    if (b == 0):
        return 1
 
    # To store the value of a^b
    temp = power(a, b // 2)
 
    temp = (temp * temp) % MOD
 
    # Multiply temp by a until b is not 0
    if (b % 2 != 0):
        temp = (temp * a) % MOD
 
    # Return the final ans a^b
    return temp
 
# Function to find sum of all N-digit
# palindromic number divisible by 9
def palindromicSum(N):
 
    sum = 0
 
    # Base Case
    if (N == 1):
        print("9")
        return
 
    if (N == 2):
        print("99")
        return
 
    ways = N // 2
 
    # If N is even, decrease ways by 1
    if (N % 2 == 0):
        ways -= 1
 
    # Find the total number of ways
    res = power(9, ways - 1)
 
    # Iterate over [1, N] and find the
    # sum at each index
    for i in range(N):
        sum = sum * 10 + 45 * res
        sum %= MOD
 
    # Print the final Sum
    print(sum)
 
# Driver Code
if __name__ == "__main__":
 
    N = 3
    palindromicSum(N)
 
# This code is contributed by chitranayal




// C# program for the above approach
using System;
 
class GFG{
 
static int MOD = 1000000007;
 
// Function to find a^b efficiently
static int power(int a, int b)
{
 
    // Base Case
    if (b == 0)
        return 1;
 
    // To store the value of a^b
    int temp = power(a, b / 2);
    temp = (temp * temp) % MOD;
 
    // Multiply temp by a until b is not 0
    if (b % 2 != 0)
    {
        temp = (temp * a) % MOD;
    }
 
    // Return the readonly ans a^b
    return temp;
}
 
// Function to find sum of all N-digit
// palindromic number divisible by 9
static void palindromicSum(int N)
{
    int sum = 0, res, ways;
 
    // Base Case
    if (N == 1)
    {
        Console.Write("9" + "\n");
        return;
    }
 
    if (N == 2)
    {
        Console.Write("99" + "\n");
        return;
    }
    ways = N / 2;
 
    // If N is even, decrease ways by 1
    if (N % 2 == 0)
        ways--;
 
    // Find the total number of ways
    res = power(9, ways - 1);
 
    // Iterate over [1, N] and find the
    // sum at each index
    for(int i = 0; i < N; i++)
    {
       sum = sum * 10 + 45 * res;
       sum %= MOD;
    }
 
    // Print the readonly sum
    Console.Write(sum + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 3;
     
    palindromicSum(N);
}
}
 
// This code is contributed by AbhiThakur




<script>
 
// Javascript program for the above approach
 
let MOD = 1000000007;
  
// Function to find a^b efficiently
function power(a, b)
{
  
    // Base Case
    if (b == 0)
        return 1;
  
    // To store the value of a^b
    let temp = power(a, b / 2);
    temp = (temp * temp) % MOD;
  
    // Multiply temp by a until b is not 0
    if (b % 2 != 0)
    {
        temp = (temp * a) % MOD;
    }
  
    // Return the final ans a^b
    return temp;
}
  
// Function to find sum of all N-digit
// palindromic number divisible by 9
function palindromicSum(N)
{
    let sum = 0, res, ways;
  
    // Base Case
    if (N == 1)
    {
        document.write("9" + "\n");
        return;
    }
  
    if (N == 2)
    {
        document.write("99" + "\n");
        return;
    }
    ways = Math.floor(N / 2);
  
    // If N is even, decrease ways by 1
    if (N % 2 == 0)
        ways--;
  
    // Find the total number of ways
    res = power(9, ways - 1);
  
    // Iterate over [1, N] and find the
    // sum at each index
    for (let i = 0; i < N; i++)
    {
        sum = sum * 10 + 45 * res;
        sum %= MOD;
    }
  
    // Print the final Sum
    document.write(sum + "<br/>");
}
 
// Driver Code
     
    let N = 3;
    palindromicSum(N);
         
</script>

Output: 
4995

 

Time Complexity: O(N), where N is the given number.
Auxiliary Space: O(1)


Article Tags :