I misspelled the question.I want to ask how can I get the number of terms.
houw can I get the number of terms?
2 views (last 30 days)
Show older comments
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.
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
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".
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
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.
I tested this method and it appears to work fine.
syms N positive
will avoid irrelevant roots of the equation.
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!