Search code examples
pythonpowlargenumber

pow large numbers in Python


How can I raise large numbers to a power in python?

a = 62608558862573792084872798679396455703616395237802859621162736207631538899993
b = 93910650126758265671774994856253142403789359314618444886584691522424141933664
c = pow(a, b)

It is impossible to get an answer that way. Are there any ways to raise large numbers to a power to make it work?


Solution

  • If you calculate the result to all digits, it has 10^78 digits. That's more than will fit into any RAM of any computer in the world today.

    It is impossible to get an answer that way.

    It will be impossible to get a precise answer for a long time, given that Earth only has ~10^50 atoms.

    The number 62608558862573792084872798679396455703616395237802859621162736207631538899993 looks like a pseudo prime number (is has only 5 prime factors) as used in cryptography. Cryptography often works with modulo operations to limit the number of digits. You can use pow to do modulo math as well:

    pow(base, exp, mod=None)

    Return base to the power exp; if mod is present, return base to the power exp, modulo mod (computed more efficiently than pow(base, exp) % mod).