
Kirt U. answered 08/25/14
Tutor
4.7
(187)
Expert computer programming and math tutor
Use a helper function to see if a number is prime and first seed your prime list with [2, 3] :
primes = [2,3] ## seed the prime array
## create a helper function based on the prime array to see if the number is prime.
## if the number n passed to the function is mod 0 with any of the numbers in the
##primes array, then return false.
## If the number n is not mod 0 with any of the numbers in the primes array then
## it is a new prime number
def isPrime(n):
for num in primes:
if n % num == 0:
return False
return True
## loop through the sequence 4..100 inclusive
## if isPrime() returns True then append the number to
## the primes array
def run():
for n in range(4, 101):
if isPrime(n):
primes.append(n)
print primes
if __name__ == "__main__":
run()
for n in range(4, 101):
if isPrime(n):
primes.append(n)
print primes
if __name__ == "__main__":
run()
Peter H.
06/29/14