princeofsudan

ProjectEuler#53 Combinatoric selections

Mar 29th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. #There are exactly ten ways of selecting three from five, 12345:
  2.  
  3. #123, 124, 125, 134, 135, 145, 234, 235, 245, and 345
  4.  
  5. #In combinatorics, we use the notation, 5C3 = 10.
  6.  
  7. #In general, nCr = n!/(r!(n-r)!), where r <= n
  8.  
  9. #It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066.
  10.  
  11. #How many, not necessarily distinct, values of  nCr, for 1 <= n <= 100,
  12. #are greater than one-million?
  13.  
  14. def fact(n):
  15.     if n == 1 or n == 0:
  16.         return 1
  17.     return n*fact(n-1)
  18.  
  19. def nCr(n,r):
  20.     return fact(n)/(fact(r)*fact(n-r))
  21.  
  22. count = 0
  23. for n in range(1,101):
  24.     for r in range(1,n+1):
  25.         if nCr(n,r) > 1000000:
  26.             count += 1
  27. print count
Advertisement
Add Comment
Please, Sign In to add comment