An attempt to find Mersenne primes using the Lucas-Lehmer primality test:
n=int(input("Enter n to check if 2^n-1 is a prime:"))
a=4
for i in range(0,n-2):
b=a**2
a=b-2
if i==n-3:
c=(a)%((2**n) - 1)
if c==0:
print((2**n) - 1 ,"is a prime number")
break
if c!=0:
print((2**n) - 1 ,"is not a prime number")
Can you critique this code? It is apparent that it needs improvement as it doesnt work for n<=2 and computing power increases exponentially for increasing n. I have been learning python for less than a year, I am only 15 and would love any feedback on this program.
Edit: The confusion with n-2 and n-3 is because we have to check if the (n-1)th term in the series is divisible by l = 2^n - 1 to establish that l is a Mersenne prime. But as you would have noticed the above written code considers the 0th term as 14 which is the 3rd term in the actual series. This means there is a gap of two terms between the actual series and the series procured by the program, therefore to actually study the (n-1)th term in the series , we have to consider the (n-3)th term here. Hence the mess that doesn't allow n to be <=2.
Please provide your opinion on this
True
for 0 < n <= 2 andFalse
for n <= 0? \$\endgroup\$