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
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'
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 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
Discrete Math In this problem, we will implement the RSA algorithm to encrypt and decrypt the...
Discrete Math In this problem, we will implement the RSA algorithm to encrypt and decrypt the message ”148”.For this exercise, you may want to use some kind of calculator that can compute the mod function. 1. Set the primes p and q as follows:p=31 and q=47. What are the values for N and φ? 2.The value for e is chosen to be 11. Use Euclid’s algorithm to verify that e and φ are relatively prime and to find d, the...
1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt...
1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order. 2)You must implement a recursive Mergesort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order. My List txt Values 7 3 4 1 4 4 9 9 4 8 4 5 3 9 2 3 7 0 6 4 4 5 0 1 9 2 1...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT