Write a program to calculate 1^1 + 2^2 + 3^3 + ... + 10^10. Without using ^.
Show older comments
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 Comments
madhan ravi
on 24 Mar 2019
Without using "^." no worries use ".^" homework solved.
Walter Roberson
on 24 Mar 2019
realpow()
John D'Errico
on 24 Mar 2019
Edited: John D'Errico
on 24 Mar 2019
Interesting. Based on the title, I would have thought the answer was sum(11:11:1010). I guess there was supposed to be a power in there between the digits.
Oh well, my program is:
x = 10405071317;
Short. Sweet. Accurate. ;-)
Ok, I'll give you a hint, that does not even require tools like realpow.
You want to form the sum of terms like n^n, but without ever using the ^ operator.
What is exp(log(x))? Note that they are inverses of each other. One undoes what the other does. So exp(log(x)) = x.
Given that, what is the log(n^n)? I hope you know that. log(n^n) = n*log(n). Therefore, what is exp(n*log(n))? If you want it to return the exact integer form, you might need round(exp(n*log(n))).
dpb
on 24 Mar 2019
Depending upon the groundrules for the HW assignment regarding "how", there's always the two nested for...end loop way to compute it with nothing but elemental operations...
Of course, using outside knowledge of mathematics to be more creative is the key to writing good programs in the long run, but for learing the basics of just writing code and debugging I would tend to ask for the "deadahead" solution first and then extra credit for the alternative solutions...
John D'Errico
on 24 Mar 2019
Oh, dpb, you are just no fun at all. ;-) I say to live on the edge. Take chances, and hope the person grading your work has a sense of humor. Of course, I had to worry about passing a course only sometime in the previous century.
Seth Herft
on 24 Mar 2019
"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>
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?
Answers (1)
Anant Upadhyay
on 27 Mar 2019
0 votes
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.
Categories
Find more on Loops and Conditional Statements 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!