From 63d01c7b4e37b5c394798fd69a57cb1090c22d19 Mon Sep 17 00:00:00 2001 From: simitoor <109592870+simitoor@users.noreply.github.com> Date: Sat, 8 Oct 2022 21:16:35 +0530 Subject: [PATCH] Create Exponents optimized --- .../Java/bit-manipulation/Exponents optimized | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 algorithms/Java/bit-manipulation/Exponents optimized diff --git a/algorithms/Java/bit-manipulation/Exponents optimized b/algorithms/Java/bit-manipulation/Exponents optimized new file mode 100644 index 00000000..54b626c7 --- /dev/null +++ b/algorithms/Java/bit-manipulation/Exponents optimized @@ -0,0 +1,24 @@ +public class powerfib { + public static int power(int n,int pow) + { + if(pow==1) + { + return n; + } + if(pow%2==0) + return power(n,pow/2)*power(n,pow/2); + else + return n*power(n,pow/2)*power(n,pow/2); + }//optimised approach + /* if(pow==1) + { + return n; + } + return n*power(n,pow-1); + } simple approach */ + public static void main(String args[]) + { + + System.out.print(power(2,5)); + } +}