Q:
The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)^2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
A:
import time
t1 = time.time()
''' method 1
fast than method 2
'''
sumone = 0
sumtwo = 0
for i in range(1,101):
sumone += i ** 2
sumtwo += i
sumtwo = sumtwo ** 2
print sumtwo - sumone
t2 = time.time()
print "time:%s" %str(t2-t1)
''' method 2
多出来的项目是2((a*1 + a*2 + a*3 + ... +a*(a-1)) + (b*1 + b*2 + b*3 + ... +b*(b-1) + ... )
'''
sumthree = 0
for i in range(100,1,-1):
for j in range(1,i):
sumthree += i * j
print sumthree * 2
t3 = time.time()
print "time:%s" %str(t3-t2)
''' method 3
faster than the two above
'''
def problem(r):
return sum(r) ** 2 - sum([x**2 for x in r])
problem(range(1,101))
t4 = time.time()
print "time:%s" %str(t4-t3)
分享到:
相关推荐
6Existence and uniqueness of the MP inverse . . . . . . . . . . . 37viContents 7Some properties of the MP inverse . . . . . . . . . . . . . . . . 38 8Further properties . . . . . . . . . . . . . . . ....
Given a cave with a number of rooms, represented as a 4x4 square. There is a wumpus, a pit, a pile of gold in the world....6. You start the adventure at the square (1,1) and leave the world at (4,4).
6 Existence and uniqueness of the MP inverse . . . . . . . . . . . 37 v vi Contents 7 Some properties of the MP inverse . . . . . . . . . . . . . . . . 38 8 Further properties . . . . . . . . . . . . ...
scanned the old master copies and produced electronic versions in Portable Document Format. The quality of the images varies depending on the quality of the originals. The images have not been ...
Least-squares estimation of transformation parameters between two point patterns。 相似变换相当于等距变换和均匀缩放的一个复合,即为: 左上角2*2矩阵为旋转部分,右上角为平移因子。它有四个自由度,即旋转...
整体最小二乘问题(The Total Least Squares Problem)是统计学与数值分析领域中的一个重要概念,尤其是在处理具有误差的数据集时尤为关键。本文将基于提供的标题、描述、标签以及部分内容,深入探讨整体最小二乘的...
A.2 The Sum of Two Vector Spaces 460 A.3 The Cartesian Product of Two Vector Spaces 461 A.4 The Tensor Product of Two Vector Spaces 461 A.5 The Kronecker Product AB of Two Matrices 462 B The Jordan ...
Partial Least Squares (PLS) Analysis was first developed in the late 60’s by Herman Wold, and works on the assumption that the focus of analysis is on which aspects of the signal in one matrix are ...
algorithms are a class of adaptive filter used to mimic a desired filter by finding the filter coefficients that relate to producing the least mean squares of the error signal (difference between the ...
The authors explain the natural links between LS-SVM classifiers and kernel Fisher discriminant analysis. Bayesian inference of LS-SVM models is discussed, together with methods for imposing ...
Each cell in the matrix represents the correlation coefficient between two variables, indicating the strength and direction of the association. This is particularly useful for identifying potential ...
It introduces various methods and computational issues involved in the formulation and implementation of the weighted least squares (WLS) approach, presents statistical tests for the detection and ...
The authors explain the basics of COM servers, including the difference between COM servers and conventional DLLs, and provide step-by-step instructions for building and registering a ...
Circle of Squares: A Simulation On the CD-ROM The Resources Page 15 Objects and Arrays A Refresher on Objects and Arrays Opcodes for Objects Opcodes for Arrays Three-Dimensional Array: A ...
在"sum_of_square"这个上下文中,"平方和"是指误差的平方和,是衡量模型拟合优度的一个重要指标。在PLS中,目标是找到最优的因子组合,使得这些因子构成的模型预测的误差平方和最小,从而达到最佳的拟合效果。 PLS...
models were constructed to analyze the associations between the incidence of human infection with H7N9 virus and population density, poultry density, and the number of positive environmental samples, ...
+ The position and size of the main window is saved on exit and restored the next time CurveExpert runs Bug Fixes: + Whoops! The new file reading procedure masked out all minus and minus signs ...