Advertisement
GokulDeep

FastExp

Feb 28th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1. package com.gokul.demo.InProgress;
  2.  
  3. public class FastExpo {
  4.  
  5.     public static void main(String[] args) {
  6.         FastExpo fe = new FastExpo();
  7.         double myPow = fe.myPow(1.0000,-2147483648
  8.         );
  9.         System.out.println(myPow);
  10.     }
  11.  
  12.     public double myPow(double x, int n) {
  13.         if (n == 0) {
  14.             return 1;
  15.         }
  16.         if (n < 0) {
  17.             return 1 / myPow(x, -1 * n);
  18.         }
  19.  
  20.         double k = myPow(x, n / 2);
  21.         if (n % 2 == 0) {
  22.             return k * k;
  23.         }
  24.  
  25.         return k * k * x;
  26.  
  27.     }
  28.  
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement