4 ms·
That's not entirely right. It should be: print ((n+1)*n)/2 (fold the sequence in half, which gives you 100 + 1 on one end and 50 + 51 on the other.)
by jeroen 17y ago
That's not entirely right. It should be:
print ((n+1)*n)/2
(fold the sequence in half, which gives you 100 + 1 on one end and 50 + 51 on the other.)
- jacquesm 17y agoAh right, sure. I took the 'n' to be the 100, not the 101. A little too quick on the submit there, I should have run it :) But anyway, that seems to be a damn sight faster than to run a loop to add a bunch of numbers in a fixed interval. Especially for large 'n'.
- jeroen 17y agoProbably much faster, but also doing half the work yourself. It'll depend on the situation which is the better solution.