Skip to content

Conversation

shimmer12
Copy link
Contributor

Description of Change

  • Implemented Modular Exponentiation in R using fast binary exponentiation.
  • Achieves O(log n) time complexity with constant extra space.
  • Handles both positive and negative bases correctly (normalizing modulo values).
  • Provides support for optional modulus input; when omitted, computes plain exponentiation.
  • Includes roxygen-style documentation with examples and parameter descriptions.
  • Added self-tests using stopifnot() for validation of sample cases.
  • Stored under mathematics/modular_exponentiation.r following project structure and naming conventions.

Checklist

  • Verified that this algorithm is not already implemented elsewhere in the repository (including under a different name)
  • Confirmed this is a recognized CS algorithm (not a problem-specific variant)
  • The file uses a lowercase .r extension and follows directory structure (mathematics/)
  • DIRECTORY.md updated with the new entry
  • Code includes appropriate documentation (roxygen-style header) and examples
  • Variable naming follows repository conventions (e.g., snake_case or dot.case)
  • Code runs without errors/warnings on test cases; included a simple self-test (e.g., stopifnot(...))

Notes:
Adds an efficient and well-documented implementation of Modular Exponentiation under mathematics/, demonstrating logarithmic-time exponentiation via binary exponentiation. Includes examples, edge case handling, and validation tests.

Implements modular exponentiation using fast binary exponentiation with optional modulus. Validates input parameters and handles negative bases correctly.
@Copilot Copilot AI review requested due to automatic review settings October 11, 2025 19:57
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds a new modular exponentiation function to the mathematics directory, implementing fast binary exponentiation with O(log n) time complexity. The function supports both regular exponentiation and modular exponentiation with proper handling of negative bases and comprehensive input validation.

Key Changes

  • Implements efficient binary exponentiation algorithm with optional modular arithmetic
  • Includes comprehensive roxygen-style documentation with examples and parameter descriptions
  • Provides input validation for exponent and modulus parameters

Updated example for modular_exponentiation function to include calculation explanation.
Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@siriak siriak merged commit 47ff5ed into TheAlgorithms:master Oct 11, 2025
2 checks passed
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

Successfully merging this pull request may close these issues.

2 participants