DEV Community

B Mithilesh
B Mithilesh

Posted on

Simple Sieve - DSA

๐Ÿงฎ A Simple Sieve of Eratosthenes in Java: Find All Primes Up to N

"The prime numbers are the building blocks of all natural numbers."

โ€” Carl Friedrich Gauss

Prime numbers play a fundamental role in number theory and cryptography. In this post, we'll walk through the Sieve of Eratosthenes โ€” a simple and efficient algorithm to generate all prime numbers up to a given number n.

We'll cover:

  • โœ… What the sieve is
  • ๐Ÿง  How it works
  • ๐Ÿ”ง Java implementation (with fixed code)
  • ๐ŸŒ Real-life applications of primes

๐Ÿ“Œ What Is the Sieve of Eratosthenes?

The Sieve of Eratosthenes is a classic algorithm used to find all prime numbers less than a given integer n. It does so by iteratively marking the multiples of each prime starting from 2.

Time Complexity: O(n log log n)

Space Complexity: O(n)


๐Ÿ’ก How Does It Work?

  1. Create a boolean array of size n + 1, initialized to true. Each index represents a number.
  2. Starting from 2, for each number, if it is marked true, mark all its multiples as false.
  3. At the end, all indices with true are prime.

๐Ÿงช Java Implementation

Here's a clean and corrected version of the sieve algorithm in Java:

import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); // Input the upper limit boolean[] isPrime = new boolean[n + 1]; Arrays.fill(isPrime, true); isPrime[0] = false; isPrime[1] = false; for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } } System.out.println("Prime numbers up to " + n + " are:"); for (int i = 2; i <= n; i++) { if (isPrime[i]) { System.out.print(i + " "); } } } } 
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Fixes Made to the Original Code

  • Replaced undeclared variable num with n
  • Fixed syntax error: === โ†’ == (Java uses == for comparison)
  • Included primes up to n (inclusive)
  • Set isPrime[0] and isPrime[1] to false explicitly
  • Used Arrays.fill() for simplicity and clarity

๐ŸŒ Real-Life Applications of Prime Numbers

๐Ÿ” Cryptography & Cybersecurity

Public-key encryption algorithms like RSA use large primes for secure key generation.

๐ŸŽฒ Random Number Generation

Primes are used in linear congruential generators and hash functions to produce uniform randomness.

๐Ÿ’ฝ Hashing Algorithms

Prime table sizes reduce hash collisions, improving performance in hash tables and bloom filters.

๐ŸŽฎ Procedural Generation in Graphics

Games use prime intervals to generate less predictable world seeds and layouts.

๐Ÿ“Š Mathematical Research & Theories

The study of primes leads to groundbreaking theorems and conjectures (e.g., Goldbach's, Riemann Hypothesis).


๐Ÿš€ Try It Out Yourself

You can run the above code on any online Java compiler:

Just copy-paste the code, input a number like 100, and watch the primes roll out!

Top comments (0)