# Find total number of non-decreasing numbers with n digits in Python

In this tutorial, we’re going to learn how to find the total number of non-decreasing numbers with n digits in Python using the NumPy library. But first, we need to understand what a non-decreasing number is. A non-decreasing number is a number in which every digit (other than the first digit) is equal to or greater than its previous digit.

Also, we need to understand the utility of NumPy. NumPy is a special library in Python that can be implemented to use various mathematical functions, multi-dimensional arrays, etc. We need to explicitly import the NumPy library in Python using the ‘import’ command.

Let’s first check how to import NumPy library in Python:

import numpy as np;

## Finding the total number of non-decreasing numbers in Python

import numpy as np; def count(n) : c=np.zeros((n+1,10)); for i in range(10): c[0][i] = 1; for i in range(1,n+1): c[i][9]=1; for i in range(1,n+1): for j in range(8,-1,-1): c[i][j]=c[i-1][j]+c[i][j+1]; result=int(c[n][0]); return result; n=input("Enter number of digits:"); no=int(n); print("Total no. of non-decreasing digits is ",count(no))

In this program, we are evaluating each of the numbers between a specified range and checking for the condition of the non-decreasing number. If the condition is satisfied, we’re counting the same. After executing the above code, we’ll find the total number of non-decreasing numbers with any number of digits.

Output generated will be:

Enter no. of digits: 3 Total no. of non-decreasing digits is 220

## Leave a Reply

You must be logged in to post a comment.