mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 22:14:08 +08:00
Change project structure to a Maven Java project + Refactor (#2816)
This commit is contained in:

committed by
GitHub

parent
8e533d2617
commit
9fb3364ccc
@ -0,0 +1,22 @@
|
||||
package com.thealgorithms.divideandconquer;
|
||||
|
||||
public class BinaryExponentiation {
|
||||
|
||||
public static void main(String args[]) {
|
||||
System.out.println(calculatePower(2, 30));
|
||||
}
|
||||
|
||||
// Function to calculate x^y
|
||||
// Time Complexity: O(logn)
|
||||
public static long calculatePower(long x, long y) {
|
||||
if (y == 0) {
|
||||
return 1;
|
||||
}
|
||||
long val = calculatePower(x, y / 2);
|
||||
val *= val;
|
||||
if (y % 2 == 1) {
|
||||
val *= x;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user