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
amount = 200 | |
coins = (200, 100, 50, 20, 10, 5, 2, 1) | |
ways = [1] + [0] * amount | |
# dynamic programing (quick) | |
for coin in coins: | |
for i in range(coin, amount + 1): | |
ways[i] += ways[i - coin] | |
print ways[amount] |
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 pandigital(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 gcd(i, j): | |
while j: | |
i, j = j, i % j | |
return i | |
n, d = reduce(lambda (n1,d1), (n2, d2): (n1*n2, d1*d2), | |
((n, d) | |
for i in range(1, 11) | |
for d in range(1, i) | |
for n in range(1, d) |
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 fact(n): | |
s = 1 | |
while n > 0: | |
s = s * n | |
n -= 1 | |
return s | |
print sum(n for n in xrange(3, 100000) | |
if sum(fact(int(c)) for c in str(n)) == 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
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 |
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 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 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
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
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) |