146k views
0 votes
a triangular number is a number that is the sum of the integers from 1 to some integer n. thus 1 is a triangular number because it is the sum of the integers from 1 to 1; 6 is a triangular number because 1 + 2 + 3 = 6.given the non-negative integer n, create a list of the first n triangular numbers. thus if n was 5, the list would be: {1, 3, 6, 10, 15}.assign the list to variable traingulars

User Danvk
by
5.2k points

1 Answer

5 votes

Answer:

def triangular_numbers(n):

triangulars = []

sum = 0

for i in range(1, n+1):

sum += i

triangulars.append(sum)

return triangulars

# Test the function

print(triangular_numbers(5)) # Output: [1, 3, 6, 10, 15]

Step-by-step explanation:

To create a list of the first n triangular numbers, you can use a for loop to iterate from 1 to n, and for each iteration, add the current number to the sum of the previous numbers.

This function will create a list of the first n triangular numbers and return it. You can then assign the returned list to the variable triangulars.

User Chere
by
6.0k points