I wrote down simple algorithm for checking if Mersenne number is prime:
def is_prime_mers(result, step, modulo):
if result != 0:
if step == 0:
return False
result = result ** 2 - 2
if result >= modulo:
result %= modulo
return is_prime_mers(result, step - 1, modulo)
return True
Generally I don't need to give result parameter when script calls this function however I need it for recursive calls.
So only result need to be initialised for this function with value of 4
I can write some initializing function like:
def is_prime_mers_init(step):
is_prime_mers(4, step, count_mersenne(step))
But maybe there is some python/general programming pattern to do that in first function?
Edit: For all curious ones :) This is function what implements Lucas-Lehmer test and checks if given Mersenne number is prime http://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test - however I read only mathematic implementation - so this is purely my code solution.
step is number of recursive calls
count_mersenne(step) gives value of some Mersenne number: 2 ** step - 1 however I use some check in count_mersenne(step) because searching for prime Mersenne numbers can be limited to prime step values.
count_mersenneand4?stepandresultbe?resultshould be 4, step can be any, and basicallycount_mersennevalue depends onstepand returns Mersenne number so this value is set outside, for performance reason I excluded this computation from base function. Updated question. :)