0
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
27 odpowiedzi
+ 7
Oyarekhua 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)
+ 6
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
+ 4
N=int(input())
print(int(N*(N+1)/2))
#we can print that simply too
+ 3
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.
+ 3
'''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'''
+ 3
Clever witty 1 liner:
# python
(lambda n: n * (n + 1) / 2) (int(input()))
+ 2
Thanks for giving us the whole question. Please provide the code you've attempted.
+ 2
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.
+ 2
#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....
+ 2
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.
+ 2
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.
+ 2
Christina Ricci Yup 😊😊😊
+ 1
N = int (input ())
S = 0
for i in range (N):
S+=N
N-=1
print (S)
+ 1
Do you know this formula?:
(N(N+1))/2
If yes, then I have given you a big hint
+ 1
if your N = 5
then you have to do
1+2+3+4+5 = 15
output = 15
so this is your question.
+ 1
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...
+ 1
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
+ 1
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).
+ 1
That's user(asker) choice.