Use python to define functions in the form of loop and recursion, and find the sum of 1~100.

1. Subject requirements:

Use python to define functions in the form of loop and recursion, and find the sum of 1~100.

2. Come on, show:

# coding=utf-8
# 循环
def for_sum(i):
    sum = 0
    j = 1
    while j <= i:
        sum += j
        j += 1
    return sum
# 递归
def fact_sum(i):
    if(i == 1):
        return 1
    return i + fact_sum(i - 1)
print(for_sum(100))
print(fact_sum(100))

3. Operation results:

I hope I can help everyone, I ask you if you want a like, will you give it, thank you all.
Copyright statement: The copyright of this article belongs to the author (@攻城狮小关) and CSDN. Welcome to reprint, but you must keep this paragraph without the author’s consent Statement, and provide the original link in an obvious place on the article page, otherwise the right to pursue legal responsibility is reserved.
It is not easy for everyone to write articles, please respect the fruits of labor~ 
Communication plus Q: 1909561302
Blog Park Address https://www.cnblogs.com/guanguan-com/

 

Guess you like

Origin blog.csdn.net/Mumaren6/article/details/108713688