Java icon indicating copy to clipboard operation
Java copied to clipboard

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

Open Vinayak-v12 opened this issue 3 months ago • 2 comments

This PR optimizes the power() method by replacing the linear recursive implementation with an exponentiation-by-squaring approach.

✔ Time complexity reduced from O(n) → O(log n) ✔ Handles negative exponents ✔ Keeps same behavior as original implementation ✔ Fully compatible with existing test cases ✔ No API changes

This improves performance and reduces recursion depth while maintaining clean readability.

Vinayak-v12 avatar Nov 15 '25 07:11 Vinayak-v12

Codecov Report

:x: Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review. :white_check_mark: Project coverage is 78.33%. Comparing base (a008cc2) to head (0537442).

Files with missing lines Patch % Lines
...a/com/thealgorithms/maths/PowerUsingRecursion.java 87.50% 0 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff            @@
##             master    #7069   +/-   ##
=========================================
  Coverage     78.33%   78.33%           
- Complexity     6670     6672    +2     
=========================================
  Files           753      753           
  Lines         22185    22192    +7     
  Branches       4356     4359    +3     
=========================================
+ Hits          17378    17384    +6     
  Misses         4107     4107           
- Partials        700      701    +1     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Nov 15 '25 07:11 codecov-commenter

whats the difference between your implementation and https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/BinaryExponentiation.java ? @Vinayak-v12

DenizAltunkapan avatar Nov 23 '25 15:11 DenizAltunkapan