简体   繁体   中英

Python recursive zeno() function

In python, I need to define a recursive function that takes a number a returns the sum 1/2^0 + 1/2^1 + 1/2^2 + 1/2^3 + ... + 1/2^n. I need to accomplish this without using a for or while loop. This is what i have tried.

def zeno(n):
    if n==0:
        return 1/1
    else:
        return float(1/1 + 1/2**zeno(n-1))
def zeno(n):
    if n==0: 
        return 1 #return 1 for base n==0 case, x ^ 0 is always 1
    else:
        return 0.5**n + zeno(n-1)  #calculate (1/2) ^ n + (1/2)^(n-1) recursively

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM