Quantcast
Channel: User raptortech97 - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 22

Answer by raptortech97 for Finding powers of multiple array pairs

$
0
0

I don't know Ruby, but your issue is that you are taking a^b and then taking the modulus. This may mean you are dealing with giant intermediary numbers. The solution is to use a proper modular exponentiation algorithm. This might be built into Ruby, or you might have to roll your own. Here's some pythonish psuedocode for modular exponentiation by squaring:

def exponent_mod(a, b, mod):    r = a    while b > 0:        if b % 2 == 1:            r = (r * r) % mod        b = b / 2        a = (a * a) % mod    return r

If you're really looking for the highest speed possible, though, this is probably a CPU instruction, if you can find a way to reach that far down the stack.


Viewing all articles
Browse latest Browse all 22

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>