dimanche 17 février 2019

Calculating Modulo between 2 large numbers reprezented as strings

I have tried to do a code, to compute % of large numbers, I only managed to do so for only the dividend to be large and the mod to be represented as integer, I have no idea how to do otherwise. I need this exactly for Large numbers division that is why I need this. No I will not use a BigInts library, it is not accepted by online judges, moreover Id like to know how to do it myself.

This is what I have written for just one large number.

int mod(string num, int a) 
{ 
int res = 0; 

for (int i = 0; i < num.length(); i++) 
     res = (res*10 + (int)num[i] - '0') %a; 

return res; 
} 

Aucun commentaire:

Enregistrer un commentaire