|
1 | | -def reverse_factorial(n: int, divisor: int = 1) -> int: |
| 1 | +def reverse_factorial_recursive(value: int, current_divisor: int = 1) -> int: |
2 | 2 | """ |
3 | | - Return x such that x! equals the given n, or -1 if no such integer exists. |
| 3 | + Return x such that x! == value, otherwise return -1. |
4 | 4 |
|
5 | | - This function recursively divides the number `n` by successive integers |
6 | | - starting from 1 until it reaches 1. If the division completes exactly at 1, |
7 | | - the last divisor - 1 is the factorial root. Otherwise, return -1. |
| 5 | + The function divides `value` by 1, 2, 3, ... recursively. If the repeated |
| 6 | + division reduces `value` exactly to 1, the factorial root x is |
| 7 | + (current_divisor - 1). If the division ever has a remainder, no integer x |
| 8 | + exists and the function returns -1. |
8 | 9 |
|
9 | 10 | Parameters |
10 | 11 | ---------- |
11 | | - n : int |
12 | | - The number whose factorial root is to be found. |
13 | | - divisor : int, optional |
14 | | - The current divisor used in the recursion (default is 1). |
| 12 | + value : int |
| 13 | + The positive integer to test (candidate factorial value). |
| 14 | + current_divisor : int, optional |
| 15 | + The current divisor used while reducing `value` (default is 1). |
15 | 16 |
|
16 | 17 | Returns |
17 | 18 | ------- |
18 | 19 | int |
19 | | - The factorial root if it exists, otherwise -1. |
| 20 | + The factorial root (x) if x! == value, otherwise -1. |
20 | 21 |
|
21 | 22 | Examples |
22 | 23 | -------- |
23 | | - >>> reverse_factorial(120) |
| 24 | + >>> reverse_factorial_recursive(120) |
24 | 25 | 5 |
25 | | - >>> reverse_factorial(24) |
| 26 | + >>> reverse_factorial_recursive(24) |
26 | 27 | 4 |
27 | | - >>> reverse_factorial(150) |
| 28 | + >>> reverse_factorial_recursive(150) |
28 | 29 | -1 |
29 | | - >>> reverse_factorial(1) |
| 30 | + >>> reverse_factorial_recursive(1) |
30 | 31 | 1 |
31 | | - >>> reverse_factorial(720) |
32 | | - 6 |
| 32 | + >>> reverse_factorial_recursive(2) |
| 33 | + 2 |
33 | 34 | """ |
34 | | - if n < 1: |
35 | | - raise ValueError("Input must be a positive integer.") |
36 | | - if n == 1: |
37 | | - return divisor |
38 | | - if n % divisor != 0: |
| 35 | + if not isinstance(value, int): |
| 36 | + raise TypeError("value must be an integer") |
| 37 | + if not isinstance(current_divisor, int): |
| 38 | + raise TypeError("current_divisor must be an integer") |
| 39 | + |
| 40 | + if value < 1: |
| 41 | + raise ValueError("value must be a positive integer") |
| 42 | + |
| 43 | + # Special-case: initial call with value == 1 should return 1 (since 1! = 1). |
| 44 | + if value == 1 and current_divisor == 1: |
| 45 | + return 1 |
| 46 | + |
| 47 | + # If value reduced to 1 during recursion, the factorial root is divisor - 1. |
| 48 | + if value == 1: |
| 49 | + return current_divisor - 1 |
| 50 | + |
| 51 | + # If not divisible by the current divisor, it's not a factorial number. |
| 52 | + if value % current_divisor != 0: |
39 | 53 | return -1 |
40 | | - return reverse_factorial(n // divisor, divisor + 1) |
| 54 | + |
| 55 | + return reverse_factorial_recursive(value // current_divisor, current_divisor + 1) |
0 commit comments