In this tutorial, we will learn how to find a total number of non-decreasing numbers with n digits in Python. First, we need to know what a non-decreasing number is. A non-decreasing number is like a list of digits where each successive digit is equal to or greater than the one before it.
In this, we use the NumPy library. NumPy is used for handling large datasets, performing mathematical operations, and many more. We need to explicitly import the NumPy library in Python using the ‘import’ command.
Now we will see how to import the 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): s=np.zeros((n+1,10)); for i in range(10): s[0][i] = 1; for i in range(1,n+1): s[i][9]=1; for i in range(1,n+1): for j in range(8,-1,-1): s[i][j]=s[i-1][j]+s[i][j+1]; result=int(s[n][0]); return result; n=input("Enter number of digits:"); number=int(n); print("Total number of non-decreasing numbers is:",count(number))
Output:
Enter number of digits:6 Total number of non-decreasing numbers is: 5005