1. 程式人生 > >尤拉計劃問題六matlab實現

尤拉計劃問題六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

短短几行程式碼就解決了問題,程式設計確實提升了我們的工作效率!大家多多交流!