Skip to content

Instantly share code, notes, and snippets.

@alphastorm
Created October 22, 2012 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphastorm/3933965 to your computer and use it in GitHub Desktop.
Save alphastorm/3933965 to your computer and use it in GitHub Desktop.
Math-y Challenge from zerocater.com/challenge
"""
Sunil Srivatsa - 10/22/12
Math-y Challenge from zerocater.com/challenge
Notes:
I use a .txt file of the first 78498 prime numbers from 2 to <1 million from:
http://www.svobodat.com/primes/PRIMES1T.TXT
"""
import sys, urllib2
def main():
# keep track of the current curr_sum/chain and # of results found
curr_sum, results = 0, 0
chain = []
# take command line input for the target # of results, default to 5
num_results = 5
if len(sys.argv) == 2: num_results = sys.argv[1]
# fetch and sanitize list of primes
txt = urllib2.urlopen("http://www.svobodat.com/primes/PRIMES1T.TXT")
primes = map(int, txt.read().strip().split('\n'))
# iterate over the primes printing the length of clean chains
for prime in primes:
chain.append(prime)
curr_sum += prime
if curr_sum % prime == 0:
print len(chain)
results += 1
if results > num_results: break
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment