Commit 80029271 authored by Nayuki Minase's avatar Nayuki Minase

P15, P53, P113: Moved binomial() function into eulerlib.

parent 1dc03886
...@@ -51,3 +51,8 @@ def factorial(n): ...@@ -51,3 +51,8 @@ def factorial(n):
for i in range(1, n + 1): for i in range(1, n + 1):
result *= i result *= i
return result return result
def binomial(n, k):
assert n >= 0 and 0 <= k <= n
return factorial(n) // (factorial(k) * factorial(n - k))
...@@ -10,11 +10,7 @@ import eulerlib ...@@ -10,11 +10,7 @@ import eulerlib
def compute(): def compute():
return str(binomial(40, 20)) return str(eulerlib.binomial(40, 20))
def binomial(n, k):
return eulerlib.factorial(n) // (eulerlib.factorial(k) * eulerlib.factorial(n - k))
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -13,14 +13,10 @@ def compute(): ...@@ -13,14 +13,10 @@ def compute():
ans = 0 ans = 0
for n in range(1, 101): for n in range(1, 101):
for k in range(0, n + 1): for k in range(0, n + 1):
if binomial(n, k) > 1000000: if eulerlib.binomial(n, k) > 1000000:
ans += 1 ans += 1
return str(ans) return str(ans)
def binomial(n, k):
return eulerlib.factorial(n) // (eulerlib.factorial(k) * eulerlib.factorial(n - k))
if __name__ == "__main__": if __name__ == "__main__":
print(compute()) print(compute())
...@@ -35,16 +35,12 @@ import eulerlib ...@@ -35,16 +35,12 @@ import eulerlib
# (Technically, in the problem statement and this solution, "increasing" actually means "nondecreasing" and "decreasing" means "nonincreasing".) # (Technically, in the problem statement and this solution, "increasing" actually means "nondecreasing" and "decreasing" means "nonincreasing".)
def compute(): def compute():
DIGITS = 100 DIGITS = 100
increasing = binomial(DIGITS + 9, 9) - 1 increasing = eulerlib.binomial(DIGITS + 9, 9) - 1
decreasing = binomial(DIGITS + 10, 10) - (DIGITS + 1) decreasing = eulerlib.binomial(DIGITS + 10, 10) - (DIGITS + 1)
flat = DIGITS * 9 flat = DIGITS * 9
ans = increasing + decreasing - flat ans = increasing + decreasing - flat
return str(ans) return str(ans)
def binomial(n, k):
return eulerlib.factorial(n) // (eulerlib.factorial(k) * eulerlib.factorial(n - k))
if __name__ == "__main__": if __name__ == "__main__":
print(compute()) print(compute())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment