Factorial of nonnegative integer n, via "Binary Split Factorial Formula" described at http://www.luschny.de/math/factorial/binarysplitfact.html
(n)
| 55 | return partial_product(start, mid) * partial_product(mid, stop) |
| 56 | |
| 57 | def 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 | |
| 69 | class IntMathTests(unittest.TestCase): |
no test coverage detected
searching dependent graphs…