# houw can I get the number of terms?

2 views (last 30 days)

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.

I misspelled the question.I want to ask how can I get the number of terms.

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 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.
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.

### Categories

Find more on Mathematics in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!