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
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
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
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
print sum(x for x in range(2, 9 ** 5 * 5) | |
if x == sum(int(c) ** 5 for c in str(x))) |
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
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
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 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 | |
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())) |