首页 > 其他分享 >Grasshopper - Summation

Grasshopper - Summation

时间:2023-03-27 12:55:05浏览次数:36  
标签:return sum Grasshopper number summation num Summation

Summation
Write a program that finds the summation of every number from 1 to num. The number will always be a positive integer greater than 0.
For example (Input -> Output):

2 -> 3 (1 + 2)
8 -> 36 (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)

Solution

# mine
def summation(num):
    sum = 0
    
    while num > 0:
        sum += num
        num -= 1
    
    return sum
# others
def summation(num):
    return sum(range(1,num+1))

标签:return,sum,Grasshopper,number,summation,num,Summation
From: https://www.cnblogs.com/artwalker/p/17261180.html

相关文章

  • Grasshopper - Personalized Message
    InstructionsCreateafunctionthatgivesapersonalizedgreeting.Thisfunctiontakestwoparameters:nameandowner.Useconditionalstoreturntheproperme......