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
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 | |
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
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 operator import mul | |
from itertools import takewhile | |
def nth(): | |
i = 1 | |
s = '1' | |
while 1: | |
yield i, s[i - 1] | |
i += 1 | |
s += str(i) |
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 perimeters(n): | |
s = 0 | |
for a in range(1, n / 3): | |
if n * (n - 2 * a) % (2 * (n - a)) == 0: | |
s += 1 | |
return s | |
print max((perimeters(i), i) for i in range(2, 1000, 2))[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 pandigital(s): | |
return ''.join(sorted(s)) == '123456789' | |
print max(filter(pandigital, (str(n) + str(n * 2) | |
for n in range(9876, 9123, -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 | |
from itertools import islice | |
from math import sqrt | |
def truncated(n): | |
l, s = [], 10 | |
while n / s > 0: | |
l.extend([n / s, n % s]) | |
s *= 10 | |
return set(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
def palindrome(s): | |
return s == s[::-1] | |
def d2b(n): | |
s = '' | |
while n: | |
if n & 1: | |
s = '1' + s | |
else: | |
s = '0' + s |
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 | |
from math import log | |
def rotate(n): | |
d = int(log(n, 10)) | |
m = 10 ** d | |
for _ in range(d): | |
r = n % 10 |