Question

Testing for a prime number by testing divisibility is a bit inefficient. One method to ​efficiently...

Testing for a prime number by testing divisibility is a bit inefficient. One method to ​efficiently find prime numbers is to use what is called a “prime sieve” algorithm. Check out the following link to learn more about a famous instance of such an algorithm. ​ https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes The Sieve of Eratosthenes is a method for finding all prime numbers up to a given number. Your task​ is to write a program that defines an array of 100 boolean values. Your goal is to implement the Sieve of Eratosthenes, which will take this array of 100 values and set all the prime indices​ to ​true​ and ​false​ otherwise. For example, let ​A​ be our array. Then ​A[2]​ will be ​true​ and ​A[4]​ would be ​false, ​etc. C++

please new solution not copied thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code

============================================================================================

#include<iostream>

using namespace std;


void Sieve_of_Eratosthene(bool A[],int n)
{
   int k=2;

   while(k*k<=n)
   {
   //check if number is prime
   if (A[k] == true)
   {
       for(int j=k*2;j<=n;j+=k)
       {
           A[j] = false;
       }
      
   }
   k++;
  
}


  
}

int main()
{
int n=100;
bool A[n+1];

//set bool array to true
for(int i=0;i<=n;i++)
A[i]=true;

Sieve_of_Eratosthene(A,n);


cout <<"Prime numbers using sieve of ethoresis method upto 100: "<<endl;
  
for(int i = 2; i <=n; i++)
{
if (A[i] == true)
{
cout << i << " ";
}
}

cout << endl;


return 0;
}

============================================================================================

Output

Add a comment
Know the answer?
Add Answer to:
Testing for a prime number by testing divisibility is a bit inefficient. One method to ​efficiently...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to...

    The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite lie., not prime) the multiples of each prime, starting with the multiples of 2 The sieve of Eratosthenes can be expressed in pseudocode, as follows: Input: an integer n Let A be an array of 8oo1ean values, indexed by integers 2 to n, initially all set to true. for t - 2, 3,...

  • The following method checks if a number is prime by checking for divisibility on numbers less...

    The following method checks if a number is prime by checking for divisibility on numbers less than it only needs to go up to the square root of n because if n is divisible by a number greater than its square root then it’s divisible by something smaller than it. boolean isPrime(int n){ for (int x = 2; x* x<=n; x++){ if (n % x == 0){ return false; } } } What’s the time complexity of the method above...

  • One way to find prime numbers less than n is to treat them as array indices....

    One way to find prime numbers less than n is to treat them as array indices. Mark each position as True initially, assuming that all numbers are prime. Then, starting with index 2, mark all multiples of 2 (greater than 2) as not prime (False). Repeat this for multiples of 3, then multples of 4, etc. When the algorithm terminates, only prime indices will still be True; everything else will be False. The following function takes an integer n as...

  • #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements....

    #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...

  • Question 0: package hw3; public class Question0 { /* * TODO: Complete the method isSorted that...

    Question 0: package hw3; public class Question0 { /* * TODO: Complete the method isSorted that * takes in as input an array of String * and returns back a boolean value whether it * is sorted in Lexicographical order or not. * You can read up more on this here: * https://en.wikipedia.org/wiki/Lexicographical_order */ public static boolean isSorted(String[] array) { } /* * Do not write any code inside the main method and expect it to get marked. * Any...

  • I am working on my java but I keep getting this one wrong. Method Name: arrayContains...

    I am working on my java but I keep getting this one wrong. Method Name: arrayContains Access modifier: Private Parameters: 1 integer named number Return type: boolean Purpose: This method returns a true or a false indicating if the number is present in an array or not. In order for this method to work, there must be an array already declared. Therefore, in the CLASS BLOCK, declare an array as follows: static int[] listof Numbers = {1, 2, 3, 4,...

  • You are going to create a Queue. (alternately you can create a list and simply implement...

    You are going to create a Queue. (alternately you can create a list and simply implement enqueue and dequeue functions in the List – that will technically make it a queue). You will fill the first list with numbers consecutively numbered from 2 to n where n is entered by the user (we will call this Q1). When creating your Queue object use the correct function names for enqueue and dequeue functions. Again – sorry, cannot use an Javascript array...

  • Python Program Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate...

    Python Program Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate the diameter of the earth. For several decades he served as the director and chief librarian of the famous library in Alexandria. He was highly regarded in the ancient world, but unfortunately only fragments of his writing have survived. The algorithm described for this assignment is known as the Sieve of Eratosthenes. The algorithm is designed to find all prime numbers within a given...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT