def compute(): square_free = set() for n in range(1, 51): for k in range(int(n/2)+1): if all(choose(n,k) % (p**2) != 0 for p in prime_factors): square_free.add(choose(n,k)) return sum(square_free)
def only_red_and_orange(n): if n == min_num_colors: return 1 return choose(n * balls_per_color, balls_to_draw) - sum([ choose(n, x) * only_red_and_orange(x) for x in xrange(min_num_colors, n) ])
def run(): total = 0 for n in range(23, 101): subtotal = 0 for r in range(1, n): if euler.choose(n, r) >= 1000000: break subtotal += 1 total += n - 2*subtotal - 1 return total
def naive_transition_matrix( n ): states = [ tuple(state) for state in euler.permutations( range(1,n+1) ) ] m = len(states) mat = numpy.zeros( (m,m) ) state2id = dict(zip(states, range(len(states)))) id2state = dict(zip(range(len(states)), states)) choices = list(euler.choose(range(n), 2)) for u in states[1:]: for i, j in choices: v = list(u) v[i], v[j] = v[j], v[i] v = tuple(v) mat[ state2id[u], state2id[v] ] = 1.0/len(choices) return numpy.matrix(mat)
def run(): primes = euler.primesUpTo(18097) pascals = set() for n in range(0, 50): for k in range(0, n//2+1): pascals.add(euler.choose(n, k)) total = 0 for pascal in pascals: squarefree = True for prime in primes: if pascal % (prime*prime) == 0: squarefree = False break if squarefree: total += pascal return total
def only_red_and_orange(n): if n == min_num_colors: return 1 return choose(n * balls_per_color, balls_to_draw) - sum([choose(n, x) * only_red_and_orange(x) for x in xrange(min_num_colors, n)])
from euler import choose colors_in_rainbow = 7 balls_to_draw = 20 balls_per_color = 10 total_balls = balls_per_color * colors_in_rainbow min_num_colors = balls_to_draw / balls_per_color # How many ways can we choose balls_to_draw balls from our collection, # selecting from exactly n _specified_ colors def only_red_and_orange(n): if n == min_num_colors: return 1 return choose(n * balls_per_color, balls_to_draw) - sum([choose(n, x) * only_red_and_orange(x) for x in xrange(min_num_colors, n)]) def only_n_distinct_colors(n): return choose(colors_in_rainbow, n) * only_red_and_orange(n) print float(sum([a * only_n_distinct_colors(a) for a in range(min_num_colors, colors_in_rainbow + 1)])) / choose(total_balls, balls_to_draw)
] def constr(a, b, v1, v2): if a in v1 and b in v2: return True def row_or(row, v1, v2): for a, b in row: if constr(a, b, v1, v2): return True return False def col_and(cols, v1, v2): for row in cols: if row_or(row, v1, v2) == False: return False return True combs = set() faces = list(euler.choose(range(10), 6)) for d1 in faces: for d2 in faces: if col_and(lst, d1, d2): a = tuple(d1) b = tuple(d2) ab = (a, b) if b < a: ab = (b, a) combs.add(ab) print len(combs)
import euler size = 20 print euler.choose(2*size, size)
def euler53(): print(sum(1 for n in range(1, 101) for r in range(n) if choose(n, r) > 1000000))
def euler15(): grid_size = 20 print(choose(grid_size * 2, grid_size))
def non_bouncy(n): return choose(n+10, n) + choose(n+9, 9) - 10*n - 2
from decimal import Decimal from euler import choose,product ways = [Decimal(0) for _ in range(0,8)] for r in range(0,11): for o in range(0,min(11, 21-r)): for y in range(0,min(11,21-r-o)): for g in range(0,min(11,21-r-o-y)): for b in range(0,min(11,21-r-o-y-g)): for i in range(0,min(11,21-r-o-y-g-b)): v = 20 - sum([r,o,y,g,b,i]) roygbiv = [r,o,y,g,b,i,v] count = sum(1 for x in roygbiv if x > 0) different_ways = product(choose(10,x) for x in roygbiv) ways[count] += Decimal(different_ways) tot_ways = sum(ways) EV = sum(Decimal(x) * (ways[x] / tot_ways) for x in range(2,8)) print(EV)
def compute(): return len([(n,r) for n in range(101) for r in range(1,n) if choose(n,r) > 10**6])
def only_n_distinct_colors(n): return choose(colors_in_rainbow, n) * only_red_and_orange(n)
from euler import choose print choose(40, 20)
from euler import choose colors_in_rainbow = 7 balls_to_draw = 20 balls_per_color = 10 total_balls = balls_per_color * colors_in_rainbow min_num_colors = balls_to_draw / balls_per_color # How many ways can we choose balls_to_draw balls from our collection, # selecting from exactly n _specified_ colors def only_red_and_orange(n): if n == min_num_colors: return 1 return choose(n * balls_per_color, balls_to_draw) - sum([ choose(n, x) * only_red_and_orange(x) for x in xrange(min_num_colors, n) ]) def only_n_distinct_colors(n): return choose(colors_in_rainbow, n) * only_red_and_orange(n) print float( sum([ a * only_n_distinct_colors(a) for a in range(min_num_colors, colors_in_rainbow + 1) ])) / choose(total_balls, balls_to_draw)
if a in v1 and b in v2: return True def row_or(row, v1, v2): for a, b in row: if constr(a, b, v1, v2): return True return False def col_and(cols, v1, v2): for row in cols: if row_or(row, v1, v2) == False: return False return True combs = set() faces = list(euler.choose(range(10), 6)) for d1 in faces: for d2 in faces: if col_and(lst, d1, d2): a = tuple(d1) b = tuple(d2) ab = (a, b) if b < a: ab = (b, a) combs.add(ab) print len(combs)
#!/usr/bin/env python """ Problem 015: Starting in the top left corner of a 2 x 2 grid, there are 6 routes (without backtracking) to the bottom right corner. How many routes are there through a 20 x 20 grid? """ from euler import choose N = 20 print(choose(2 * N, N)) # Central binomial coefficient
def run(): total = 0 depth = 100 for n in range(1, 10): total += euler.choose(depth+n-1, n) + euler.choose(depth+n, n+1) return total-9*depth
SIZE = 20 grid = [[1 for _ in range(SIZE + 1)] for _ in range(SIZE + 1)] for i in range(2, (SIZE + 1) * 2): for x in range(1, i): y = i - x if x >= SIZE + 1 or y >= SIZE + 1: continue grid[x][y] = grid[x - 1][y] + grid[x][y - 1] print grid[SIZE][SIZE] ### # C(40, 20) = 20! / 40!(40-20)! import euler print euler.choose(SIZE * 2, SIZE)
def euler53(): print( sum(1 for n in range(1, 101) for r in range(n) if choose(n, r) > 1000000))
def EN(k): if k == 0: return rational(0) return rational(1,(2**k-1)) * sum( [choose(k, i,1) * (EN(k - i) + 1) for i in range(1,k + 1)], rational(1))
def compute(): return 7.0 * (1 - 1.0 * choose(60, 40) / choose(70, 50))
import euler size = 20 print euler.choose(2 * size, size)
from euler import choose, squarefree, primes N = 51 ps = primes(int(choose(N-1,(N-1)//2)**.5)) print("got primes") sfrees = set() for n in range(0,N): for k in range(0,(n+3)//2): coeff = choose(n,k) if squarefree(coeff,ps): sfrees.add(coeff) print(sum(sfrees))
def run(): return euler.choose(40, 20)