In [ ]:
def isPrime( n ):
'''(int) -> bool
Return True if the given number n is a prime number return False otherwise
'''
if n <= 1: return False
for i in range(2, n):
if n % i == 0: return False
return True
In [ ]:
def sumPrimes(start, end):
'''(int, int) -> int
Pre: start > 0
end > start
Return the sum of prime numbers between start and end
'''
partial = 0
for i in range( start, end + 1 ):
if isPrime( i ): partial += i
return partial
In [ ]:
def sumPrimes2(start, end):
'''(int, int) -> int
Pre: start > 0
end > start
Return the sum of prime numbers between start and end
'''
elements = []
for i in range(start, end + 1):
if isPrime( i ): elements.append( i )
return sum( elements )
In [ ]:
import random
j = random.choice( range(2, 101) )
In [ ]:
primes = []
for i in range(2, j + 1):
if isPrime( i ): primes.append( i )
In [ ]:
print( '[2, {}].primes => {}'.format( j, primes ) )
In [ ]:
print( '[2, {}].sum_primes => {}'.format( j, sumPrimes( 2, j ) ) )
In [ ]:
print( '[2, {}].sum_primes => {}'.format( j, sumPrimes2( 2, j ) ) )
In [ ]:
help(sumPrimes2)
In [ ]:
help(isPrime)