- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 modulus of a number by concatenating n times in Python
Suppose we have a number A. We have to generate a large number X by concatenating A, n times in a row and find the value of X modulo m.
So, if the input is like A = 15 n = 3 m = 8, then the output will be 3, because the number x will be 151515, and 151515 mod 8 = 3.
To solve this, we will follow these steps −
- if A is same as 0, then
- return 0
- an:= A
- c:= number of digits in A
- c:= 10^c
- d:= c-1
- newmod := d*m
- val := (c ^ n mod newmod) -1
- val :=(val + newmod) mod newmod
- an :=(an * val) mod newmod
- return floor of (an / d)
Example
Let us see the following implementation to get better understanding −
def solve(A, n, m): if A == 0: return 0 an=A c=len(str(A)) c=10**c d=c-1 newmod = d*m val = pow(c,n,newmod)-1 val = (val+newmod) % newmod an = (an*val) % newmod return an // d A = 15 n = 3 m = 8 print(solve(A, n, m))
Input
15, 3, 8
Output
3
Advertisements