Last active
July 27, 2016 02:51
-
-
Save jwnelson/02519ff7eaef3817fdfb8532172f7028 to your computer and use it in GitHub Desktop.
Script that finds the number of primes produced by the function x^2 - 2999x + 2248541 evaluated over a range of integers passed in as args. Produces 80 primes between 1460 and 1539 (inclusive). Inspired by this tweet from @AlgebraFact: https://twitter.com/AlgebraFact/status/757674572254674944
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
Finds the number of primes produced by the function x^2 - 2999x + 2248541 | |
evaluated over a range of integers passed in as args. Produces 80 primes | |
between 1460 and 1539 (inclusive). Inspired by this tweet from @AlgebraFact: | |
https://twitter.com/AlgebraFact/status/757674572254674944 | |
Usage: | |
./primes_in_range.py <lower bound> <upper bound> | |
""" | |
import math | |
import sys | |
def check_prime(num): | |
a = 2 | |
while a < num: | |
if num % a == 0 and a!=num: | |
return False | |
a += 1 | |
return True | |
def main(argv): | |
prime_count = 0 | |
if len(argv) == 0: | |
lower = 1460 | |
upper = 1539 # there should be 80 consecutive primes in this range! | |
else: | |
lower = int(argv[0]) | |
upper = int(argv[1]) | |
for x in range(lower, upper + 1): | |
y = x**2 - 2999*x + 2248541 | |
print "f(%d) = %d" %(x,y) | |
prime_bool = check_prime(y) | |
if prime_bool == True: | |
print " %d is prime" %y | |
prime_count += 1 | |
print "found %d primes between %d and %d" %(prime_count, lower, upper) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment