Skip to content

Conversation

piyushkumar0707
Copy link
Contributor

🚀 Overview

This PR implements the Sieve of Eratosthenes - an ancient, highly efficient algorithm for finding all prime numbers up to a given limit, with multiple optimizations and variants.

✨ Features

  • Classic sieve implementation - The traditional O(n log log n) algorithm
  • Optimized version - Only processes odd numbers after 2
  • Segmented sieve - Find primes in specific ranges efficiently
  • Prime counting - Count primes without storing them
  • Performance comparison - Benchmarks against trial division
  • Multiple variants - Educational and production-optimized versions

🎯 Why This Matters

The Sieve of Eratosthenes is fundamental for:

  • Cryptography - RSA key generation requires large primes
  • Number theory research - Prime distribution studies
  • Algorithm education - Classic example of optimization techniques
  • Mathematical computing - Efficient prime generation for calculations
  • Competitive programming - Fast prime generation in contests

📚 Implementation Details

  • Time Complexity: O(n log log n) - significantly faster than trial division
  • Space Complexity: O(n) for the boolean sieve array
  • Optimizations:
    • Skip even numbers (except 2)
    • Start marking from p²
    • Segmented approach for large ranges

🔧 Advanced Features

  • sieve_of_eratosthenes() - Basic implementation
  • sieve_of_eratosthenes_optimized() - Odd-numbers-only optimization
  • segmented_sieve() - Memory-efficient for large ranges
  • count_primes_sieve() - Count without storage
  • Performance benchmarking - Speed comparison demonstrations

🧪 Testing & Examples

Comprehensive testing including:

  • Small prime sets with manual verification
  • Large scale performance tests (up to 10,000+)
  • Edge cases (n < 2, n = 2, n = 3)
  • Segmented sieve range testing
  • Speed comparisons with trial division

📊 Performance Benefits

Example: Finding primes up to 1,000

  • Sieve method: ~0.001 seconds
  • Trial division: ~0.05+ seconds
  • Speedup: 50x+ faster

📁 Files Added

  • mathematics/sieve_of_eratosthenes.r - Complete implementation with variants
  • Updated DIRECTORY.md with new entry

🎓 Educational Value

Perfect for learning:

  • Algorithm optimization techniques
  • Trade-offs between time and space complexity
  • Historical algorithms still used today
  • Performance analysis methods

🎃 Hacktoberfest 2025

Adds a classical, highly-optimized algorithm with significant practical applications in cryptography and computational mathematics.

- Implement both basic and optimized versions of the sieve
- Include segmented sieve for finding primes in ranges
- Add performance comparison with trial division
- Comprehensive examples and edge case handling
- Update DIRECTORY.md to include the new algorithm
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

This PR implements the Sieve of Eratosthenes algorithm with multiple variants and optimizations. The implementation provides a classical prime number generation algorithm that's significantly more efficient than trial division methods.

Key changes:

  • Complete Sieve of Eratosthenes implementation with basic and optimized variants
  • Segmented sieve for memory-efficient range-based prime finding
  • Performance comparison utilities and comprehensive testing examples

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
mathematics/sieve_of_eratosthenes.r Main implementation with multiple sieve variants, performance benchmarks, and comprehensive examples
DIRECTORY.md Added entry for the new sieve algorithm file

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@siriak
Copy link
Member

siriak commented Oct 4, 2025

Check Copilot's comments

@siriak siriak merged commit 68bc3c8 into TheAlgorithms:master Oct 5, 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