An effective way to eat and fashion in ruby

I need to know the power and mode value from 3 values, for example:

print 1_299_709 ** 1_300_751 % 104_729

      

were there any ruby ​​stones or a faster way to figure this out using ruby?

+4


source to share


2 answers


This is called modular exponentiation and is used in cryptography. Its quite easy to write a modular exponentiation algorithm, a demo is in the wikipedia article linked above.

You can use the openssl standard library to achieve your goal:



require 'openssl'
1_299_709.to_bn.mod_exp(1_300_751, 104_729) # => 90827

      

+10


source


Since Ruby 2.5 modular exponentiation is built:



print 1_299_709.pow(1_300_751, 104_729)

      

0


source







All Articles