# ba_mod_exp

Definition: | include builtins\bigatom.e bigatom res = ba_mod_exp(object base, exponent, modulus)
Description: | Returns ba_mod(ba_power(base,exponent),modulus), aka (base^^exponent)%modulus, but much faster, for instance base=123456789, exponent=1234, modulus=12 takes this ˜0s compared to the longhand ba_mod(ba_power(..)) taking about 8.6s, and if you increase the exponent to 12345 you will simply give up on the longhand method (or run out of memory), whereas this still finishes near-instantly. base/exponent/modulus can be integer/string/bigatom. Returns a bigatom, which may be NO_VALUE when an error has occured.
Comments: | Currently used exclusively for rosettacode tasks. Other programming languages have a similar function named ModPow, powmod, expmod, modexpt, powm, or in the case of python the standard pow function has an optional third argument which presumably defaults to 1. I suppose I could have made bigatom.e do the same (as python), but that would break all existing uses of ba_power() that supply the existing optional third bRound parameter as positional rather than named, so I did’t. Obviously when I say "aka (base^^exponent)%modulus", that is not legal Phix syntax, but refers to the way that such expressions might be written in other programming languages. Search rosettacode.org for examples of use; a few but not all such tasks may have copies in demo/rosetta, but the majority of said do not appear to be potentially useful enough to warrant that.
Example: |
    include bigatom.e
    bigatom res = ba_mod_exp(123456789, 1234, 12)
See Also: | mod, power, powmod