Skip to content

VibhuDixit-2215001940/PrimeSieveAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Œ PrimeSieveAPI

An efficient Prime Number API using the Sieve of Eratosthenes and MongoDB caching πŸš€


πŸ”Ή Features

βœ… Fast prime number generation using Sieve of Eratosthenes
βœ… MongoDB caching for frequently requested values
βœ… Simple REST API with GET /api/primes/:N
βœ… Built with Node.js, Express, and MongoDB


πŸ”Ή Sieve of Eratosthenes Algorithm

The Sieve of Eratosthenes is an ancient algorithm used to find all prime numbers up to a given limit. It works by iteratively marking the multiples of each prime number starting from 2. The numbers which remain unmarked are primes.

How It Works:

  1. Start with a list of numbers from 2 to N (the limit).
  2. Starting from 2, mark all of its multiples as non-prime.
  3. Move to the next unmarked number (it will be a prime), and mark all of its multiples as non-prime.
  4. Repeat this process until you've processed all numbers up to N.
  5. The remaining unmarked numbers are primes.

🧐 Sneak Peek 1: The First Few Steps

image image

For N = 20:

  1. List numbers from 2 to 20:
    [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

  2. Mark multiples of 2:
    [2, 3, X, 5, X, 7, X, 9, X, 11, X, 13, X, 15, X, 17, X, 19, X]

  3. Mark multiples of 3:
    [2, 3, X, 5, X, 7, X, 9, X, 11, X, 13, X, 15, X, 17, X, 19, X]

  4. Continue marking multiples of each unmarked number.


🧐 Sneak Peek 2: Final Prime Numbers

After applying the algorithm, the unmarked numbers up to 20 are: image


πŸš€ Installation & Setup

1️⃣ Clone the Repository

git clone https://github.com/your-username/PrimeSieveAPI.git
cd PrimeSieveAPI

2️⃣ Install Dependencies

npm install

3️⃣ Set Up Environment Variables

MONGO_URI=mongodb://localhost:27017/primeDB
PORT=5000

4️⃣ Start the Server

npm run dev

πŸ”Ή API Endpoints

πŸ“Œ Get Prime Numbers

GET /api/primes/:N

πŸ“₯ Request:

Replace :N with any number (e.g., /api/primes/20).

πŸ“€ Response:

{
  "primes": [2, 3, 5, 7, 11, 13, 17, 19]
}

πŸ”Ή Project Structure

PrimeSieveAPI/
│── src/
β”‚   │── models/          # MongoDB Model
β”‚   │── routes/          # API Routes
β”‚   │── controllers/     # API Logic
β”‚   │── db/              # Database Connection
β”‚   │── app.js           # Express App
│── server.js            # Server Entry Point
│── .env                 # Environment Variables
│── package.json         # Dependencies
│── README.md            # Project Documentation

πŸ”Ή Technologies Used

πŸš€ Node.js – Backend framework
πŸš€ Express.js – API routing
πŸš€ MongoDB – Caching for performance
πŸš€ Mongoose – Database ORM
πŸš€ Nodemon – Development utility


πŸ”Ή Contributing

πŸ”Ή Fork the repo
πŸ”Ή Create a new branch (git checkout -b feature-name)
πŸ”Ή Commit changes (git commit -m "Added new feature")
πŸ”Ή Push to branch (git push origin feature-name)
πŸ”Ή Open a Pull Request πŸš€


πŸ”Ή License

πŸ“œ MIT License – Free to use & modify.

About

This API efficiently calculates using Sieve of Eratosthenes algo and caches prime numbers using MongoDB, improving performance for frequently requested values. πŸš€

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors