Skip to content

Instantly share code, notes, and snippets.

@fyxme
Last active October 10, 2024 16:12
Show Gist options
  • Save fyxme/77905a9e6f964595cdbe6da1826f7f0f to your computer and use it in GitHub Desktop.
Save fyxme/77905a9e6f964595cdbe6da1826f7f0f to your computer and use it in GitHub Desktop.
next_prime.py
#!/usr/bin/env python
import math
def isPrime(n):
""" Check if n is prime using trial division as our primality test """
if n%2 == 0 and n > 2:
# takes care of all the even numbers
return False
for i in range(3, int(math.sqrt(n))+1, 2):
if(n%i == 0):
# Found a number which divides n therefore n is not prime
return False
return True
def main():
lowerbound = int(raw_input('lowerbound : '))
num_primes = int(raw_input('number of primes to find: '))
# Added count and current variable in case you need to
# keep user input for later on. Such as to print it out at the end
# If you didn't need it you could also use num_primes as the
# while loop counter and lowerbound as your current
count = 0 # count represents the number of primes found
current = lowerbound # represents the current number to check for
while count < num_primes:
if isPrime(current):
# Found a prime!
print current
count += 1
current += 1
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment