explain this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

explain this code

#sum of consecutive number N = int(input()) sum = int(N*(N+1)/2) print (sum)

18th Aug 2022, 8:38 PM
Amar Ghosh
Amar Ghosh - avatar
4 Answers
+ 3
That looks very simple, can you share what you think it means?
18th Aug 2022, 8:47 PM
Chris Coder
Chris Coder - avatar
+ 1
sum is a bit misleading name for the result of n*(n+1)/2. result would make more sense.
18th Aug 2022, 11:54 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 1
Apollo-Roboto it's the formula for the sum of all numbers from 1 to N.
19th Aug 2022, 4:19 AM
lion
lion - avatar
+ 1
Sd Amar it is a brilliant technique invented by Gauss as a youngster (age 10). The reasoning behind it is a little sophisticated. I explain it in the comments of the code posted here: https://code.sololearn.com/cxQki21ZMDIX/?ref=app
19th Aug 2022, 7:39 AM
Brian
Brian - avatar