202. Sum to N
Foundation1000 ms256 MBSolved by 0%
Print the sum 1 + 2 + 3 + ... + n.
Write it recursively: the sum up to n is n plus the sum up to n - 1.
Then look at the constraint. A recursive call needs a stack frame, and a million of them is more than most languages will give you, so the recursive version is the one to think in and not necessarily the one to submit.
Constraints - `1 ≤ n ≤ 1000000`
Input
A single line containing one integer n.
Output
Print one integer, the sum from 1 to n.
Input5
Output15
Noteis 1+2+3+4+5, checkable by hand
Input1
Output1
Noteis the base case, where the answer is n itself
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.