Home > Mobile >  Simple recursion function in python
Simple recursion function in python

Time:10-09

Very new to coding and especially functions. I am trying to create a recursion function that starts at n == 1, divides n by two, and then continue to divide the next number by two. The math way is fn = fn-1/2. code is as follows

def half(n):
    return half(n/2)
half(1)

but i keep getting "maximum recursion depth exceeded"

help please?

CodePudding user response:

You need to set a limit to recursion, you can’t display infinite Something like when if n>0.00001: return half(n/2) else return n

CodePudding user response:

You're hitting a recursion limit in Python. Your function would lead to infinite recursion if Python didn't stop at the maximum recursion depth.

  • Related