ZhangZhihui's Blog  

 

# Note that this function is already available in the math module as fsum().
def number_sum(n):
    if n == 0:
        return 0
    else:
        return n + number_sum(n - 1)


if __name__ == "__main__":
    from timeit import Timer
    t = Timer(
        "number_sum(50)",
        "from __main__ import number_sum",
    )
    print("Time: ", t.timeit())

 

posted on 2024-08-17 10:05  ZhangZhihuiAAA  阅读(2)  评论(0编辑  收藏  举报