0,1,3,6,10,15,21,... each term gets incremented in the order of natural numbers I tried to generate the nth of the series but ended with TLE here's my code
s=0
for(int i=1;i<=n;i++)
s=s+(i-1);
Can any anybody help me with a better algorithm.
0,1,3,6,10,15,21,... each term gets incremented in the order of natural numbers I tried to generate the nth of the series but ended with TLE here's my code
s=0
for(int i=1;i<=n;i++)
s=s+(i-1);
Can any anybody help me with a better algorithm.
Think the problem as solving recurrence of the form
T(n) = T(n-1) + n given T(0)=0
T(1) = T(0) + 1
T(2) = T(1) + 2
on solving recurrence you are going to obtain T(n) = n*(n+1)/2
This series, gives for n
the sum of the natural number from 0
to n
. There is a simple formula for calculating this (n * (n+1)) / 2
.