Question

Implement THIS algorithm of the sieve of Eratosthenes

Implement THIS algorithm of the sieve of Eratosthenes

Homework Answers

Answer #1

Since you have not mentioned the language of your preference, I am providing the code in JAVA.

CODE

import java.util.Scanner;

class SieveOfEratosthenes

{

public static void findPrimesUsingSieveOfEratosthenes(int n)

{

boolean prime[] = new boolean[n+1];

for(int i=0;i<n;i++)

prime[i] = true;

for(int p = 2; p*p <=n; p++)

{

if(prime[p] == true)

{

for(int i = p*p; i <= n; i += p)

prime[i] = false;

}

}

for(int i = 2; i <= n; i++)

{

if(prime[i] == true)

System.out.print(i + " ");

}

}

public static void main(String args[])

{

int n;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the value of n : ");

n = sc.nextInt();

System.out.println("Prime numbers upto " + n + " are given below: ");

findPrimesUsingSieveOfEratosthenes(n);

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement in matlab the Equalization Algorithm
Implement in matlab the Equalization Algorithm
implement hungarian algorithm with python
implement hungarian algorithm with python
In a standard sieve analysis, which of the following sieve size is the boundary between Course...
In a standard sieve analysis, which of the following sieve size is the boundary between Course Grain and Fine Grain Soil in the USCS Soil Classification System? No. 4 Sieve No. 200 Sieve No. 40 Sieve No. 16 Sieve
Implement an algorithm in Ruby that removes the elements that are in matrix A that are...
Implement an algorithm in Ruby that removes the elements that are in matrix A that are corresponding with other matrices    Input MatrixA = 'whiteblueyellowgreenpurplewhiteyellow' MatrixB = 'blue' MatrixC = 'white' Output Result = 'yellowgreenpurpleyellow'
Implement Dijkstra’s Shortest Path Algorithm in C using an Adjacency List (linked list) graph.
Implement Dijkstra’s Shortest Path Algorithm in C using an Adjacency List (linked list) graph.
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for...
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for reversing a singly linked list L, so that the ordering of the nodes becomes opposite of what it was before. What is the running time of your algorithm on a list of n values? Provide both the growth function and BigOh complexity. Create a driver class to test your implementation.
In this lab, you will implement the priority scheduling algorithm based on the nice value for...
In this lab, you will implement the priority scheduling algorithm based on the nice value for xv6. 1. Write a user level program nice.c that will accepts two arguments: pid and priority which will change the nice value of the process with process id pid to priority, i.e. nice pid priority.
Write and test code in MIPS assembly language program to implement algorithms "The Non-Restoring Algorithm "of...
Write and test code in MIPS assembly language program to implement algorithms "The Non-Restoring Algorithm "of an 8-bit integer the user shoud insert the number then by the algo well find the square root
The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version...
The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version of the binary search algorithm. The program should prompt Y / y to continue searching and N / n to discontinue. If an item is found in the list display the following message: x found at position y else: x is not in the list Use the following list of integers when testing your program: 2, 6, 8, 13, 25, 33, 39, 42, 53,...
LANGUAGE: JAVA (using greedy algorithm) Implement a dynamic programming solution to identify the least-cost way of...
LANGUAGE: JAVA (using greedy algorithm) Implement a dynamic programming solution to identify the least-cost way of performing a chained matrix multiplication. (this part is just for reference, need the code for below). (I want this second part to be solved using greedy algo)--> Also implement, to compare the costs, greedy way of performing chained matrix multiplication
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT