Advertisement
MeShootIn

Binary power

May 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.28 KB | None | 0 0
  1. double binary_power(double a, int n){
  2.     if(n == 0){
  3.         return 1;
  4.     }
  5.    
  6.     if(n % 2 == 0){
  7.         double res = binary_power(a, n / 2);
  8.        
  9.         return res * res;
  10.     }
  11.     else{
  12.         if(n > 0){
  13.             return a * binary_power(a, n - 1);
  14.         }
  15.         else{
  16.             return (1 / a) * binary_power(a, n + 1);
  17.         }
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement