the power function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • logicode
    New Member
    • Apr 2007
    • 22

    #16
    Originally posted by sicarie
    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?

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #17
      Originally posted by logicode
      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.

      Comment

      • logicode
        New Member
        • Apr 2007
        • 22

        #18
        Originally posted by sicarie
        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 sum. What's sum * x?
        x^3 because x^2 * x = x^3

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #19
          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.

          Comment

          • logicode
            New Member
            • Apr 2007
            • 22

            #20
            Originally posted by Ganon11
            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?

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #21
              The answer is what you start out with. What are you passed initially?

              Comment

              • logicode
                New Member
                • Apr 2007
                • 22

                #22
                Originally posted by sicarie
                The answer is what you start out with. What are you passed initially?

                I passed in base first and being base

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #23
                  Originally posted by logicode
                  well in your example n should start out with sum =1 but doesnt the sum = 1 restart the sum = sum * base when it loops?
                  OK, think through this example.

                  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.

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #24
                    Originally posted by logicode
                    I passed in base first and being base
                    So you start out with base. Can you represent that as an exponent?

                    Comment

                    • logicode
                      New Member
                      • Apr 2007
                      • 22

                      #25
                      Originally posted by sicarie
                      So you start out with base. Can you represent that as an exponent?

                      I dont quite get what you are trying to ask me

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #26
                        Originally posted by logicode
                        I dont quite get what you are trying to ask me
                        Look at what Ganon posted, and think about it - you're right there.

                        Comment

                        • logicode
                          New Member
                          • Apr 2007
                          • 22

                          #27
                          Originally posted by Ganon11
                          OK, think through this example.

                          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.

                          Comment

                          • Ganon11
                            Recognized Expert Specialist
                            • Oct 2006
                            • 3651

                            #28
                            It would if you declare sum within the loop. But you should declare it right before the loop enters.

                            Comment

                            • logicode
                              New Member
                              • Apr 2007
                              • 22

                              #29
                              hmmm like

                              for( i=0; i<exponent; i++; sum=1 )
                              {
                              sum= base* sum
                              }

                              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.

                              Comment

                              • Ganon11
                                Recognized Expert Specialist
                                • Oct 2006
                                • 3651

                                #30
                                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

                                Working...