mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-28 07:21:07 +00:00
Combinatoric solution using Pascal's Triangle to Problem 15
This commit is contained in:
parent
301c907376
commit
ac14455ac0
20
Project Euler/Problem 15/sol1.py
Normal file
20
Project Euler/Problem 15/sol1.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
from __future__ import print_function
|
||||||
|
from math import factorial, ceil
|
||||||
|
|
||||||
|
def lattice_paths(n):
|
||||||
|
n = 2*n #middle entry of odd rows starting at row 3 is the solution for n = 1, 2, 3,...
|
||||||
|
k = n/2
|
||||||
|
|
||||||
|
return factorial(n)/(factorial(k)*factorial(n-k))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import sys
|
||||||
|
|
||||||
|
if len(sys.argv) == 1:
|
||||||
|
print(lattice_paths(20))
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
n = int(sys.argv[1])
|
||||||
|
print(lattice_paths(n))
|
||||||
|
except ValueError:
|
||||||
|
print('Invalid entry - please enter a number.')
|
Loading…
Reference in New Issue
Block a user