1、通过牛顿迭代法求解平方根
链接: https://blog.csdn.net/XQC_KKK/article/details/123790167.
2、计算前n个整数之和
def sumOfN(n):
theSum = 0
for i in range(1, n + 1):
theSum += i
return theSum
print(sumOfN(3))
3、计算执行时间
import time
def sumOfN2(n):
start = time.time()
theSum = 0
for i in range(1, n + 1):
theSum += i
end = time.time()
return theSum, end - start
print(sumOfN2(100000))
4、不使用循环计算前n个整数之和(高斯方法)
def sumOfN3(n):
return (n * (n + 1)) / 2
print(sumOfN3(3))