Ask Question

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's the sum of the integers from 1 to 1; 6 is a triangular number because it's 1+2+3=6. Given the non-negative integers m and n (with m < n), create a list of the triangular numbers between (and including) m and n. Thus if m is 3 and n is 20, the list would be: [3, 6, 10, 15]. Associate the list with the variable triangulars.

+1
Answers (1)
  1. 5 October, 00:16
    0
    m = 3 n = 20 triList = [] current = 0 for i in range (1, n + 1) : current = current + i if (current > = m and current < = n) : triList. append (current) print (triList)

    Explanation:

    The solution code is written in Python 3.

    Firstly, create variable m and n and set the value 3 and 20 to the variables (Line 1 - 2)

    Create a triangle number list (Line 4) and another variable current to hold the value of current total of triangle number (Line 5).

    Create a for loop and iterate through the number between m and n (Line 6). Calculate the current total of triangle number (Line 7) and proceed to check if the current triangle number is bigger or equal to m and smaller and equal to n, add the current triangle number to triList (Line 8-9).

    Print the triList (Line 11) and we shall get [3, 6, 10, 15]
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “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's the ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers