Search This Blog

#Day98#100DaysChallenge- Matlab Loops |Project Euler: Natural numbers, squares and sums-Matlab Cody.

#Day98-Project Euler:  Natural numbers, squares and sums-Matlab Cody.

Task:
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 = 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 N (where N is the input) natural numbers and the square of the sum.

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.

Matlab Code:

function y = euler006(x)

  num=1:1:x;

  sq_sum=sum(num.^2);

  sum_sq=(sum(num(:,:)))^2;

  y=sum_sq-sq_sum;

end

Click here for Video description



Live Support: t.me/matlabirawen



 


No comments