Skip to content

Commit be223d3

Browse files
committed
Day 27: try dp
1 parent d215d64 commit be223d3

File tree

1 file changed

+86
-37
lines changed

1 file changed

+86
-37
lines changed
Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,97 @@
1-
def countingBinomialCoefficient(num, prime):
2-
prevRow = []
3-
count = 0
4-
5-
for rowNum in xrange(num+1):
6-
row = [1] * (rowNum+1)
7-
8-
for i in xrange(len(prevRow)-1):
9-
row[i+1] = prevRow[i] + prevRow[i+1]
10-
11-
for j in xrange(len(row)):
12-
if row[j] % prime != 0:
13-
count += 1
14-
prevRow = row
15-
16-
return count
17-
18-
def testCountingBinomialCoefficient():
1+
class BinomialCoefficient(object):
2+
def __init__(self):
3+
self.coefficients = {}
4+
self.notDivisible = {}
5+
6+
# returns the binomial expansion coefficient for TOTAL choose SUBGROUP
7+
# aka C(total, subgroup)
8+
def getBinomialCoefficient(self, total, subgroup):
9+
if (total, subgroup) in self.coefficients:
10+
return self.coefficients[(total, subgroup)]
11+
12+
if total == subgroup or subgroup == 0:
13+
self.coefficients[(total, subgroup)] = 1
14+
return 1
15+
16+
return self.getBinomialCoefficient(total-1, subgroup) + self.getBinomialCoefficient(total-1, subgroup-1)
17+
18+
# returns the number of coefficients in Pascal's triangle up to
19+
# the NUMth row of the triangle that are not divisible by PRIME
20+
def binomialCoefficientsNotDivisibleByPrime(self, num, prime):
21+
if num == 0:
22+
self.notDivisible[prime] = {0: 1}
23+
return 1
24+
25+
if prime in self.notDivisible and num in self.notDivisible[prime]:
26+
return self.notDivisible[prime][num]
27+
28+
notDivisibleThisRow = len([i for i in xrange(num+1) if self.getBinomialCoefficient(num, i) % prime != 0])
29+
30+
self.notDivisible[prime][num] = self.binomialCoefficientsNotDivisibleByPrime(num-1, prime) + notDivisibleThisRow
31+
return self.notDivisible[prime][num]
32+
33+
def testGetBinomialCoefficient(binomialCalculator):
34+
b = binomialCalculator
35+
36+
assert b.getBinomialCoefficient(0, 0) == 1
37+
38+
assert b.getBinomialCoefficient(1, 0) == 1
39+
assert b.getBinomialCoefficient(1, 1) == 1
40+
41+
assert b.getBinomialCoefficient(2, 0) == 1
42+
assert b.getBinomialCoefficient(2, 1) == 2
43+
assert b.getBinomialCoefficient(2, 2) == 1
44+
45+
assert b.getBinomialCoefficient(3, 0) == 1
46+
assert b.getBinomialCoefficient(3, 1) == 3
47+
assert b.getBinomialCoefficient(3, 2) == 3
48+
assert b.getBinomialCoefficient(3, 3) == 1
49+
50+
assert b.getBinomialCoefficient(4, 0) == 1
51+
assert b.getBinomialCoefficient(4, 1) == 4
52+
assert b.getBinomialCoefficient(4, 2) == 6
53+
assert b.getBinomialCoefficient(4, 3) == 4
54+
assert b.getBinomialCoefficient(4, 4) == 1
55+
56+
assert b.getBinomialCoefficient(5, 0) == 1
57+
assert b.getBinomialCoefficient(5, 1) == 5
58+
assert b.getBinomialCoefficient(5, 2) == 10
59+
assert b.getBinomialCoefficient(5, 3) == 10
60+
assert b.getBinomialCoefficient(5, 4) == 5
61+
assert b.getBinomialCoefficient(5, 5) == 1
62+
63+
def testCountBinomialCoefficientsNotDivisibleByPrime(binomialCalculator):
64+
b = binomialCalculator
65+
1966
# prime = 2
20-
assert countingBinomialCoefficient(0, 2) == 1
21-
assert countingBinomialCoefficient(1, 2) == 3
22-
assert countingBinomialCoefficient(2, 2) == 5
23-
assert countingBinomialCoefficient(3, 2) == 9
24-
assert countingBinomialCoefficient(4, 2) == 11
25-
assert countingBinomialCoefficient(5, 2) == 15
67+
assert b.binomialCoefficientsNotDivisibleByPrime(0, 2) == 1
68+
assert b.binomialCoefficientsNotDivisibleByPrime(1, 2) == 3
69+
assert b.binomialCoefficientsNotDivisibleByPrime(2, 2) == 5
70+
assert b.binomialCoefficientsNotDivisibleByPrime(3, 2) == 9
71+
assert b.binomialCoefficientsNotDivisibleByPrime(4, 2) == 11
72+
assert b.binomialCoefficientsNotDivisibleByPrime(5, 2) == 15
2673

2774
# prime = 5
28-
assert countingBinomialCoefficient(0, 5) == 1
29-
assert countingBinomialCoefficient(1, 5) == 3
30-
assert countingBinomialCoefficient(2, 5) == 6
31-
assert countingBinomialCoefficient(3, 5) == 10
32-
assert countingBinomialCoefficient(4, 5) == 15
33-
assert countingBinomialCoefficient(5, 5) == 17
34-
assert countingBinomialCoefficient(6, 5) == 21
75+
assert b.binomialCoefficientsNotDivisibleByPrime(0, 5) == 1
76+
assert b.binomialCoefficientsNotDivisibleByPrime(1, 5) == 3
77+
assert b.binomialCoefficientsNotDivisibleByPrime(2, 5) == 6
78+
assert b.binomialCoefficientsNotDivisibleByPrime(3, 5) == 10
79+
assert b.binomialCoefficientsNotDivisibleByPrime(4, 5) == 15
80+
assert b.binomialCoefficientsNotDivisibleByPrime(5, 5) == 17
81+
assert b.binomialCoefficientsNotDivisibleByPrime(6, 5) == 21
3582

3683
# prime = 7
37-
assert countingBinomialCoefficient(5, 7) == 21
84+
assert b.binomialCoefficientsNotDivisibleByPrime(5, 7) == 21
3885

39-
# BIG DATA (these don't finish running with the brute force implementation)
40-
# assert countingBinomialCoefficient(999999999, 7) == 2129970655314432
41-
# assert countingBinomialCoefficient(879799878, 17) == 6026990181372288
42-
# assert countingBinomialCoefficient(879799878, 19) == 8480245105257600
86+
# BIG DATA
87+
assert b.binomialCoefficientsNotDivisibleByPrime(999999999, 7) == 2129970655314432
88+
assert b.binomialCoefficientsNotDivisibleByPrime(879799878, 17) == 6026990181372288
89+
assert b.binomialCoefficientsNotDivisibleByPrime(879799878, 19) == 8480245105257600
4390

4491
def main():
45-
testCountingBinomialCoefficient()
92+
b = BinomialCoefficient()
93+
testGetBinomialCoefficient(b)
94+
testCountBinomialCoefficientsNotDivisibleByPrime(b)
4695

4796
if __name__ == "__main__":
4897
main()

0 commit comments

Comments
 (0)