필터 지우기
필터 지우기

Write a program to calculate 1^1 + 2^2 + 3^3 + ... + 10^10. Without using ^.

조회 수: 6 (최근 30일)
Seth Herft
Seth Herft 2019년 3월 24일
답변: Anant Upadhyay 2019년 3월 27일
I am having trouble solving this. This is my code with the "^" symbol although can not figure out how to solve this problem without.
x = 0;
for i = 1:10
x = x + i^i;
end
disp(x);
Thank you.
  댓글 수: 8
Guillaume
Guillaume 2019년 3월 24일
편집: Guillaume 2019년 3월 24일
"My professor wants us to use a nesting loop, one for adding 1 to 10 and one loop for calculation exponentiation."
So, why don't you go ahead and do it? What is difficult about that?
On the other hand, you could complain to the professor thar asking you to use loops for things that shouldn't be done with loops is teaching you the wrong way to use matlab. (Something we spend a lot of time unteaching people on Answers, unfortunately).</rant over>
dpb
dpb 2019년 3월 24일
편집: dpb 2019년 3월 25일
I gotcha' John... :)
Mayhaps too plebian an approach, indeed, but figured to push towards what figured was probably the expected solution.
OK, Seth, that's what I suspected was the desired solution--so how would you write a loop to have a variable number of iterations? If you get that step, then there's just one coding bookkeeping step you have to think of to get the exponentiated term to add to the previous sum...
HINT: What algebraic expression equals M**N on paper without using the power for the expression?

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

답변 (1개)

Anant Upadhyay
Anant Upadhyay 2019년 3월 27일
Hi Seth,
According to my understanding, the problem is to compute the sum of 1^1 + 2^2 + 3^3 +….+ 10^10 and you do not want to use “^” symbol for calculating the above sum.
Basically, any natural number ‘N’ raised to a power ‘K’ can be computed by performing the product of the natural number ‘N’ ‘K’ times. Therefore, to compute ‘N^K’, a “for-loop” can be used.
Now, to calculate the required sum, you will need an outer loop that will run from starting term (1 in this case) to ending term (10 in this case) of the sequence and for each term, calculate the required value by multiplying the term with itself ‘K’ times, where ‘K’ is the value of exponent.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by