Created
March 6, 2013 05:36
-
-
Save kayzhu/5096990 to your computer and use it in GitHub Desktop.
Clean chain calculation
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
import itertools | |
def generate_prime(): | |
d = {} | |
for i in itertools.count(2): | |
n = d.pop(i, None) | |
if n: | |
p = n + i | |
while p in d: | |
p += n | |
d[p] = n | |
else: | |
yield i | |
d[i * i] = i | |
def clean_chain(seq_num): | |
prime_sum = 0 | |
valid_chain_counter = 0 | |
seq_length = 0 | |
for e in generate_prime(): | |
seq_length += 1 | |
print e, prime_sum | |
prime_sum += e | |
if prime_sum % e == 0: | |
valid_chain_counter += 1 | |
if valid_chain_counter == seq_num: | |
break | |
return seq_length | |
if __name__ == "__main__": | |
print clean_chain(4) | |
print "---------------------------------------------------" | |
print clean_chain(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment