Skip to content

Instantly share code, notes, and snippets.

View clementi's full-sized avatar
🎵
NP: Piano Sonata No. 1 in F-Sharp Minor, Op… (3:48/5:21)

Jeff Pratt clementi

🎵
NP: Piano Sonata No. 1 in F-Sharp Minor, Op… (3:48/5:21)
View GitHub Profile
@clementi
clementi / euler-29.py
Created February 9, 2011 23:09
Project Euler Problem #29 Solution
products = set()
for pair in [(a, b) for a in range(2, 101) for b in range(2, 101)]:
products.add(pair[0] ** pair[1])
print len(products)
@clementi
clementi / euler-56.py
Created February 10, 2011 03:31
Project Euler Problem #56 Solution
from itertools import *
pairs = product(range(1, 100), range(1, 100))
exponentials = imap(lambda pair: pair[0] ** pair[1], pairs)
strings = imap(lambda exponential: str(exponential), exponentials)
sums = imap(lambda string: sum(map(lambda digit: int(digit), string)), strings)
print max(sums)
@clementi
clementi / euler-28.py
Created February 10, 2011 03:35
Project Euler Problem #28 Solution
current = 0
step = 2
sum = 0
limit = 1001 ** 2
spiral = range(1, limit + 1)
while current < limit:
for i in range(4):
if current > limit:
@clementi
clementi / euler-53.py
Created February 12, 2011 02:25
Project Euler Problem #53 Solution
import operator
def factorial(n):
return 1 if n == 0 else reduce(operator.mul, range(1, n + 1))
def combo(n, r):
return factorial(n) / (factorial(r) * factorial(n - r))
count = 0
@clementi
clementi / euler-40.py
Created February 12, 2011 02:49
Project Euler Problem #40 Solution
digits = ""
for n in range(1, 1000001):
digits += str(n)
product = 1
for i in range(7):
product *= int(digits[10 ** i - 1])
print product
(defn prime-factorization-aux [n candidate]
(if (= n 1)
'()
(if (= (mod n candidate) 0)
(cons candidate (prime-factorization-aux (/ n candidate) candidate))
(prime-factorization-aux n (+ candidate 1)))))
(defn prime-factorization [n]
(prime-factorization-aux n 2))
@clementi
clementi / prime-factorization.py
Created February 14, 2011 05:06
Prime Factorization Algorithm
def prime_factorization(n):
candidate = 2
while n > 1:
factors = []
while n % candidate == 0:
factors.append(candidate)
n /= candidate
if factors:
yield factors
candidate += 1
@clementi
clementi / euler-12.py
Created February 14, 2011 05:09
Project Euler Problem #12 Solution
def triangle_numbers():
n = 1
while True:
yield n * (n + 1) / 2
n += 1
def prime_factorization(n):
candidate = 2
while n > 1:
factors = []
@clementi
clementi / euler-14.py
Created February 16, 2011 02:37
Project Euler Problem #14 Solution
def is_even(n):
return n % 2 == 0
def collatz_length(n):
length = 0
while n > 1:
length += 1
if is_even(n):
n /= 2
else:
@clementi
clementi / euler-24.py
Created February 16, 2011 02:42
Project Euler Problem #24 Solution
from itertools import permutations
MILLIONTH = 10 ** 6 - 1
print reduce(lambda x, y: str(x) + str(y), list(permutations(range(10), 10))[MILLIONTH])