Statement of Completion#1a522b09
Python Collections
medium
Finding Mersenne primes with Python
Resolution
Activities
Reading the first 1M Prime numbers¶
In [ ]:
!head primes1.csv
We'll skip the n
row (the header) and then just read all the lines. Don't worry if this code doesn't make much sense. You'll learn how to work with files in other Skill Tracks!
In [ ]:
with open('primes1.csv') as fp:
fp.readline()
primes = [int(n) for n in fp]
In [ ]:
len(primes)
In [ ]:
primes[:15]
Detecting if a Prime is a Mersenne Prime¶
1) Write the function is_mersenne
to identify if a prime is a Mersenne prime¶
In [ ]:
def is_mersenne(p):
pass
In [ ]:
# check by yourself
In [ ]:
is_mersenne(3) # should be True
In [ ]:
is_mersenne(11) # should be False
In [ ]:
is_mersenne(31) # should be True
2) Find all Mersenne primes¶
In [ ]:
mersenne_primes = ...
Improving the performance¶
3) Create the list mersenne_numbers
¶
In [ ]:
mersenne_numbers = ...
4) Even more efficient, turn mersenne_numbers
into a set¶
In [ ]:
# now it's a set
mersenne_numbers = ...
5) Find Mersenne primes using mersenne_numbers
¶
In [ ]:
efficient_mersenne_primes = ...