Create Exponents optimized

pull/989/head
simitoor 2022-10-08 21:16:35 +05:30 committed by GitHub
parent 3c7339e59c
commit 63d01c7b4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 0 deletions

View File

@ -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));
}
}