The Wayback Machine - https://web.archive.org/web/20210720113836/https://www.geeksforgeeks.org/count-n-digit-numbers-such-that-every-position-is-divisible-by-the-digit-at-that-position/
Skip to content
Related Articles

Related Articles

Improve Article

Count N-digit numbers such that every position is divisible by the digit at that position

  • Last Updated : 20 Jul, 2021

Given a positive integer N, the task is to count the number of N-digit numbers such that every index (1-based indexing) in the number is divisible by the digit occurring at that index. Since the court can be very large, print it modulo 109 + 7.

Examples:

Input: N = 2
Output: 2
Explanation: The numbers 11 and 12 are the only 2-digit numbers satisfying the condition.

Input: N = 5
Output: 24

Naive Approach: The simplest approach to solve the problem is to generate all possible N-digit numbers and for each such number, check if all its digits satisfy the required condition or not.
Time Complexity: O(10N*N)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to observe the fact that for every position, there are 9 possible options from 1 to 9. Check for every possible option and find the result. 
Follow the steps below to solve this problem:



  • Initialize the variable ans as 1 to store the answer.
  • Iterate over the range [1, N] using the variable index and perform the following tasks:
    • Initialize the variable, say choices as 0, to store the number of options for that particular index.
    • Iterate over the range [1, 9] using a variable digit and perform the following tasks:
      • If index%digit is equal to 0, then increase the value of choices by 1.
    • Set the value of ans as (ans*1LL*choices)%mod.
  • After performing the above steps, print the value of ans as the answer.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
const int mod = 1e9 + 7;
  
// Function to count N-digit numbers
// such that each position is divisible
// by the digit occurring at that position
void countOfNumbers(int N)
{
    // Stores the answer.
    int ans = 1;
  
    // Iterate from indices 1 to N
    for (int index = 1; index <= N; ++index) {
  
        // Stores count of digits that can
        // be placed at the current index
        int choices = 0;
  
        // Iterate from digit 1 to 9
        for (int digit = 1; digit <= 9; ++digit) {
  
            // If index is divisible by digit
            if (index % digit == 0) {
                ++choices;
            }
        }
  
        // Multiply answer with possible choices
        ans = (ans * 1LL * choices) % mod;
    }
  
    cout << ans << endl;
}
  
// Driver Code
int main()
{
    // Given Input
    int N = 5;
  
    // Function call
    countOfNumbers(N);
  
    return 0;
}
Output:
24

Time Complexity: O(10 * N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :