Python 从 1 到 n 的整数之和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43901484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sum of the integers from 1 to n
提问by mabski
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.
我正在尝试编写一个程序来将 1 到 n 的数字相加。我设法让它多次打印数字,但没有将它们全部添加。它只是将两个数字相加。
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?
我该如何解决这个问题?
采纳答案by Sruthi Susan Thomas
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.
您的代码中需要 2 个不同的变量——一个变量,您可以在迭代这些值时存储总和并将它们相加(my_sum 在我的代码中),另一个变量(在我的代码中)从 0 开始迭代数字到 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_sum
You are using the my_sum variable in your code to both store the sum and iterate through the numbers.
您在代码中使用 my_sum 变量来存储总和并遍历数字。
回答by Guillaume Jacquenot
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
您可以用一行来完成,您可以在其中创建一个整数列表0
ton
并使用sum
函数对所有元素求和
def problem1_3(n):
return sum(range(n+1))
回答by mabski
You could use numpy.
你可以使用 numpy。
import numpy as np
Your function should return
你的函数应该返回
np.sum(np.arange(1, n+1))
https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.sum.htmlhttps://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.arange.html
https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.sum.html https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy .arange.html
回答by Hesham
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.
从 1 到 n 的数字之和将大于 n。例如,从 1 到 5 的数字总和是 15,这显然大于 5。您的 while 循环过早终止。您需要为循环维护一个单独的计数器。
回答by JohanL
Real programmers use recursion (and hopes for a not too big n since there is no tail call optimization in Python):
真正的程序员使用递归(并希望 n 不会太大,因为 Python 中没有尾调用优化):
def problem1_3(n):
return n + problem1_3(n-1) if n > 1 else 1
回答by Abdulazizkhon Abduganiev
How about you try it using a "While Loop":
你试试用“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)
回答by faquin
This one line do the job :
这一行完成了这项工作:
sum(range(1, n+1))
回答by yoloy
so it will be more optimal
所以它会更优化
def f(a):
return (a + 1) * (abs(a) + 2 * (a <= 0)) // 2
回答by anonymous
print("Sum =",sum(range(int(input("Enter a number\n"))+1)))
回答by Akshat Tamrakar
I don't understand why everyone keeps making everything complex. Here is my simple solution
我不明白为什么每个人都让一切变得复杂。这是我的简单解决方案
n = int(input())
print(n * (n + 1) // 2)