I have an algorithm for prime numbers, coded in python. There I need to calculate exponentiations modulo large numbers. To be more precise: For h and k let n = h* 2^k + 1 and I want to calculate 17^((n-1)/2) mod n.
It works, for example I can calculate this with k = 10^4 and h = 123456 in 2 second. With k = 10^5 and h = 123.456, the calculation needs 20 minutes.
But I want even larger exponents, k = 10^9 for example. But I would like to know whether this is possible in a day or a week.
Can I estimate the time needed? Or can python even show me the progress?
Here is my code:
import time
start = time.time()
k = 10**4
h = 123456
n = h*2**k+1
print(pow(17, (n-1)//2,n))
print("Time needed:", time.time() - start, "seconds.")
Edit: I took the exponents k from 10^3 to 10^4 and measured the times it needed for the modular calculation. It's an exponential growth. How do I estimate now how long it will take for k = 10^9?
You can use
numpy.polyfitto produce an equation that estimates the time required for anyk.First, we generate some data, which you've already done:
The execution time look quadratic in your plot, so let's fit a 2nd degree polynomial to the data using numpy.polyfit and numpy.poly1d
How does the quadratic fit against the measured data?
Not bad!
Now we plug in 10**9 for k in the polynomial
Output: about 1118 years on my computer with these specific parameters. What will really happen is that your computer will fall flat on its face when trying to calculate n for k = 10**9 and the polynomial won't actually hold.
Edit: Another thing to note is that I sampled k on the order of 10^3 so any small jitter or inaccuracy can produce an error of many thousands of years once you project all the way out to 10^9