Created
October 22, 2012 20:30
-
-
Save alphastorm/3933956 to your computer and use it in GitHub Desktop.
Math-y Challenge from zerocater.com/challenge
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
""" | |
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 | |
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