houw can I get the number of terms?

3 views (last 30 days)
欣婷
欣婷 on 18 Mar 2023
Commented: Walter Roberson on 18 Mar 2023
Write a program to compute the sum of the series :
2^3 +4^3 + 6^3 + ...
such that the sum is as large as possible without exceeding 1000. The program should display the sum and how many terms are used in the sum.
First, please don’t use the inbuilt function SUM.
Then, after getting the number of terms, please solve this problem using vectorisation.
  1 Comment
欣婷
欣婷 on 18 Mar 2023
I misspelled the question.I want to ask how can I get the number of terms.

Sign in to comment.

Answers (2)

Voss
Voss on 18 Mar 2023
Moved: Torsten on 18 Mar 2023
You can use a while loop:
total = 0;
ii = 0;
while total <= 1000
ii = ii + 1; % number of terms used so far
total = total + something; % sum so far
end
disp(sprintf('The sum is %d',total));
disp(sprintf('The number of terms used is %d',ii));
You have to figure out what expression goes in place of "something".

Walter Roberson
Walter Roberson on 18 Mar 2023
First realize that the sum of 2 cube + 4 cube + 6 cube and so on, is 2 cube times (1 cube + 2 cube + 3 cube +...)
Now look up the formula for the sum of cubes of the first n natural numbers
https://www.cuemath.com/algebra/sum-of-cubes-of-n-natural-numbers/
and multiply it by 2 cube.
You now have a closed formula, and you have a target of 1000, and you can solve the equation. Then round down to the nearest integer.
  1 Comment
Walter Roberson
Walter Roberson on 18 Mar 2023
I tested this method and it appears to work fine.
hint:
syms N positive
will avoid irrelevant roots of the equation.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!