Consecutive Primes

January 1, 2011   ·   By ana   ·   2 Comments   ·   Posted in Code

This morning I woke up to see this tweet:

While I have no reason to doubt the good professor’s claim, I thought it might be fun to check it out. A little googling (yeah, I was feeling very lazy) found a Python isprime() function:

def isprime(n):
'''check if integer n is a prime'''
# range starts with 2 and only needs to go up the squareroot of n
for x in range(2, int(n**0.5)+1):
if n % x == 0:
return False
return True


And we can use this to verify that 2011 is a prime number:

>>> isprime(2011)
True
>>>


Next let’s check that the list of numbers given does indeed add up to 2011:

>>> PRIMES_TO_SUM = [157,163,167,173,179,181,191,193,197,199,211]
>>> sum(PRIMES_TO_SUM)
2011
>>>


And that they are all, in fact, prime:

>>> [isprime(p) for p in PRIMES_TO_SUM]
[True, True, True, True, True, True, True, True, True, True, True]
>>>


And finally that the primes are consecutive, that is there are no other primes hiding in this range of numbers:

>>> for x in range(min(PRIMES_TO_SUM), max(PRIMES_TO_SUM)+1):
…     if isprime(x):
…         if x in PRIMES_TO_SUM:
…             print "%s is prime and already in PRIMES_TO_SUM" % x
…         else:
…             raise Exception("%s is prime and not in PRIMES_TO_SUM" % x)
…     else:
…         print "%s is not prime" % x
…
157 is prime and already in PRIMES_TO_SUM
158 is not prime
159 is not prime
160 is not prime
161 is not prime
162 is not prime
163 is prime and already in PRIMES_TO_SUM
164 is not prime
165 is not prime
166 is not prime
167 is prime and already in PRIMES_TO_SUM
168 is not prime
169 is not prime
170 is not prime
171 is not prime
172 is not prime
173 is prime and already in PRIMES_TO_SUM
174 is not prime
175 is not prime
176 is not prime
177 is not prime
178 is not prime
179 is prime and already in PRIMES_TO_SUM
180 is not prime
181 is prime and already in PRIMES_TO_SUM
182 is not prime
183 is not prime
184 is not prime
185 is not prime
186 is not prime
187 is not prime
188 is not prime
189 is not prime
190 is not prime
191 is prime and already in PRIMES_TO_SUM
192 is not prime
193 is prime and already in PRIMES_TO_SUM
194 is not prime
195 is not prime
196 is not prime
197 is prime and already in PRIMES_TO_SUM
198 is not prime
199 is prime and already in PRIMES_TO_SUM
200 is not prime
201 is not prime
202 is not prime
203 is not prime
204 is not prime
205 is not prime
206 is not prime
207 is not prime
208 is not prime
209 is not prime
210 is not prime
211 is prime and already in PRIMES_TO_SUM


We can also phrase these as assertions, which will raise an error if false:

assert isprime(2011)
assert all([isprime(p) for p in PRIMES_TO_SUM])
assert sum(PRIMES_TO_SUM) == 2011


And since the source for this blog post is tiny, let’s just include it here:


This morning I woke up to see this tweet:
src="https://img.skitch.com/20110101-nbcxum7efh8583f63yw2b498hq.png"
alt="Twitter / @math prof: 2011 is also the sum of 11 …" /></a>
<!—more—>
While I have no reason to doubt the good professor's claim, I thought it might
be fun to check it out. A little googling (yeah, I was feeling very lazy) found
a "Python isprime()
function":http://www.daniweb.com/forums/post319401.html#post319401:
{{ d['sections']['check-prime-2011.py|idio']['is-prime'] }}
And we can use this to verify that 2011 is a prime number:
{{ d['sections']['check-prime-2011.py|idio|pycon|pyg']['2011-is-prime'] }}
Next let's check that the list of numbers given does indeed add up to 2011:
{{ d['sections']['check-prime-2011.py|idio|pycon|pyg']['primes-to-sum'] }}
And that they are all, in fact, prime:
{{ d['sections']['check-prime-2011.py|idio|pycon|pyg']['check-all-prime'] }}
And finally that the primes are consecutive, that is there are no other primes
hiding in this range of numbers:
{{ d['sections']['check-prime-2011.py|idio|pycon|pyg']['check-consecutive'] }}
We can also phrase these as assertions, which will raise an error if false:
{{ d['sections']['check-prime-2011.py|idio']['assertions'] }}
And since the source for this blog post is tiny, let's just include it here:
<notextile><pre>
{{ d['index.txt|wrap']|e }}
</pre></notextile>
And here is the Python script in full:
{{ d['check-prime-2011.py|pyg'] }}
Raw source code is of course available at <a
href="http://bitbucket.org/ananelson/dexy-blog">bitbucket</a>.


And here is the Python script in full:

# isprime() function taken from
# http://www.daniweb.com/forums/post319401.html#post319401
### @export "is-prime"
def isprime(n):
'''check if integer n is a prime'''
# range starts with 2 and only needs to go up the squareroot of n
for x in range(2, int(n**0.5)+1):
if n % x == 0:
return False
return True
### @export "2011-is-prime"
isprime(2011)
### @export "primes-to-sum"
PRIMES_TO_SUM = [157,163,167,173,179,181,191,193,197,199,211]
sum(PRIMES_TO_SUM)
### @export "check-all-prime"
[isprime(p) for p in PRIMES_TO_SUM]
### @export "check-consecutive"
for x in range(min(PRIMES_TO_SUM), max(PRIMES_TO_SUM)+1):
if isprime(x):
if x in PRIMES_TO_SUM:
print "%s is prime and already in PRIMES_TO_SUM" % x
else:
raise Exception("%s is prime and not in PRIMES_TO_SUM" % x)
else:
print "%s is not prime" % x
### @export "assertions"
assert isprime(2011)
assert all([isprime(p) for p in PRIMES_TO_SUM])
assert sum(PRIMES_TO_SUM) == 2011


Raw source code is of course available at bitbucket.