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).
What are the differences between least-squares and Kalman filtering
6 Existence and uniqueness of the MP inverse . . . . . . . . . . . 37 v vi Contents 7 Some properties of the MP inverse . . . . . . . . . . . . . . . . 38 8 Further properties . . . . . . . . . . . . ...
3.1.1 Maximum likelihood and least squares . . . . . . . . . . . . 140 3.1.2 Geometry of least squares . . . . . . . . . . . . . . . . . . 143 3.1.3 Sequential learning . . . . . . . . . . . . . . . ....
4.1.1 Mean Square Continuity and Differentiability . . . . . . . . . . . . 81 4.2 Examples of Covariance Functions . . . . . . . . . . . . . . . . . . . . . . 81 4.2.1 Stationary Covariance Functions...
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 ...
8.9 Using the CATMOD and GENMOD Procedures for Logistic Regression . . . 232 Appendix A: Statistical Methodology for Dichotomous Logistic Regression . . . 239 Chapter 9. Logistic Regression II: ...
Sudoku is a game with incremental intelligence and is popularizing in many countries ...squares, and the mathematical model and statistical method of data from a Sudoku square design, are presented here
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 ...
In other words, squares are a subtype of rectangles, and rectangles are a supertype of squares. Recognizing and acting upon such subtype / supertype relationships provides numerous benefits: ...
A long-standing problem in classication is the determination of the regularization parameter. Nearly every classication algorithm uses a parameter (or set of parameters) to control classier complexity...
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 ...
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 ...
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 ...
整体最小二乘基本理论,介绍整体最小二乘基本原理,配合实例计算。
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 ...
Due to the continuing progress of sensor technology, the availability of 3-D cam- eras is already foreseeable. These cameras are capable of generating a large set of measurement points within a very ...