-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathproblem15.py
More file actions
48 lines (40 loc) · 1016 Bytes
/
problem15.py
File metadata and controls
48 lines (40 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# combination
# I solved using (very ugly) brute force to generate the first 5 terms
# in the sequence, which I then entered into Wolfram Alpha.
# http://mathworld.wolfram.com/BinomialCoefficient.html
# the brute force solution is at the bottom
def fac(n):
if n > 1:
return n * fac(n - 1)
else:
return 1
# binomial
print fac(40)/(fac(20)**2)
# brute force (it's slow). guess what i'm doing:
import string
def s2b (s):
n = int(s)
c = ''
while n > 0:
c = str(n % 2) + c
n /= 2
return c
def b2s (b):
return string.atoi(b, 2)
def max_paths(length_of_side):
i = 0
c = 0
maximum = ''
for j in range(0, length_of_side):
maximum = '1' + maximum + '0'
maximum = b2s(maximum)
while i <= maximum:
b = s2b(i)
b = b.zfill(2 * length_of_side)
if b.count('0') == b.count('1'):
c += 1
i += 1
return c
print "First 5 terms in sequence:"
for i in range(1, 6):
print max_paths(i)