Python Program to Count Non-Bouncy numbers
Last Updated :
03 Aug, 2022
If any number is represented in such a way that when we are reading it from left to right each ith Digit is greater or equal than i-1th digit is known as an increasing number. And if digits of any number are decreasing from left to right it's known as decreasing number.`
Example:
Increasing Number ?235668
all the digits from left to right are greater or equal to the previous digit.
Decreasing Number ? 653221
all the digits from left to right are lesser than or equal to the previous digit.
But if the number is neither increasing nor decreasing is Known as Bouncy Number.
Example:
523469 -> Some Digits from left to right are decreasing from left to right and some are increasing. So this is the example of Bouncy Number.
The task in this article is to count the total number of Non-Bouncy Numbers below 10k and print the final count in mod(109+7). To do this we will use the Stars and Bars method to calculate the number of non-bouncy numbers in the given range.
Stars and Bars Method:
Stars and Bars method is a technique that is used to deal with combination based problems. These type of problems arises when we want the number of identical groups.
The formula for calculating identical groups:
count = (N+M-1)/N
Where N are the identical objects and M is the container or range.
Final Formula:
count=\frac{N!}{M!(N-M)!}
Examples:
Input : k = 6
Output : 12951
Input : k = 9
Output : 140906
Below is the implementation:
Python3
# import reduce function from functools
from functools import reduce
# define a function to
# calculate nCr
def nCr(n, k):
# this approach is based on
# approach of stars and bar method
# using reduce and lambda function
# to calculate number & denom
number = reduce(lambda x, y: x * y,
list(range(n, n - k, -1)))
denom = reduce(lambda x, y: x * y,
list(range(1, k + 1)))
# denom root of number will be the final result
return number // denom
# Driver Code
# input value of k
k = 6
# calculating r using function call
r = int((nCr(k + 10, 10) +
nCr(k + 9, 9)
- 2 - 10 * k))
# print final result
print(r % (1000000000 + 7))
Output:
12951
Similar Reads
Python Program to Count Even and Odd Numbers in a List In Python working with lists is a common task and one of the frequent operations is counting how many even and odd numbers are present in a given list. The collections.Counter method is the most efficient for large datasets, followed by the filter() and lambda approach for clean and compact code. Us
4 min read
Python program to add two binary numbers Given two binary numbers, write a Python program to compute their sum. Examples: Input: a = "11", b = "1" Output: "100" Input: a = "1101", b = "100" Output: 10001Approach: Naive Approach: The idea is to start from the last characters of two strings and compute digit sum one by one. If the sum become
4 min read
Python Program to Convert a Number into 32-Bit Binary Format Binary representation is a fundamental concept in computer science, and converting numbers into binary format is a common task for programmers. In this article, we will explore some simple and generally used methods to convert a number into a 32-bit binary format using Python. Each method provides a
2 min read
Python program to count positive and negative numbers in a list In this article, we will explore various methods to count positive and negative numbers in a list. The simplest way to do this is by using a loop. This method counts the positive and negative numbers in a list by iterating through each element using for loop.Pythona = [10, -20, 30, -40, 50, -60, 0]
2 min read
Python program to convert Base 4 system to binary number Given a base 4 number N, the task is to write a python program to print its binary equivalent. Conversion Table: Examples: Input : N=11002233 Output : 101000010101111 Explanation : From that conversion table we changed 1 to 01, 2 to 10 ,3 to 11 ,0 to 00.Input : N=321321 Output: 111001111001Method 1:
3 min read