I find the smallest divisor of a number with this code:
def smallestDisvisor(n):
return factor(n,2)
def factor(n,divisor):
if (square(divisor) - n > 0):
return n
elif (n % divisor == 0):
return divisor
else:
return factor(n,divisor + 1)
print(smallestDisvisor(32452843))
However, when I run this with a large enough value, I get:
RecursionError: maximum recursion depth exceeded
[Finished in 0.5s with exit code 1]
I do not understand the recursion error. Isn't this code implementing an iterative process?
factor(32452843,2) -> factor(32452843,3) -> factor(32452843,4)...
If not, how can I implement an iterative process for this algorithm?