C Programming Reference
 
C Programming Reference >> C  Programming Reference Question Bank >>

19/07/07 - 459 Views - Ratings :   3 of 5 / 1 Votes

Level - Advance
      

Question -

What is the aritmetic operation performed by function on x in relation with n?
int  mystery ( int x , int  n)  
{    


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

}
Answer -
x^n ( x raised to power n)
Explanation -

This code is difficult to understand, but here the operation performed is x^n . The mathematical operation is performed by binary doubling strategy. In this strategy if next most significant binary digit of n( power) is one then multiply accumulated product calculatedd using recursion by current val , then we reduce n by a factor of two using integer division . get next val by multiply current value by itself

Reader Comments -

Author Comments
Add Comments 


Name :    
Reply :   


Rating :

Code :
Code

 

 
© 2006 cencyclopedia.com