houw can I get the number of terms?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
Risposte (2)
Voss
il 18 Mar 2023
Spostato: Torsten
il 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".
0 Commenti
Walter Roberson
il 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 Commento
Walter Roberson
il 18 Mar 2023
I tested this method and it appears to work fine.
hint:
syms N positive
will avoid irrelevant roots of the equation.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!