-5

I want large mathematical operation in c++.

long long h= 4294967295;
long long d=7910266469;
long long n=10021211227;
long long result;

I am need calculate this is:

h^d mod n

result=pow(h,d) % n;

I dont know which type using.Please help me for choose type numbers.. Thanks

1 Answers1

1

Look at this wikipedia article. There is a pretty nice example with the following memory-efficient pseudo code:

function modular_pow(base, exponent, modulus)
    if modulus = 1 then return 0 
    c := 1
    for e_prime = 1 to exponent 
        c := (c * base) mod modulus
    return c

There is even a example for beeing memory-efficient and using less operations. I think getting the c++ code out of it should be possible.

If you use this method, long long for your solution should be fine.


Not tested but a simple 1:1 translation from the pseudo code from above...

long long result = 1; 
int i;
for( i=0; i<d;i++){
    result = (result * h) % n;
}
izlin
  • 2,129
  • 24
  • 30