python求和_Python程序查找特殊求和系列的解决方案

python求和

We are going to design a special sum series function which has following characteristics:

我们将设计一个特殊的求和系列函数,该函数具有以下特征:

    f(0) = 0
    f(1) = 1
    f(2) = 1
    f(3) = 0
    f(x) = f(x-1) + f(x-3)

Python solution of the above sum series

上述sum系列的Python解决方案

# function to find the sum of the series
def summ(x):
    if x == 0:
        return 0
    if x == 1:
        return 1
    if x == 2:
        return 1
    if x == 3:
        return 0
    
    else:
        return summ(x-1) + summ(x-4)

# main code
if __name__ == '__main__':
    # finding the sum of the series till given value of x
    print("summ(0) :", summ(0))
    print("summ(1) :", summ(1))
    print("summ(2) :", summ(2))
    print("summ(3) :", summ(3))
    print("summ(10):", summ(10))
    print("summ(14):", summ(14))

Output

输出量

summ(0) : 0
summ(1) : 1
summ(2) : 1
summ(3) : 0
summ(10): 5
summ(14): 17


翻译自: https://www.includehelp.com/python/find-the-solution-of-a-special-sum-series.aspx

python求和

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值