MCPcopy Index your code
hub / github.com/python/cpython / py_factorial

Function py_factorial

Lib/test/test_math_integer.py:57–66  ·  view source on GitHub ↗

Factorial of nonnegative integer n, via "Binary Split Factorial Formula" described at http://www.luschny.de/math/factorial/binarysplitfact.html

(n)

Source from the content-addressed store, hash-verified

55 return partial_product(start, mid) * partial_product(mid, stop)
56
57def py_factorial(n):
58 """Factorial of nonnegative integer n, via "Binary Split Factorial Formula"
59 described at http://www.luschny.de/math/factorial/binarysplitfact.html
60
61 """
62 inner = outer = 1
63 for i in reversed(range(n.bit_length())):
64 inner *= partial_product((n >> i + 1) + 1 | 1, (n >> i) + 1 | 1)
65 outer *= inner
66 return outer << (n - count_set_bits(n))
67
68
69class IntMathTests(unittest.TestCase):

Callers 1

test_factorialMethod · 0.85

Calls 3

partial_productFunction · 0.85
count_set_bitsFunction · 0.85
bit_lengthMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…