Sunday, March 27, 2011

What is the output of this pseudocode?

procedure DoSomething(a_1, ... a_n)
 p = a_1
 for i = 2 to n
  temp = p
  for j = 1 to a_i
   p = p * temp

DoSomething(10,2,2,2)

We are getting mixed results. One of us got 10^7, the other 10^27.

I Think I found my error... I keep substituting 10 for p every time, instead of the new value for temp.

EDIT: here's my work:

{10, 2, 2, 2}
p = 10
i = 2 to 4
 temp = p = 10
 j = 1 to 2
  p = 10 * 10 = 10^2
  p = 10^2 * 10 = 10^3
i = 3 to 4
 temp = 10^3
 j = 1 to 2
  p = 10^3 * 10 = 10^4
  p = 10^4 * 10 = 10^5
i = 4 to 4
 temp = 10^5
 j = 1 to 2
  p = 10^5 * 10 = 10^6
  p = 10^6 * 10 = 10^7

10^7

From stackoverflow
  • There's a reason folks have called Python "executable pseudocode":

    >>> def doSomething(*args):
    ...     args = list(args);
    ...     p = args.pop(0)
    ...     for i in range(len(args)):
    ...         temp = p
    ...         for j in range(args[i]):
    ...           p *= temp
    ...     return p
    ...
    >>> print doSomething(10,2,2,2)
    1000000000000000000000000000
    
  • I entered the program into my TI-89 and got an answer of 1e27 for the value of p.

    t(a)
    Func
      Local i,j,p,tmp
      a[1]->p
      For i,2,dim(a)
        p->tmp
        For j,1,a[i]
          p*tmp->p
        EndFor
      EndFor
      Return p
    EndFunc
    
    t({10,2,2,2})       1.E27
    
  • Isn't it ((10^3)^4)^5 = 10 ^ 60 ?

  • It's 10^27 as shown by this bit of python code:

    a = [10,2,2,2]
    p = a[0]
    for i in range(1,len(a)):
        temp = p
        for j in range(a[i]):
            p *= temp
    print p
    

    1,000,000,000,000,000,000,000,000,000

    The problems with your code as posted are:

    • in your 10^7 solution, you're always multiplying by 10, not temp (which is increased to the final value of p after the j loop).
    • You're setting temp to arr[i], not p, in your PHP code (which I'll include here so my answer still makes sense after you edited it out of your question :-).

      $arr = array(10, 2, 2, 2);
      $p = $arr[0];
      $temp = 0;
      for($i = 1; $i <= 3; $i++)
      {
          $temp = $arr[$i];
          for($j = 0; $j <= $arr[$i]; $j++)
          {
              $p = $p * $temp;
          }
      }
      echo $p;
      
    TT : Realized this a bit before you posted, but thanks for taking the time to answer.
  • In C:

    #include <stdio.h>
    
    double DoSomething(double array[], int count)
    {
      double p, temp;
      int i, j;
    
      p = array[0];
    
      for(i=1;i<count;i++)
      {
        temp = p;
        for(j=0; j<array[i];j++)
        {
          printf("p=%g, temp=%g\n", p, temp); /* useful to see what's going on */
          p = p * temp;
        }
      }
      return p; /* this isn't specified, but I assume it's the procedure output */
    }
    
    double array[4] = {10.0,2.0,2.0,2.0};
    
    int main(void)
    {
      printf("%g\n", DoSomething(array, 4));
      return 0;
    }
    

    And, as others have indicated, 10e27. Note that the above is very verbose from your pseudo code - it could be simplified in many ways.

    I used the Tiny C Compiler - very small, lightweight, and easy to use for simple stuff like this.

  • Seems to be a function to calculate

    
    (((a_1^(a_2+1))^(a_3+1))^(a_4+1)...
    

    Thus we get ((10^3)^3)^3 = 10^(3^3) = 10^27

  • There is an error in your computation for 10^7, See below. The correct answer is 10^27 {10, 2, 2, 2}

    p = 10
    i = 2 to 4
     temp = p = 10
     j = 1 to 2
      p = 10 * 10 = 10^2
      p = 10^2 * 10 = 10^3
    i = 3 to 4
     temp = 10^3
     j = 1 to 2
      p = 10^3 * 10 = 10^4 -- p=p*temp, p=10^3 and temp=10^3, hence p=10^3 * 10^3.
      p = 10^4 * 10 = 10^5 -- Similarly for other steps.
    i = 4 to 4
     temp = 10^5
     j = 1 to 2
      p = 10^5 * 10 = 10^6
      p = 10^6 * 10 = 10^7
    
    drhorrible : formatting would be helpful. (Use two newlines in the editor to produce one in the final output)
    Charles Duffy : two newlines? ugh! the code-format option (four spaces before each line) looks much better.

0 comments:

Post a Comment