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(n for n in range(1000) if n % 3 == 0 or n % 5 == 0) |
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(n for n in takewhile(lambda f: f < 4000000, fib()) if n % 2 == 0) |
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 factorise(n): | |
result = [] | |
check = 2 | |
while (check * check <= n): | |
if n % check == 0: | |
result.append(check) | |
n /= check | |
else: | |
check += 1 | |
if 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
def palindrome(n): | |
return n == int(str(n)[::-1]) | |
print max((i * j for i in range(100, 1000) | |
for j in range(100, 1000) | |
if palindrome(i * j))) |
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 | |
def lcm(i, j): | |
return i * j / gcd(i, j) | |
print reduce(lcm, range(1, 21)) |
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(range(1, 101)) ** 2 - sum(n * n for n in range(1, 101)) |
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 | |
def prime(): | |
primes = [] | |
for n in count(2): | |
composite = 0 | |
for p in primes: | |
if not n % p: | |
composite = 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 islice | |
s = '\ | |
73167176531330624919225119674426574742355349194934\ | |
96983520312774506326239578318016984801869478851843\ | |
85861560789112949495459501737958331952853208805511\ | |
12540698747158523863050715693290963295227443043557\ | |
66896648950445244523161731856403098711121722383113\ | |
62229893423380308135336276614282806444486645238749\ |
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 takewhile | |
def prime(): | |
primes = [] | |
for n in count(2): | |
composite = 0 | |
for p in primes: | |
if not n % p: | |
composite = 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 dropwhile | |
from math import sqrt | |
from operator import mul | |
def triple(): | |
for a in range(1, 1000): | |
for b in range(a + 1, 1000): | |
c = int(sqrt(a * a + b * b)) | |
if c * c == a * a + b * b: |
OlderNewer