Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[50] Unrolling A Recursion Termination Condition #6

Open
charmhe opened this issue Mar 9, 2022 · 0 comments
Open

[50] Unrolling A Recursion Termination Condition #6

charmhe opened this issue Mar 9, 2022 · 0 comments

Comments

@charmhe
Copy link

charmhe commented Mar 9, 2022

The original solution is perfect. Seems to be faster if pulling the condition of the negative outside.

double pow(double x, int n) {
    return n < 0 ? 1/pow(x, (long)-1*n) : pow(x, (long)n);
}
double __pow(double x, long n) {
    if (n == 0) return 1;
    if (n == 1) return x;
    if (n == 2) return x * x;
    if(n % 2)
        return __pow(__pow(x, n / 2), 2);
    else
	return __pow(__pow(x, n / 2), 2) * x;
}
@charmhe charmhe changed the title [Optimization on LC 50] [50] Unrolling A Recursion Termination Condition Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant