 # Find all Prime Numbers in Range

Published: 2017-05-21 11:03:23 +0000
Categories: Python,

Python

### Description

Will find and identify any prime numbers between the given start and end values. Primes are identified using a factorial.

Originally written as part of a solution for my May 2016 puzzle

### Snippet

``````import math

def checkNumPrime(n):
''' Check whether n is a prime number
'''

# Numbers less than 4 are all prime
if n < 4:
return True

# Numbers divisible by 2 or 3 aren't prime
if n%2 == 0 or n%3 == 0:
return False

# Create the seeds
di=2
i=5

# Work out the square root of n
# (no need to check anything higher)
lim = math.floor(math.sqrt(n))

while i < lim:
if n%i == 0:
return False

i=i+di
di=6-di

# If we got this far, didn't find a whole number.
# Number is prime
return True

def calculatePrimesForRange(s,e):
''' Iterate over each integer between start and end

return list
'''
primes = []
while s < e:
if checkNumPrime(s):
primes.append(s)
s = s+1

return primes

``````

### Usage Example

``````print calculatePrimesForRange(1,100)
[1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
``````