So you have a loop that will iterate exponent number of times, what action do you want to do one time for each iteration to get the final number?
well you want the base * base so would sum = base * base work I dont think so because then I think the sum with then need to be multiplyed by the base? Am I making things more complex?
well you want the base * base so would sum = base * base work I dont think so because then I think the sum with then need to be multiplyed by the base? Am I making things more complex?
You are, but you're close. You're right about the base*base. Now think about the fact that you can multiply x^2 by x^3 and get x^5.
Another way of looking at it, x*x = x^2, right? Now let's call that product. What's product * x?
::Edit - I guess product would be more apt than sum, so I updated it.
You're so close. You're right in thinking that base = base * base; will not work, because with x = 5, your first iteration results in 25, then 625, then...etc.
If you were adding numbers together, you'd use a sum variable. Now, since we are adding, sum should start at 0, because 0 + x = x (This is the Addition Identity Rule). But you are multiplying. If you started sum as 0, then your result would be 0, because 0 * x = 0. What you need is a number n such that n * x = x. Can you think of the number that will fulfill this requirement? This is what your sum variable will start out as - then do sum = sum * base.
You're so close. You're right in thinking that base = base * base; will not work, because with x = 5, your first iteration results in 25, then 625, then...etc.
If you were adding numbers together, you'd use a sum variable. Now, since we are adding, sum should start at 0, because 0 + x = x (This is the Addition Identity Rule). But you are multiplying. If you started sum as 0, then your result would be 0, because 0 * x = 0. What you need is a number n such that n * x = x. Can you think of the number that will fulfill this requirement? This is what your sum variable will start out as - then do sum = sum * base.
well in your example n should start out with sum =1 but doesnt the sum = 1 restart the sum = sum * base when it loops?
I've called the function, passing 5 and 3 (i.e. I want to find 5^3).
Now, sum starts as 1 before the loop.
First execution of the loop: sum = sum * base;
sum = 1 * 5;
sum = 5;
Second execution of the loop: sum = sum * base;
sum = 5 * 5;
sum = 25;
etc...
So, no, using sum = 1 will not reset the value, but setting sum = 0 will.
ok I am seeing the working but I still dont get how you will declare the sum = 1
And in the loop the sum will have a new value but when the loop, loops, I beleive the sum will become 1 again.
then base which is the base will be multiplyed by the sum which os one but when the loop comes back around wont the sum be 1 again? This doesnt make sense to me.
No, since you are assigning the new value (sum * base) to sum, it will change values every time within the loop. Try it for yourself to see that it changes.
Comment