Given a string of numbers and given another number (say m) [0 <= m <= 10^18]. The task is to calculate the modulus of the given number.
Input : num = "214" m = 5 Output : Remainder = 4 Quotient = 42 Input : num = "214755974562154868" m = 17 Output : Remainder = 15 Quotient = 12632704386009109 Input : num = "6466868439215689498894" m = 277 Output : Remainder = 213 Quotient = 23346095448432092053
To find quotient, we can print quotient as we progress in our algorithm or store those numbers in array and print them later.
Initialize mod = 0 First take first digit (from right) and find mod using formula: mod = (mod * 10 + digit) % m quo[i] = mod / m where i denotes the position of quotient number Let's take an example. num = 12345 m = 9 Initialize mod = 0 quo[i] = (mod * 10 + num[i]) / m mod = (mod * 10 + num[i]) % m Where i denotes the position of the i-th digit 1) quo = (0 * 10 + 1) / 9 = 0 mod = (0 * 10 + 1) % 9 = 1 2) quo = (1 * 10 + 2) / 9 = 12 / 9 = 1 mod = (1 * 10 + 2) % 9 = 12 % 9 = 3 3) quo = (3 * 10 + 3) / 9 = 33 / 9 = 3 mod = (3 * 10 + 3) % 9 = 33 % 9 = 6 4) quo = (6 * 10 + 4) / 9 = 64 / 9 = 7 mod = (6 * 10 + 4) % 9 = 64 % 9 = 1 5) quo = (1 * 10 + 5) / 9 = 15 / 9 = 1 mod = (1 * 10 + 5) % 9 = 15 % 9 = 6 Concatenating all values of quotient together (from 1 to n) where n is the number of digits. Thus, modulus is 6 and quotient is 01371.
We can use this technique to find quotient and remainder of big numbers also.
Below is the implementation of above approach :
Remainder = 430 Quotient = 29976385930729688
This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find Quotient and Remainder of two integer without using division operators
- Count of divisors having more set bits than quotient on dividing N
- Program to find remainder when large number is divided by 11
- Program to find remainder when large number is divided by r
- Program to find remainder without using modulo or % operator
- How to compute mod of a big number?
- Prime factors of a big number
- Divide a big number into two parts that differ by k
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Find the remainder when First digit of a number is divided by its Last digit
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- First number to leave an odd remainder after repetitive division by 2
- Find numbers that divide X and Y to produce the same remainder
- Find the remainder when N is divided by 4 using Bitwise AND operator
- Sorting Big Integers
- Chinese Remainder Theorem | Set 1 (Introduction)
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
- Remainder with 7 for large numbers
- Using Chinese Remainder Theorem to Combine Modular equations