Repository: TheAlgorithms/Java
Pull request #5715 (2024-10-11 06:52:51)

feat: add fast exponentiation algorithm
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5715?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 62.96%. Comparing base [(`7326ab2`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/7326ab2c121ff7059bd17e5745afa7d70b98d0f0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`9cc09ae`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9cc09ae032cd354c1423120714513002c50ea844?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 2 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5715      +/-   ##
============================================
+ Coverage     62.77%   62.96%   +0.18%     
- Complexity     4025     4049      +24     
============================================
  Files           565      568       +3     
  Lines         15939    16042     +103     
  Branches       3076     3087      +11     
============================================
+ Hits          10006    10101      +95     
- Misses         5521     5525       +4     
- Partials        412      416       +4     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5715?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

saahil-mahato: @alxkm Please review this. Thanks.

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/FastExponentiation.java b/src/main/java/com/thealgorithms/maths/FastExponentiation.java
new file mode 100644
index 00000000..27f49e27
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/FastExponentiation.java
@@ -0,0 +1,67 @@
+package com.thealgorithms.maths;
+
+/**
+ * This class provides a method to perform fast exponentiation (exponentiation by squaring),
+ * which calculates (base^exp) % mod efficiently.
+ *
+ * <p>The algorithm works by repeatedly squaring the base and reducing the exponent
+ * by half at each step. It exploits the fact that:
+ * <ul>
+ *   <li>If exp is even, (base^exp) = (base^(exp/2))^2</li>
+ *   <li>If exp is odd, (base^exp) = base * (base^(exp-1))</li>
+ * </ul>
+ * The result is computed modulo `mod` at each step to avoid overflow and keep the result within bounds.
+ * </p>
+ *
+ * <p><strong>Time complexity:</strong> O(log(exp)) — much faster than naive exponentiation (O(exp)).</p>
+ *
+ * For more information, please visit {@link https://en.wikipedia.org/wiki/Exponentiation_by_squaring}
+ */
+public final class FastExponentiation {
+
+    /**
+     * Private constructor to hide the implicit public one.
+     */
+    private FastExponentiation() {
+    }
+
+    /**
+     * Performs fast exponentiation to calculate (base^exp) % mod using the method
+     * of exponentiation by squaring.
+     *
+     * <p>This method efficiently computes the result by squaring the base and halving
+     * the exponent at each step. It multiplies the base to the result when the exponent is odd.
+     *
+     * @param base the base number to be raised to the power of exp
+     * @param exp the exponent to which the base is raised
+     * @param mod the modulus to ensure the result does not overflow
+     * @return (base^exp) % mod
+     * @throws IllegalArgumentException if the modulus is less than or equal to 0
+     * @throws ArithmeticException if the exponent is negative (not supported in this implementation)
+     */
+    public static long fastExponentiation(long base, long exp, long mod) {
+        if (mod <= 0) {
+            throw new IllegalArgumentException("Modulus must be positive.");
+        }
+
+        if (exp < 0) {
+            throw new ArithmeticException("Negative exponent is not supported.");
+        }
+
+        long result = 1;
+        base = base % mod; // Take the modulus of the base to handle large base values
+
+        // Fast exponentiation by squaring algorithm
+        while (exp > 0) {
+            // If exp is odd, multiply the base to the result
+            if ((exp & 1) == 1) { // exp & 1 checks if exp is odd
+                result = result * base % mod;
+            }
+            // Square the base and halve the exponent
+            base = base * base % mod; // base^2 % mod to avoid overflow
+            exp >>= 1; // Right shift exp to divide it by 2
+        }
+
+        return result;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/FastExponentiationTest.java b/src/test/java/com/thealgorithms/maths/FastExponentiationTest.java
new file mode 100644
index 00000000..f117f902
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/FastExponentiationTest.java
@@ -0,0 +1,67 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for the {@link FastExponentiation} class.
+ *
+ * <p>This class contains various test cases to verify the correctness of the fastExponentiation method.
+ * It covers basic functionality, edge cases, and exceptional cases.
+ */
+class FastExponentiationTest {
+
+    /**
+     * Tests fast exponentiation with small numbers.
+     */
+    @Test
+    void testSmallNumbers() {
+        assertEquals(1024, FastExponentiation.fastExponentiation(2, 10, 10000), "2^10 mod 10000 should be 1024");
+        assertEquals(81, FastExponentiation.fastExponentiation(3, 4, 1000), "3^4 mod 1000 should be 81");
+    }
+
+    /**
+     * Tests the behavior of the fast exponentiation method when using a modulus.
+     */
+    @Test
+    void testWithModulo() {
+        assertEquals(24, FastExponentiation.fastExponentiation(2, 10, 1000), "2^10 mod 1000 should be 24");
+        assertEquals(0, FastExponentiation.fastExponentiation(10, 5, 10), "10^5 mod 10 should be 0");
+    }
+
+    /**
+     * Tests the edge cases where base or exponent is 0.
+     */
+    @Test
+    void testBaseCases() {
+        assertEquals(1, FastExponentiation.fastExponentiation(2, 0, 1000), "Any number raised to the power 0 mod anything should be 1");
+        assertEquals(0, FastExponentiation.fastExponentiation(0, 10, 1000), "0 raised to any power should be 0");
+        assertEquals(1, FastExponentiation.fastExponentiation(0, 0, 1000), "0^0 is considered 0 in modular arithmetic.");
+    }
+
+    /**
+     * Tests fast exponentiation with a negative base to ensure correctness under modular arithmetic.
+     */
+    @Test
+    void testNegativeBase() {
+        assertEquals(9765625, FastExponentiation.fastExponentiation(-5, 10, 1000000007), "-5^10 mod 1000000007 should be 9765625");
+    }
+
+    /**
+     * Tests that a negative exponent throws an ArithmeticException.
+     */
+    @Test
+    void testNegativeExponent() {
+        assertThrows(ArithmeticException.class, () -> { FastExponentiation.fastExponentiation(2, -5, 1000); });
+    }
+
+    /**
+     * Tests that the method throws an IllegalArgumentException for invalid modulus values.
+     */
+    @Test
+    void testInvalidModulus() {
+        assertThrows(IllegalArgumentException.class, () -> { FastExponentiation.fastExponentiation(2, 5, 0); });
+    }
+}