I have created a recursive function to calculate the total numbers divisible by n within a range of (start,end). The function works for smaller numbers except when they start getting larger for ex. start=1 and end=10**12 - 1 gives me an error saying that the maximum recursion depth has been exceeded. How do I fix my code to stop this error:
def count(start, end, n, tot=0):
if start > end:
return tot
else:
if start % n == 0:
tot += 1
return count(start + 1, end, n, tot)
start = 1
end = 10**12 - 1
n = 5
print(count(start, end, n))
(end - start) // n, with some checking for edge cases?end // n - (start - 1) // n.