Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Shouldn't that be

   print ((n-1)*n)/2


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.)


Ah 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'.


Probably much faster, but also doing half the work yourself. It'll depend on the situation which is the better solution.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: