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
from math import sqrt | |
def divisors(n): | |
for x in xrange(1, int(sqrt(n)) + 1): | |
if n % x == 0: | |
yield x | |
if x != 1 and x * x != n: | |
yield n / x | |
def amicable(n): |
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
names = sorted(open('names.txt').readline().replace('"', '').split(',')) | |
print sum(((i + 1) * sum(ord(c) - ord('A') + 1 for c in n) | |
for (i, n) in enumerate(names))) |
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
from math import sqrt | |
def divisors(n): | |
for x in xrange(1, int(sqrt(n)) + 1): | |
if n % x == 0: | |
yield x | |
if x != 1 and x * x != n: | |
yield n / x | |
abundants = set() |
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
from itertools import islice | |
def comb(l): | |
if len(l) == 0: | |
yield [] | |
else: | |
for i in range(len(l)): | |
for c in comb(l[:i] + l[i + 1:]): | |
yield [l[i]] + c |
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
from itertools import takewhile | |
def fib(): | |
a, b = 1, 1 | |
while 1: | |
yield a | |
a, b = b, a + b | |
print sum(1 for _ in takewhile(lambda x: len(str(x)) < 1000, fib())) |
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
def seqlen(n): | |
rem = [] | |
x = 1 | |
while 1: | |
x = (10 * x) % n | |
if x == 1: | |
rem.append(x) | |
break | |
elif x == 0: | |
break |
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
from itertools import takewhile | |
from itertools import count | |
from math import sqrt | |
def isprime(n): | |
for x in range(2, int(sqrt(n)) + 1): | |
if n % x == 0: | |
return 0 | |
return 1 |
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
def spiral(n): | |
max = n * n | |
s = 1 | |
n = 1 | |
i = 2 | |
while n <= max: | |
for j in range(4): | |
if n >= max: | |
return s | |
else: |
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
def combs(low, high): | |
result = [] | |
i = low | |
j = low | |
while i <= high: | |
while j <= high: | |
result.append(i ** j) | |
j += 1 | |
i += 1 | |
j = low |
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
print sum(x for x in range(2, 9 ** 5 * 5) | |
if x == sum(int(c) ** 5 for c in str(x))) |