欧拉计划问题六matlab实现

Problem 6:Sum square difference

The sum of the squares of the first ten natural numbers is:

                                 \large 1^{2}+2^{2}+3^{2}+\cdot \cdot \cdot +10^{2} = 385

The square of the sum of the first ten natural numbers is:

                             \large (1 + 2 +3 + \cdot \cdot \cdot +10)^{2} = 55^{2} = 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.

思路:

这题的话编程相对要简单一些,据题意已知1到10的和方差的求法,让我们求1到100的和方差,我们可以直接调用求和函数,大大的提高了我们编程的效率,好了,直接上代码。

n = input('varible= ')  %将输入变量赋值给n
i = 1 : n
x = sum(i.^(2));
y = sum(i).^2;
t = y - x;
t

结果: 25164150

短短几行代码就解决了问题,编程确实提升了我们的工作效率!大家多多交流!

猜你喜欢

转载自blog.csdn.net/qq_38910271/article/details/82961803