Sunday, March 6, 2022

Sum of Triangular Numbers Python Codewars

Task:

Your task is to return the sum of Triangular Numbers up-to-and-including the nth Triangular Number.

Triangular Number: "any of the series of numbers (1, 3, 6, 10, 15, etc.) obtained by continued summation of the natural numbers 1, 2, 3, 4, 5, etc."

[01]
02 [03]
04 05 [06]
07 08 09 [10]
11 12 13 14 [15]
16 17 18 19 20 [21]

e.g. If 4 is given: 1 + 3 + 6 + 10 = 20.

Triangular Numbers cannot be negative so return 0 if a negative number is given.

Solution:


def sum_triangular_numbers(n):
    sum_= 0
    temp_ = 0
    for i in range(1,n+1):
        temp_ += i
        sum_ += temp_
        
    return sum_

print(sum_triangular_numbers(4))

Sum of Triangular Numbers Solutions | Codewars



Related Posts


No comments :

Post a Comment