python - Functions throws error on recursing large numbers -
i'm using python 2.7.3 , have function:
def f(n): if n == 0: return 0 else: return (n % 3 == 0 or n % 5 == 0) * n + f(n - 1) f(999)
it works until f(993), not f(999). when try, infinite amount of errors keep popping out. don't it. can tell me what's wrong?
edit: answers. guess i'm better off using iteration in python.
better off using iterative:
def f(n): s = 0 while n > 0: s += (n%3==0 or n%5==0)*n n -= 1 return s print f(999)
which outputs:
233168
also 1 write sum
:
def f(n): return sum((n%3==0 or n%5==0)*n n in reversed(range(1,n+1)))
Comments
Post a Comment