How would you write an algorithm in C to calculate large numbers raised to large numbers modulo n. (numbers in the 10,000s) so obviously can't just do it straight off.
I tried working out x, x^2, x^4 and somehow using the binary representation of the power to work it out but I have no idea how to program this.
Code:int power(int base, int exponent, int modulo) { if exponent=1 return base % modulo; if not (exponent % 2) return power(base*base%modulo,exponent/2,modulo) else return power(base*base%modulo,exponent/2,modulo)*base%modulo; }
A power to the 10,000? That's really large, although I've always thought if supercomputers can process really, really, really big numbers.
Maybe I should write some tutorials about math in programming.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks