0
Count the number of triangle numbers on a positive integer range
A triangle number is defined as the accumulated sum of the natural progression of positive integers. For example: (0+1 = 1, 0+1+2 = 3, 0+1+2+3 = 6, 0+1+2+3+4 = 10...). Write a C++ code for a program that would count the number of triangle numbers on a positive integer range. For instance: Start range: 7 End range: 20 Number of triangle number(s): 2
2 Respostas
+ 1
took me a while to solve it but heres my hint:
ask for start range
ask for end range
let count as the total
get triangle numbers from 1 to end range
if the triangle numbers is higher than or equal to start range
and triangle is lower or equal to end range
{
add count by 1
}
finally, print the count
sorry if its confusing im bad at explaining hehe 😅
0
your code has to be efficient enough to compute a lot of triangle numbers: computing sum of natural numbers with a loop at each iteration would be inefficient ^^
you could either use the gauss formula or start with the first one (1) and for next only add to it the next natural number (later seems to be the best in this case, as you only do one addition per iteration) ;)