9 Jun 2011

Euler Project6 欧拉工程6



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.

计算前100个自然数"和的平方"与"平方的和"的差值.
%% Find the difference between the sum of the squares of 1:100 and the square of the sum.
% Elapsed time is 0.000032 seconds.
% ans =
% 25164150
function result = euler6()
tic;
x = 1:100;
result = sum(x).^2-sum(x.^2);
toc;
end

No comments :

Post a Comment