GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Placement Papers  >  Adobe  >  Placement Papers

 Print  |  
Question:  write an O(log2(N)) algorithm to find X^N



April 04, 2006 15:25:06 #1
 Sameer Kumar Boxi   Member Since: Visitor    Total Comments: N/A 

RE: write an O(log2(N)) algorithm to find X^N
 

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;

  }

}

     

 

Back To Question