I moved my solutions to a full-fledged repo, so that's where you should go looking for my latest kludges.
Last active
December 16, 2015 18:39
-
-
Save jboynyc/5479576 to your computer and use it in GitHub Desktop.
Project Euler solutions
This file contains 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
# http://projecteuler.net/problem=1 | |
sum(filter(lambda n: False if n % 3 and n % 5 else True, range(0,1000))) |
This file contains 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
#https://projecteuler.net/problem=10 | |
is_prime = lambda n: not len(filter(lambda x: not n % x, range(2,int(round(sqrt(n)))+1))) | |
sum(filter(is_prime, xrange(2,2000000))) # slow; use gist.github.com/jboynyc/6247226 instead |
This file contains 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
# http://projecteuler.net/problem=2 | |
def fibb(max=4000000, l=[1,2]): | |
n = l[-1] + l[-2] | |
if n < max: | |
l.append(n) | |
fibb(max,l) | |
else: | |
return l | |
sum(filter(lambda x: False if x % 2 else True, fibb())) |
This file contains 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
# http://projecteuler.net/problem=3 | |
def factors_of(n): | |
return filter(lambda x: not n % x, range(1,int(n**.5))) | |
is_prime = lambda n: not len(filter(lambda x: not n % x, range(2,int(round(sqrt(n)))+1))) | |
max(filter(is_prime, factors_of(600851475143))) |
This file contains 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
# http://projecteuler.net/problem=4 | |
def is_palindrome(n): | |
if str(n)[0] == str(n)[-1] and str(n)[1] == str(n)[-2] and str(n)[2] == str(n)[-3]: | |
return True | |
else: | |
return False | |
l = [] | |
for i in range(100,1000): | |
for j in range(100,1000): | |
l.append(i*j) | |
max(filter(is_palindrome, l)) |
This file contains 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
# https://projecteuler.net/problem=6 | |
sum(range(1,101))**2 - sum(map(lambda n: n**2, range(1,101))) |
This file contains 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
# https://projecteuler.net/problem=7 | |
is_prime = lambda n: not len(filter(lambda x: not n % x, range(2,int(round(sqrt(n)))+1))) | |
def nth_prime(n, c=1, p=2): | |
if c==n: | |
return p | |
else: | |
if is_prime(p+1): return nth_prime(n, c+1, p+1) | |
else: return nth_prime(n, c, p+1) | |
nth_prime(10001) # exceeds maximum recursion depth; use gist.github.com/jboynyc/6232278 instead |
This file contains 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
# https://projecteuler.net/problem=8 | |
def qp(i): | |
from operator import mul | |
return reduce(mul, map(int, list(i))) | |
s = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" | |
l = [] | |
for i in range(0,len(s)-4): l.append(s[i:i+5]) | |
p = map(qp, l) | |
l[p.index(max(p))] |
This file contains 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
# https://projecteuler.net/problem=9 | |
def is_py_trip((a, b, c)): | |
return True if (a*a + b*b == c*c) and (a < b < c) else False | |
import itertools | |
from operator import mul | |
r = filter(lambda t: True if sum(t)==1000 and is_py_trip(t) else False, itertools.combinations(xrange(1,998),3)) | |
reduce(mul,r[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment