Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Optimize power() using exponentiation by squaring (O(log n))#7069

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Vinayak-v12 wants to merge3 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromVinayak-v12:optimize-power-method
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,21 +2,43 @@

/**
* calculate Power using Recursion
* @authorBama Charan Chhandogi(https://github.com/BamaCharanChhandogi)
* @authorVinayak(https://github.com/Vinayak-v12)
*/

public final class PowerUsingRecursion {
private PowerUsingRecursion() {
}

/**
* Computes base raised to the given exponent.
*
* @param base the number to be raised
* @param exponent the power (can be negative)
* @return base^exponent
*/
public static double power(double base, int exponent) {
// Base case: anything raised to the power of 0 is 1

// Handle negative exponent: a^-n = 1 / (a^n)
if (exponent < 0) {
return 1.0 / power(base, -exponent);
}

// Base cases
if (exponent == 0) {
return 1;
return 1.0;
}
if (exponent == 1) {
return base;
}

// Exponentiation by Squaring
// If exponent is even: a^n = (a^(n/2))^2
if (exponent % 2 == 0) {
double half = power(base, exponent / 2);
return half * half;
}

// Recursive case: base ^ exponent = base * base ^ (exponent - 1)
// Recurse with a smaller exponent and multiply with base
// If exponent is odd: a^n = a * a^(n-1)
return base * power(base, exponent - 1);
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,15 +6,23 @@

/**
* Test case for Power using Recursion
* @authorBama Charan Chhandogi(https://github.com/BamaCharanChhandogi)
* @authorVinayak(https://github.com/Vinayak-v12)
*/

class PowerUsingRecursionTest {

@Test
void testPowerUsingRecursion() {
assertEquals(32.0, PowerUsingRecursion.power(2.0, 5));
assertEquals(97.65625, PowerUsingRecursion.power(2.5, 5));
assertEquals(81, PowerUsingRecursion.power(3, 4));
// exponent = 0
assertEquals(1.0, PowerUsingRecursion.power(5.0, 0));

// exponent = 1
assertEquals(5.0, PowerUsingRecursion.power(5.0, 1));

// negative exponent
assertEquals(0.25, PowerUsingRecursion.power(2.0, -2));

// another negative exponent
assertEquals(0.5, PowerUsingRecursion.power(2.0, -1));
}
}

[8]ページ先頭

©2009-2025 Movatter.jp