# Pls I have a question on python

No one likes homework, but your math teacher has given you an assignment to find the sum of the first N numbers. Let’s save some time by creating a program to do the calculation for you! Take a number N as input and output the sum of all numbers from 1 to N (including N). Sample Input 100 Sample Output 5050 Explanation: The sum of all numbers from 1 to 100 is equal to 5050. How can I solve it pls

1/16/2022 12:59:57 AM

Oyarekhua Sandra27 Answers

New AnswerOyarekhua Sandra , here is your code with comments : N = int (input ()) S = 0 for i in range (N +1): # we need to add 1 to the input value, as the upper bound of range is not included S+= i # we need to add "i" not "N" #N-=1 # no need to do this print (S)

n = int(input()) print(sum(range(n+1)) Consider using built-in functions instead of creating user-defined function where as possible. Built-in functions are faster than user-defined function. Because pythons built-in functions are written in C

Okay, write it again and save it. Then post the link here and we can help. Can't give proper guidence without seeing how you write the solution.

'''When we are learning, since coding is practice, a divergence aproach is to find as many different ways of doing the same thing and after some experience decide on solid understanding what way is best for a given situation. We can for example use recursion to avoid loops:''' n = int ( input ()) def add (n): if n == 1: return n else: return n + add(n-1) print (add(n)) '''I believe that when we are learning, we might try as much different ways as possible to explore, but good advice and efficient code is very important too so we sharp our understanding. If you are humble enough to receive smart critics you are benefiting the community. I liked all the comments and advices in this post'''

Lothar, as might not be any advantage not using i, my code is only better for N <= 2 as you do (2*N + 2) operations and mine do 3N operations. For memory manipulation also cannot see advantage for mine so your code is more efficient for N>=3. Well done.

#You can use directly sum method to calculate the sum, but if u want to understand the logic behind that then it is for you :- n=int(input()) sum=0 for i in range(1,n+1): sum+=i. # sum = sum+i print(sum) Oyarekhua Sandra I hope u will get concept by this code....

Neeraj Roy: I inspired me in your code to optimize. Once someone got your instructive idea, it could be implemented the following code: n = int ( input ( )) for i in range (1, n): n += i print ( n ) we declared only 1 variable and do only n iterations. Of course it is not much readable, but faster. I like to explore many possibilities together and dislike the "right way". Anyway your code is very instructive.

Neeraj Roy yes! Your code is good practice as corporations prefer clear and instructive coding. My example only makes sense together with your code to explore ideas as it is kinda of obscure and meaningless. Good job.

if your N = 5 then you have to do 1+2+3+4+5 = 15 output = 15 so this is your question.

Christina Ricci Exactly, This code really decrease the complexities, but bro I just tried to understand the logic behind this concept step by step, that's why i took 2 variables, so that no one will confuse that what's happening in this code...

Every has given diverse answer but here s mine too a=int(input("Enter the number : ")) for i in range(1,a): a=a+ i print(a) Somewhat long

Eh!, we don't need to add user prompt. well, the shortest (and the fastest) method to do it is to just print that number using this formula: (N(N+1))/2 but it won't effect in small numbers, you can use any method (till some large numbers are taken as inputs).