Last active
April 22, 2018 14:47
-
-
Save thejevans/ae0ce4b15decb0e6de130128350dd491 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import sys | |
from array import * | |
# returns true if x is prime, false is not | |
def is_prime(x): | |
if x < 2: | |
return True | |
if x >= 2: | |
for y in range(2,x): | |
if not (x % y): | |
return False | |
else: | |
return False | |
return True | |
# returns the next prime number after x | |
def next_prime(x): | |
for i in range(x+1, x+50): | |
if is_prime(i): | |
return i | |
return -1 | |
# helper for the build_chain function | |
def helper(n, p, arr): | |
print arr | |
if n == 1: | |
# printing the chain here with ./q4b 3 gives [1,3,7] | |
# chain is None after returning..? | |
return arr | |
else: | |
p1 = 2*p + 1 | |
if is_prime(p1) == False: | |
return -1 | |
else: | |
arr.append(p1) | |
return helper(n-1, p1, arr) | |
# builds x terms of the cunningham chain | |
def build_chain(n): | |
p = 1 | |
arr = [] | |
arr.append(p) | |
chain = helper(n, p, arr) | |
# printing out the chain here gives None | |
if chain == -1: | |
arr = [] | |
next_p = next_prime(p) | |
arr.append(next_p) | |
chain = helper(n, next_p, arr) | |
print "chain: " + str(chain) | |
build_chain(int(sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment