0

How can I make the following script more intelligent, like a human would do it?

(avoiding the redundant calculations of the full factorials)

def combinations_without_repetition(n, r):
    return (factorial(n) / (factorial(r) * (factorial(n - r))))
combinations_without_repetition(10, 5)

(I get errors: Too large values.)

CDJB
  • 14,043
  • 5
  • 29
  • 55
KaPy3141
  • 161
  • 14

1 Answers1

1

You can use scipy.special.comb for this. Switching exact to True gives the exact answer as an integer at the cost of speed.

>>> from scipy.special import comb
>>> comb(10, 5, exact=False, repetition=False)
252.0
>>> comb(10, 5, exact=True, repetition=False)
252
CDJB
  • 14,043
  • 5
  • 29
  • 55