Add up the squares of all odd positive integers until it equals or exceeds 5 million.
(1^2+3^2...)

댓글 수: 2

Steven Lord
Steven Lord 2016년 12월 1일
Show what you've tried to do to solve the problem and ask a specific question about where you're having difficulty and you may receive some guidance.
Rena Berman
Rena Berman 2017년 1월 20일
(Answers Dev) Restored Question.

댓글을 달려면 로그인하십시오.

답변 (3개)

Image Analyst
Image Analyst 2016년 12월 1일

2 개 추천

Try this:
theSum = 0; % Initialize
thisNumber = -1;
while theSum < 5000000
thisNumber = thisNumber + .......
theSum = theSum + ......
end
I've given you a start. Please finish the rest of your homework yourself.
s.p4m
s.p4m 2016년 12월 1일
편집: s.p4m 2016년 12월 2일

0 개 추천

sum=0;
k=0;
while(sum<=5*10^6)
if(mod(k,2))
sum=sum+k^2;
end
k=k+1;
end
Next time please try to solve your homework by yourself

댓글 수: 5

Image Analyst
Image Analyst 2016년 12월 1일
I see 3 problems with this.
s.p4m
s.p4m 2016년 12월 2일
I found one mistake, where I use i instead of k in the mod() function, but everything else seems to be fine.
Image Analyst
Image Analyst 2016년 12월 2일
OK, 4 problems then:
  1. Using i (imaginary variable) for loop index, like you said.
  2. Using <= when the user said to quit when the sum hit 5 million. Using <= will do another iteration after it should have stopped. Actually it's not really a problem since 5 million is not an odd integer so it won't increase the sum, but it is another unneeded iteration.
  3. Using sum, a built in function name, as the name of your variable will prevent you from using the sum function in the same scope. It's never a good idea to blow away/overwrite built-in functions.
  4. Iterating on every single integer will do twice as many iterations as my solution where I hope the poster figured out that you're supposed to add 2 to the loop index. Plus mod() will slow it down a little bit. It's unnecessary - just add 2 to get only odd integers.
And of course there is the issue of just answering a homework question outright. Our protocol here is not to do homework for people so they can turn in our solution as their own, but to give them hints or starter snippets that they can modify so they at least have some ownership of the solution.
s.p4m
s.p4m 2016년 12월 2일
Thanks for the answer. You are right with every point.
I didn't know about the rule not to do outher people homework, but I will embrace it from now on.
Jan
Jan 2016년 12월 2일
편집: Jan 2016년 12월 2일
5. 5*10^6 is an expensive power operation, while 5e6 is cost free constant.
Thanks, s.p4m, for you suggestion. If the OP reads the comments carefully, he has learned something about programming. :-)

댓글을 달려면 로그인하십시오.

prin
prin 2022년 10월 23일

0 개 추천

jum=100; n = 1; while sum(1:r) > jum
disp(r)
n = n - 1;

댓글 수: 1

Walter Roberson
Walter Roberson 2022년 10월 23일
r is not defined. You are missing the "end" of the "while".

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2016년 12월 1일

댓글:

2022년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by