maths.reverse_factorial_recursive

Functions

reverse_factorial_recursive(→ int)

Return x such that x! == value, otherwise return -1.

Module Contents

maths.reverse_factorial_recursive.reverse_factorial_recursive(value: int, current_divisor: int = 1) int

Return x such that x! == value, otherwise return -1.

The function divides value by 1, 2, 3, … recursively. If the repeated division reduces value exactly to 1, the factorial root x is (current_divisor - 1). If the division ever has a remainder, no integer x exists and the function returns -1.

Parameters

value: The positive integer to test (candidate factorial value). current_divisor: The current divisor used while reducing value (default is 1).

Returns

The factorial root (x) if x! == value, otherwise -1.

Examples

>>> reverse_factorial_recursive(120)
5
>>> reverse_factorial_recursive(24)
4
>>> reverse_factorial_recursive(150)
-1
>>> reverse_factorial_recursive(1)
1
>>> reverse_factorial_recursive(2)
2