Last active
June 28, 2022 14:13
-
-
Save mlhoutel/a54c210250826515839ca9eb6e8ba6ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double GetValue(double x, int n) { | |
if (n == 0) { return 1; } // O(1) | |
return (n > 0) ? RecValue(x, n) : 1 / RecValue(x, -n); // O(n*log(n)) | |
} | |
private double RecValue(double x, int n) { | |
if (n == 1) { return x; } // O(1) | |
int divider = 2; // O(1) | |
int remain = n % divider; // O(1) | |
int mult = remain == 0 ? n / divider : (n - 1) / divider; // O(1) | |
double val = GetValue(x, mult); // O(n*log(n)) | |
for (int i = 0; i < divider - 1; i++) { val *= val; } // O(1) | |
for (int i = 0; i < remain; i++) { val *= x; } // O(1) | |
return val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment