Write an O(log2(N)) algorithm to find X^N

Questions by d.sridhar85

Showing Answers 1 - 18 of 18 Answers

Sameer Kumar Boxi

  • Apr 27th, 2006
 

int computeXn(int x, int n)

{

  if(n == 2)

  {

    return x*x;

  }

 else if(n % 2 == 0)

 {

  int y = computeXn(x, n/2);

  return y*y;

 }

 else if(n % 2 == 1)

 {

  int y = computeXn(x, n/2);

  return y*y*x;

  }

}

  Was this answer useful?  Yes

Vinay

  • Apr 28th, 2006
 

The program has a bug not all the control path returns a value

  Was this answer useful?  Yes

deepak kumar

  • May 3rd, 2006
 

#include#includelong int a;long int func(long int x,long int n);int s=1;void main(){ long int x,n,p=1; scanf("%li",&x); for (n=2;n<15;n++) { a=x; s=1; if (n%2==1) p=func(x,n-1)*x; else p=func(x,n); printf("\n%li %li = %li",x,n,p*s); } getch();}long int func(long int x,long int n){ if (n==1) return x; if (n%2==1) {

  Was this answer useful?  Yes

mkag

  • May 11th, 2006
 

int Sq(int x, int n) {
    int y = 0;
 
    if (n < 0) return -1;
    if (n == 0) return 1;
    if (n == 1) return x;
 
    if (n % 2 == 0) {
        y = Sq(x, n/2);
        return y * y;
    }
 
    y = Sq(x, n/2);
    return x * y * y;
}

  Was this answer useful?  Yes

Ashish Bhawsar

  • May 13th, 2006
 

long Pow(int x, int n){ long Power=1,z=x,m=n; while(m>0) { while(m%2==0) { m=m/2; //Integer division z=z*z; } m--; Power*=z; } return Power;}

  Was this answer useful?  Yes

hari

  • Aug 20th, 2006
 

int xpwrn(int x,int n){ if(n==1) return x ; else if(n>1) { if(n%2==0) { xpwrn(x,n/2)*xpwrn(x,n/2); } else { x*xpwrn(x,n/2)*xpwrn(x,n/2); } }}

  Was this answer useful?  Yes

jinhu

  • Aug 8th, 2007
 

my anwser:
int xpwrn(int x,int n)
{
    int i=0;
    int rv=1;
    int n=x;
    for(;i<32&&(1<<i)<x;++i)
    {
            if((1<<i)&x != 0)
            {
               rv *= n;
             }
             n  *=x;
    }
    return rv;
}

  Was this answer useful?  Yes

Sandeep Phukan

  • Nov 7th, 2007
 

class powerRecurse {
 
 
 
 public int powerRec(int x,int n){
  
  if ( n==1 ) return x;
  else {
   return x*powerRec(x,--n);
  }
  
 }
 
 
 
 public static void main(String[] a){
  
  System.out.println(new powerRecurse().powerRec(2,4));
 }
 
 
 
}

  Was this answer useful?  Yes

Here is the algorithm that works well and takes O(log n) time.

Algorithm Exponientiate(x,n)
//computes x^n  for an integer n>=0

    m:= n;
    power:=1;
    z:=x;
    while( ( m mod 2 )== 0 ) do
            {
                  m:= m/2 ; //take the floor of the result.
                  z:=z^2;  //square of z
              
            
             }
     m:=m-1;
    power:=power*z;


 }
 return power;
}

  Was this answer useful?  Yes

ashgoel

  • Jun 24th, 2010
 

represent N=b(k)b(k-1)....b(1)b(0) as bit representation

then N=summation i=o to k   b(i)*2^i
so x can be represented as 

x(N)=x(b0)*x(2b1)*x(4b2)...*x(2(k)bk)

bois nothing but Nmod 2

long power(int x, int n)
{
long power=1;
long y=x;
while (n>0)
while (n%2 ==0)
{
y*=y;
n/=2;
}
n-=1;
power *=y;
}
return power;
}

}

}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions