I'm trying to write a program to add up number from 1 to n. I've managed to get it to print the numbers several times but not add them all. It keeps on just adding two of the numbers.
My 1st attempt is:
def problem1_3(n): my_sum = 0 # replace this pass (a do-nothing) statement with your code while my_sum <= n: my_sum = my_sum + (my_sum + 1) print() print(my_sum)How can I fix this problem?
asked May 10, 2017 at 19:45
3
I don't understand why everyone keeps making everything complex. Here is my simple solution
n = int(input()) print(n * (n + 1) // 2)answered Feb 22, 2020 at 3:41
Akshat TamrakarAkshat Tamrakar
1,8272 gold badges11 silver badges16 bronze badges
2
You can do it with one line, where you create a list of integers from 0 to n and sums all the elements with sum function
def problem1_3(n): return sum(range(n+1))answered May 10, 2017 at 19:57
3
That's where I use "math" to solve problems like this. There is a formula for solving this problem: n * (n+1) / 2.
The code I would write:
def sum(n): return n*(n+1)//2
Mike Rapadas
4,5932 gold badges27 silver badges21 bronze badges
answered Jun 3, 2021 at 12:42
ahmedgahmedg
1661 gold badge1 silver badge10 bronze badges
You need 2 different variables in your code -- a variable where you can store the sum as you iterate through the values and add them (my_sum in my code), and another variable (i in my code) to iterate over the numbers from 0 to n.
def problem1_3(n): my_sum = 0 i=0 #replace this pass (a do-nothing) statement with your code while i <= n: my_sum = my_sum + i print(my_sum) i+=1 return my_sumYou are using the my_sum variable in your code to both store the sum and iterate through the numbers.
ayhan
66.1k17 gold badges173 silver badges191 bronze badges
answered May 10, 2017 at 20:23
0
This one line do the job :
sum(range(1, n+1))answered Feb 8, 2018 at 14:04
answered Oct 24, 2018 at 13:27
The sum of numbers from 1 to n will be greater than n. For example, the sum of numbers from 1 to 5 is 15 which is obviously greater than 5. Your while loop terminates prematurely. You need to maintain a separate counter for the loop.
answered May 10, 2017 at 20:14
HeshamHesham
111 silver badge2 bronze badges
Actually, I have tried a lot of that type of Programs In Jupyter Notebook you may use these:
n = int(input("Enter the Number: ")) print(n * (n + 1) // 2)And Also You may try this code:
def problem1_3(n): return n + problem1_3(n-1) if n > 1 else 1And Also You may try it
def f(a): return (a + 1) * (abs(a) + 2 * (a <= 0)) // 2answered Dec 10, 2020 at 16:41
Real programmers use recursion (and hopes for a not too big n since there is no tail call optimization in Python):
def problem1_3(n): return n + problem1_3(n-1) if n > 1 else 1answered May 10, 2017 at 20:28
JohanLJohanL
6,5111 gold badge10 silver badges24 bronze badges
1
so it will be more optimal
def f(a): return (a + 1) * (abs(a) + 2 * (a <= 0)) // 2answered Sep 11, 2018 at 9:26
yoloyyoloy
1418 bronze badges
1
print("Sum =",sum(range(int(input("Enter a number\n"))+1)))
answered Oct 24, 2018 at 12:55
1
THIS WHILE LOOP ACTUALLY WORKS:
def main():
n = int(input('Enter a number:')) while n <= 1: n = int(input('Please input a new number')) total = 0 my_sum = 0 while (n - 1) >= total: total = total + 1 my_sum += total print(my_sum) returnmain()
answered Mar 8, 2021 at 3:20
1
These lines worked for me in Python
def summation(num): sumX = 0 for i in range(1, num + 1): sumX = sumX + i return sumX summation(3) #You place the num you need hereAs well as these:
def summation(num): return sum(range(1, num+1)) summation(3) #You place the num you need hereanswered Jul 10, 2021 at 22:40
How about you try it using a "While Loop":
def problem1_3(n): my_sum = 0 while my_sum <= n: print(my_sum,end=" ") # end = " " keeps from starting a new line my_sum = my_sum + 1 print(my_sum)answered Jun 28, 2017 at 12:15
n = input("Enter Number to calculate sum") n = int (n) #average = 0. #sum = 0 sum = 0. for num in range(0,n+1,1): sum = sum+num print("SUM of first ", n, "numbers is: ", sum ) # Print sum of numbers from 1 to N inclusive def sum2N(N): r = 0 for i in range(N+1): #for i in range(0,N+1,1): #r+=i r=r+i return(r) print(sum2N(10))
answered Apr 29, 2019 at 9:04