I saw an interesting challenge on SoloLearn about writing code to find all the Mersenne primes up to a number. I started by writing a function to test to see if the given number was a prime.
function returns True or False
factorList = 
for x in range (1,input+1):
if input % x == 0:
if len(factorList)== 2:
It is not perhaps the most elegant solution, but it appears to be fully functional.
I then moved on, using my function above to test to see if a given input was a Mersenne Prime.
If it is a prime the number is tested to see if it is a mersenne prime.
Function returns True or False
if primeTest(input) == False:
powerOfTwo = input + 1
index = log(powerOfTwo,2)
if index % 1 == 0:
I then put these together and produced this code to test all the numbers between 1 and 10000 to see if they were Mersenne Primes.
to see if they are a Mersenne prime
Function returns a list of Mersenne primes.
primeList = 
for x in range(1,max+1):
if mersennePrimeTest(x)== True:
The above code works, but it not particularly efficient and it is doing a lot of work that is redundant. I decided to change my approach and to check all the 2n-1 which were below the value of max.
maxIndex = int(log(max,2))
workingnumber = 1
primeList = 
for x in range (maxIndex):
workingnumber = workingnumber * 2
if primeTest(workingnumber-1) == True:
I found that this version was much faster.