Skip to content

Instantly share code, notes, and snippets.

@etcetra7n
Last active May 2, 2021 11:56
Show Gist options
  • Save etcetra7n/548e84de7348f28c54fd3749b61f5621 to your computer and use it in GitHub Desktop.
Save etcetra7n/548e84de7348f28c54fd3749b61f5621 to your computer and use it in GitHub Desktop.
Prime Number Generator in Python

Prime Number Generator in python

Use the prime_in(start,end) function in the prime_generator.py file to generate prime numbers in python

  • start -> starting value as integer
  • end -> ending value as integer

The function returns a list of prime numbers between start and end, where start might itself be included if it is a prime number, but end shall never be included

The sqrt and floor functions from the math module are required for the prime generator function, so import that one with the following code

from math import sqrt, floor

The above code is included in prime_generator.py

from math import sqrt, floor
def prime_in(start, end):
res = list()
for i in range(start, end):
for j in range(2, floor(round(sqrt(i)))+1):
if i%j == 0:
break
else:
res.append(i)
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment