Skip to content

Commit d5ba32f

Browse files
authored
Update reverse_factorial_recursive.py
1 parent 0bc3691 commit d5ba32f

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed
Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,55 @@
1-
def reverse_factorial(n: int, divisor: int = 1) -> int:
1+
def reverse_factorial_recursive(value: int, current_divisor: int = 1) -> int:
22
"""
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.
44
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.
89
910
Parameters
1011
----------
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).
1516
1617
Returns
1718
-------
1819
int
19-
The factorial root if it exists, otherwise -1.
20+
The factorial root (x) if x! == value, otherwise -1.
2021
2122
Examples
2223
--------
23-
>>> reverse_factorial(120)
24+
>>> reverse_factorial_recursive(120)
2425
5
25-
>>> reverse_factorial(24)
26+
>>> reverse_factorial_recursive(24)
2627
4
27-
>>> reverse_factorial(150)
28+
>>> reverse_factorial_recursive(150)
2829
-1
29-
>>> reverse_factorial(1)
30+
>>> reverse_factorial_recursive(1)
3031
1
31-
>>> reverse_factorial(720)
32-
6
32+
>>> reverse_factorial_recursive(2)
33+
2
3334
"""
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:
3953
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

Comments
 (0)