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
(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)) |
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
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 |
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
def triangle_numbers(): | |
n = 1 | |
while True: | |
yield n * (n + 1) / 2 | |
n += 1 | |
def prime_factorization(n): | |
candidate = 2 | |
while n > 1: | |
factors = [] |
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
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: |
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
import itertools | |
products = set() | |
for (a, b) in itertools.product(range(2, 101), range(2, 101)): | |
products.add(a ** b) | |
print len(products) |
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
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) |
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
current = 0 | |
step = 2 | |
sum = 0 | |
limit = 1001 ** 2 | |
spiral = range(1, limit + 1) | |
while current < limit: | |
for i in range(4): |
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
public static void Main(string[] args) | |
{ | |
for (var n = 286L; ; n++) | |
{ | |
var triangleNumber = TriangleNumber(n); | |
if (IsPentagonal(triangleNumber) && IsHexagonal(triangleNumber)) | |
{ | |
Console.WriteLine(triangleNumber); | |
break; | |
} |
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
from itertools import permutations | |
MILLIONTH = 10 ** 6 - 1 | |
print reduce(lambda x, y: str(x) + str(y), list(permutations(range(10), 10))[MILLIONTH]) |
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
def is_even(n): | |
return n % 2 == 0 | |
def mod_exp(base, exponent, modulus): | |
if exponent == 0: | |
return 1 | |
temp = mod_exp(base, exponent / 2, modulus) | |
if is_even(exponent): |