- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sieve of Eratosthenes in java
Sieve of Eratosthenes is the ancient algorithm to find prime numbers up to a given number.
Algorithm
1. Generate integers from 2 to n (Given number).
2. Counting from 2 mark every 2nd integer. (multiples of 2)
3. Now, starting from 3 mark every third integer. (multiples of 3)
4. Finally, marking from 5 mark every 5th integer.(multiples of 5)
Program
import java.util.Scanner; public class SievePrimeFactors { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int num = sc.nextInt(); boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } System.out.println("List of prime numbers upto given number are : "); for (int i = 2; i< bool.length; i++) { if(bool[i]==true) { System.out.println(i); } } } }
Output
Enter a number 50 List of prime numbers up to given number are : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
- Related Articles
- Python Program for Sieve of Eratosthenes
- Using Sieve of Eratosthenes to find primes JavaScript
- Java Program to get prime numbers using the Sieve of Eratosthenes algorithm
- C++ Program to Implement Sieve of eratosthenes to Generate Prime Numbers Between Given Range
- Bitwise Sieve in C++
- Prime Factorization using Sieve O(log n) for multiple queries in C++
- C++ Program to Implement Sieve of Atkin to Generate Prime Numbers Between Given Range
- Name the conducting tissue of plants which is made of sieve tubes along with companion cells.
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
- C++ Program to Implement Wheel Sieve to Generate Prime Numbers Between Given Range
- C++ Program to Implement Segmented Sieve to Generate Prime Numbers Between Given Range
- What substance/substances are transported in plants by :(a) xylem vessels and tracheids ?(b) sieve tubes (or phloem) ?
- The water conducting tissue generally present in gymnosperm is(a)vessels(b)sieve tube(c)tracheids(d)xylem fibres
- The dead element present in the phloem is(a)companion cells(b)phloem fibres(c)phloem parenchyma(d)sieve tubes
- Which one of the following has cytoplasm but no nucleus:(a) xylem vessel (b) sieve tube (c) tracheid (d) companion cell

Advertisements