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.
Examples:
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[1] = (0 * 10 + 1) / 9 = 0
mod = (0 * 10 + 1) % 9 = 1
2) quo[2] = (1 * 10 + 2) / 9 = 12 / 9 = 1
mod = (1 * 10 + 2) % 9 = 12 % 9 = 3
3) quo[3] = (3 * 10 + 3) / 9 = 33 / 9 = 3
mod = (3 * 10 + 3) % 9 = 33 % 9 = 6
4) quo[4] = (6 * 10 + 4) / 9 = 64 / 9 = 7
mod = (6 * 10 + 4) % 9 = 64 % 9 = 1
5) quo[5] = (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 :
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void modBigNumber(string num, ll m)
{
vector< int > vec;
ll mod = 0;
for ( int i = 0; i < num.size(); i++) {
int digit = num[i] - '0' ;
mod = mod * 10 + digit;
int quo = mod / m;
vec.push_back(quo);
mod = mod % m;
}
cout << "\nRemainder : " << mod << "\n" ;
cout << "Quotient : " ;
bool zeroflag = 0;
for ( int i = 0; i < vec.size(); i++) {
if (vec[i] == 0 && zeroflag == 0)
continue ;
zeroflag = 1;
cout << vec[i];
}
return ;
}
int main()
{
string num = "14598499948265358486" ;
ll m = 487;
modBigNumber(num, m);
return 0;
}
|
Java
import java.util.Vector;
class GFG {
static void modBigNumber(String num, long m) {
Vector<Integer> vec = new Vector<>();
long mod = 0 ;
for ( int i = 0 ; i < num.length(); i++) {
int digit = num.charAt(i) - '0' ;
mod = mod * 10 + digit;
int quo = ( int ) (mod / m);
vec.add(vec.size(), quo);
mod = mod % m;
}
System.out.print( "\nRemainder : " + mod + "\n" );
System.out.print( "Quotient : " );
boolean zeroflag = false ;
for ( int i = 0 ; i < vec.size(); i++) {
if (vec.get(i) == 0 && zeroflag == false ) {
continue ;
}
zeroflag = true ;
System.out.print(vec.get(i));
}
return ;
}
public static void main(String[] args) {
String num = "14598499948265358486" ;
long m = 487 ;
modBigNumber(num, m);
}
}
|
Python3
def modBigNumber(num, m):
vec = []
mod = 0
for i in range ( 0 , len (num), 1 ):
digit = ord (num[i]) - ord ( '0' )
mod = mod * 10 + digit
quo = int (mod / m)
vec.append(quo)
mod = mod % m
print ( "Remainder :" ,mod)
print ( "Quotient :" ,end = " " )
zeroflag = 0 ;
for i in range ( 0 , len (vec), 1 ):
if (vec[i] = = 0 and zeroflag = = 0 ):
continue
zeroflag = 1
print (vec[i],end = "")
return
if __name__ = = '__main__' :
num = "14598499948265358486"
m = 487
modBigNumber(num, m)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void modBigNumber( string num, long m) {
List< int > vec = new List< int >();
long mod = 0;
for ( int i = 0; i < num.Length; i++) {
int digit = num[i] - '0' ;
mod = mod * 10 + digit;
int quo = ( int ) (mod / m);
vec.Add(quo);
mod = mod % m;
}
Console.Write( "Remainder : " + mod + "\n" );
Console.Write( "Quotient : " );
bool zeroflag = false ;
for ( int i = 0; i < vec.Count; i++) {
if (vec[i] == 0 && zeroflag == false ) {
continue ;
}
zeroflag = true ;
Console.Write(vec[i]);
}
return ;
}
public static void Main() {
string num = "14598499948265358486" ;
long m = 487;
modBigNumber(num, m);
}
}
|
PHP
<?php
function modBigNumber( $num , $m )
{
$vec ;
$x = 0;
$mod = 0;
for ( $i = 0;
$i < strlen ( $num ); $i ++)
{
$digit = $num [ $i ] - '0' ;
$mod = $mod * 10 + $digit ;
$quo = (int)( $mod / $m );
$vec [ $x ++] = $quo ;
$mod = $mod % $m ;
}
echo "Remainder : " .
$mod . "\n" ;
echo "Quotient : " ;
$zeroflag = 0;
for ( $i = 0; $i < $x ; $i ++)
{
if ( $vec [ $i ] == 0 &&
$zeroflag == 0)
continue ;
$zeroflag = 1;
echo $vec [ $i ];
}
return ;
}
$num = "14598499948265358486" ;
$m = 487;
modBigNumber( $num , $m );
?>
|
Javascript
<script>
function modBigNumber(num, m)
{
let vec = [];
let x = 0;
let mod = 0;
for (let i = 0;
i < num.length; i++)
{
digit = num[i] - '0' ;
mod = mod * 10 + digit;
quo = parseInt(mod / m);
vec[x++] = quo;
mod = mod % m;
}
document.write( "Remainder : " + mod + "<br>" );
document.write( "Quotient : " );
let zeroflag = 0;
for (let i = 0; i < x; i++)
{
if (vec[i] == 0 &&
zeroflag == 0)
continue ;
zeroflag = 1;
document.write(vec[i]);
}
return ;
}
let num = "14598499948265358486" ;
let m = 487;
modBigNumber(num, m);
</script>
|
Output:
Remainder = 430
Quotient = 29976385930729688
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(N), as we are using N extra space vec.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Apr, 2022
Like Article
Save Article