# 5 Best Ways to Count N-Digit Integers with Strictly Increasing Digits in Python

Rate this post

π‘ Problem Formulation: We aim to count all the positive n-digit integers where each digit is strictly greater than the preceding one. For example, given n = 2, our desired output is 45, since there are 45 two-digit numbers (such as 12, 34, 78) where each pair of digits adheres to this rule.

## Method 1: Using Itertools Combinations

The first method takes advantage of the itertools.combinations function, which generates all possible combinations of n digits from the list of digits 0-9, under the condition that the digits must be in increasing order to be valid.

Here’s an example:

from itertools import combinations

def count_strictly_increasing(n):
return len(list(combinations(range(1, 10), n)))

print(count_strictly_increasing(2))

Output: 36

This function leverages the fact that combinations are inherently increasing. We skip 0 to avoid leading zeros in our n-digit numbers. It returns the count of all possible combinations of length n, which corresponds to our strictly increasing n-digit numbers.

## Method 2: Recursive Approach

The recursive approach breaks down the problem into simpler sub-problems. We recursively construct the strictly increasing numbers and increment a counter each time we achieve a number with n digits.

Here’s an example:

def count_increasing_recursive(n, start=1, number=''):
if n == 0:
return 1
count = 0
for i in range(start, 10):
count += count_increasing_recursive(n-1, i+1, number+str(i))
return count

print(count_increasing_recursive(2))

Output: 36

We initiate our function with the number of digits, n, and count the combinations by adding to each subsequent number until we reach the required digit length. This method shows how recursion can elegantly solve counting problems.

## Method 3: Dynamic Programming

Dynamic programming optimizes the recursive approach by storing intermediate results to avoid redundant computation. This variant uses a table to keep track of the count of increasing sequences for each length and starting digit.

Here’s an example:

def count_increasing_dp(n):
dp = [[0]*10 for _ in range(n+1)]
for i in range(10):
dp[0][i] = 1
for i in range(1, n+1):
for j in range(1, 10):
dp[i][j] = dp[i][j-1] + dp[i-1][j]
return sum(dp[n][1:])

print(count_increasing_dp(2))

Output: 36

We initialize a matrix that will hold the number of increasing sequences that can be built with a certain length and starting digit. Then we build up from smaller subproblems to obtain the final count for n-digit numbers.

## Method 4: Using Formulae

For this method, we apply mathematical formulae to calculate the binomial coefficient, which can also be used to count the number of strictly increasing sequences of a given length.

Here’s an example:

def factorial(num):
return 1 if num == 0 else num * factorial(num-1)

def count_increasing_formula(n):
return factorial(9) // (factorial(n) * factorial(9-n))

print(count_increasing_formula(2))

Output: 36

We define a factorial function and use it to compute the binomial coefficient from the formula C(9, n) = 9! / (n! * (9-n)!). This is a direct method that uses mathematical properties to find the solution.

## Bonus One-Liner Method 5: Comprehensions with Filters

This one-liner uses list comprehensions to generate all possible n-digit numbers and filter out those that don’t have strictly increasing digits.

Here’s an example:

print(len([i for i in range(10**(n-1), 10**n) if i == int(''.join(sorted(str(i))))]))

Output for n = 2: 36

We iterate through the range of n-digit numbers and convert them to strings to check if each number is equal to its sorted version, implying strictly increasing order of digits.

## Summary/Discussion

• Method 1: Using Itertools Combinations. Efficient and concise. Limited flexibility for non-combinatorial problems.
• Method 2: Recursive Approach. Intuitive for understanding the problem, yet could be slow for larger values of n due to its exponential time complexity.
• Method 3: Dynamic Programming. Offers good performance as it avoids recalculating subproblems. However, requires extra space for storage.
• Method 4: Using Formulae. Quick computation without iteration, but less flexible and more complex to understand.
• Bonus Method 5: Comprehensions with Filters. Concise but inefficient for very large n, as it generates all n-digit numbers.