1. 程式人生 > >求1+2+3+...+n python

求1+2+3+...+n python

遞迴

# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        # write code here
        return n and n + self.Sum_Solution(n-1)

reduce()函式

# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        # write code here
        def f(n,m):
            return n+m
        return reduce(f,list(range(1,n+1)))