
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find remainder after dividing n number of 1s by m in Python
Suppose we have two numbers n and m. We have to find the remainder after dividing n number of 1s by m.
So, if the input is like n = 4 m = 27, then the output will be 4, because 1111 mod 27 = 4.
To solve this, we will follow these steps −
Define a function util() . This will take x, n, m
- y := 1
- while n > 0, do
- if n is odd, then
- y := (y * x) mod m
- x := (x * x) mod m
- n := floor of n/2
- if n is odd, then
- return y
From the main method return floor of (util(10, n, 9 * m) / 9)
Example
Let us see the following implementation to get better understanding −
def util(x, n, m) : y = 1 while n > 0 : if n & 1 : y = (y * x) % m x = (x * x) % m n >>= 1 return y def solve(n, m): return util(10, n, 9 * m) // 9 n = 4 m = 27 print(solve(n, m))
Input
4, 27
Output
4
- Related Questions & Answers
- Program to find longest number of 1s after swapping one pair of bits in Python
- Maximum sum after repeatedly dividing N by a divisor in C++
- Find a positive number M such that gcd(N^M,N&M) is maximum in Python
- Program to find number of items left after selling n items in python
- Find the number closest to n and divisible by m in C++
- Count number of 1s in the array after N moves in C
- Program to find number m such that it has n number of 0s at end in Python
- Program to find longest subarray of 1s after deleting one element using Python
- Program to find number of substrings with only 1s using Python
- Program to find modulus of a number by concatenating n times in Python
- Program to find remainder when large number is divided by 11 in C++
- Program to find remainder when large number is divided by r in C++
- Program to find longest consecutive run of 1s in binary form of n in Python
- Program to delete n nodes after m nodes from a linked list in Python
- Maximum consecutive 1s after n swaps in JavaScript
Advertisements