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 chain | |
from math import sqrt | |
def comb(l): | |
if len(l) == 0: | |
yield [] | |
else: | |
for i in range(len(l)): | |
for cc in comb(l[:i] + l[i + 1:]): | |
yield [l[i]] + cc |
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 istriangle(n): | |
return (sqrt(8 * n + 1) - 1) / 2 % 1 == 0 | |
words = open('words.txt').readline().replace('"', '').split(',') | |
print sum(1 for _ in (filter(lambda x: istriangle(x), | |
(sum(ord(c) - ord('A') + 1 for c in w) | |
for w in words)))) |
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 comb(l): | |
if len(l) == 0: | |
yield [] | |
else: | |
for i in range(len(l)): | |
for cc in comb(l[:i] + l[i + 1:]): | |
yield [l[i]] + cc | |
def combn(n): | |
l = [c for c in str(n)] | |
return [int(''.join(x)) for x in comb(l)] |
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 | |
from itertools import count | |
from itertools import islice | |
def f(n): return n * (3 * n - 1) / 2 # nth pentagonal | |
def p(n): return not ((sqrt(24 * n + 1) + 1) / 6) % 1 # is pentagonal | |
print islice((abs(f(i) - f(j)) | |
for i in count(2) | |
for j in xrange(i - 1, 1, -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
from math import sqrt | |
from itertools import islice | |
from itertools import count | |
def pentagonal(n): | |
return not ((sqrt(24 * n + 1) + 1) / 6) % 1 | |
def hexagonal(n): | |
return not ((sqrt(8 * n + 1) + 1) / 4) % 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
from itertools import takewhile, count, imap | |
from math import sqrt | |
from operator import and_ | |
def twicesq(n): | |
return takewhile(lambda x: x < n, | |
(x * x * 2 for x in count(1))) | |
def isprime(n): | |
for x in range(2, int(sqrt(n)) + 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
from itertools import count, islice | |
from operator import and_ | |
def factorise(n): | |
result = [] | |
check = 2 | |
while (check * check <= n): | |
if n % check == 0: | |
result.append(check) | |
n /= check |
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 str(sum(i ** i for i in xrange(1, 1001)))[-10:] |
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 | |
from itertools import takewhile, dropwhile, count | |
def comb(l): | |
if len(l) == 0: | |
yield [] | |
else: | |
for i in range(len(l)): | |
for cc in comb(l[:i] + l[i + 1:]): | |
yield [l[i]] + cc |
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 | |
from itertools import takewhile, dropwhile, count | |
def prime(): | |
primes = [] | |
for n in count(2): | |
composite = 0 | |
for p in primes: | |
if not n % p: | |
composite = 1 |