Skip to content

Conversation

@mike-turintech
Copy link
Member

This pull request introduces significant optimizations to the IsPrime and PrimeFactors methods within the Primes class. Key changes include:

  1. Prime Checking Optimization: The loop condition in the IsPrime method has been modified from i < n to i * i <= n, reducing the number of iterations needed to check for primality. Additionally, the redundant check && i != n has been removed for clarity.

  2. Enhanced Prime Factorization: The PrimeFactors method now uses a similar loop condition (i * i <= n) for efficiency. A while loop has been implemented to handle repeated prime factors, ensuring that each factor is fully divided out before moving on. An extra check is added to include n as a prime factor if it remains greater than 1 after factorization.

These changes significantly improve the performance of both algorithms, particularly for larger integers, while also enhancing code readability.

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.

3 participants