Exemplo n.º 1
0
import fnsEuler as fns
import pandas
import numpy as np
from time import time

start = time()

# 15. Lattice paths
# Number of ways n/2 things can be selected out of n, etc
n = 20
print "Euler 15:", fns.factorial(2*n)/(fns.factorial(n)**2),
print "T:", time() - start; start = time()

# 14. Collatz
max_num = int(1e6)

def next_collatz(n):
    if n % 2 == 0:
        return n / 2
    else:
        return 3 * n + 1

def collatz_array_len_hash(n, hash_table):
    while 1 > 0:
        if n in hash_table:
            return hash_table
        else:
            new_n = next_collatz(n)
            hash_table = collatz_array_len_hash(new_n, hash_table)
            hash_table[n] = hash_table[new_n] + 1
Exemplo n.º 2
0
import fnsEuler as fns
import pandas
import numpy as np
from time import time

start = time()

# 20. Factorial sum
print "Euler 20:", sum([int(x) for x in str(fns.factorial(100))]),
print "T:", time() - start; start = time()

# 19. Counting Sundays
start_year = 1901; end_year = 2000
count = 0; days_in_month = days_passed = 1;

for y in range(start_year, end_year + 1):
    for m in range(1, 13):
        if m == 4 or m == 6 or m == 9 or m == 11:
            days_in_month = 30
        elif m == 2:
            if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
                days_in_month = 29
            else:
                days_in_month = 28
        else:
            days_in_month = 31
        if days_passed % 7 == 0:
            count += 1
        days_passed += days_in_month

print "Euler 19:", count - 1,
Exemplo n.º 3
0
def comb(n, r):
    return fns.factorial(n)/(fns.factorial(n - r) * fns.factorial(r))