Large Exponents in Ruby?
exponent, math, ruby
Solution
You need to do what is called, modular exponentiation.
Problem
I'm just doing some University related Diffie-Hellman exercises and tried to use ruby for it. Sadly, ruby doesn't seem to be able to deal with large exponents: warning: in a**b, b may be too big NaN [...] Is there any way around it? (e.g. a special math class or something along that line?) p.s. here is the code in question: ``` generator = 7789 prime = 1017473 alice_secret = 415492 bob_secret = 725193 puts from_alice_to_bob = (generator**alice_secret) % prime puts from_bob_to_alice = (generator**bob_secret) % prime puts bobs_key_calculation = (from_alice_to_bob**bob_secret) % prime puts alices_key_calculation = (from_bob_to_alice**alice_secret) % prime ```