Question

In: Computer Science

Consider this prime sieve in which an array of numbers of size N is created (i.e....

Consider this prime sieve in which an array of numbers of size N is created (i.e. int nums[10] = {2,3,4,5,6,7,8,9,10,11};) and initialized to contain counting numbers starting with 2. The sieve works by taking each number in the array, and “crossing out” (perhaps via a similar array of Booleans) multiples of that number. So beginning with 2, we would “cross out” 4, 6, 8, 10. Then repeat the process for 3, and so on. Numbers that are already “crossed out” need not be run through the sieve, as we can prove that all of its multiples are in fact crossed out as well. Provide a pseudocode implementation for a threaded implementation of the sieve that uses N threads, with each thread "crossing out" multiples of a given prime base k.

Solutions

Expert Solution

Code:

#include <iostream>
#include <thread>
#include <mutex>

using namespace std;

mutex m;

void cross(int* num,bool* bCrosed,int& number, size_t& numberIndex, const int size)
{
   //Locking for each thread
   m.lock();
   for (size_t i = numberIndex; i < size; i++)
   {
       if (bCrosed[i] && num[i] % number == 0)
       {
           bCrosed[i] = false;
       }
   }
   //Unlocking the Critical section
   m.unlock();
}

int main()
{
   const int size = 10;

   int nums[size] = { 2,3,4,5,6,7,8,9,10,11 };
   bool bCrossed[size] = { true,true,true,true,true,true,true,true,true,true };
   thread t[size];

   for (size_t i = 0; i < 10; i++)
   {
       t[i] = thread(cross, nums,bCrossed,std::ref(nums[i]),std::ref(i),size);
   }

   for (size_t i = 0; i < 10; i++)
   {
       t[i].join();
   }

   cout << "\nCrossed Elements : ";

   for (size_t i = 0; i < 10; i++)
   {
       cout << std::boolalpha << bCrossed[i]<<" ";
   }
  
   return 0;
}

OUTPUT


Related Solutions

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,” Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n - 1, whether the number is prime. In Java
Consider the statement, “For all natural numbers n,n, if nn is prime, then nn is solitary.”...
Consider the statement, “For all natural numbers n,n, if nn is prime, then nn is solitary.” You do not need to know what solitary means for this problem, just that it is a property that some numbers have and others do not. Write the converse and the contrapositive of the statement, saying which is which. Note: the original statement claims that an implication is true for all n,n, and it is that implication that we are taking the converse and...
Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given...
Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given limit. Aim Develop code for implementing the sieve of Eratosthenes. Steps for Completion Implement the isPrime() method of the SieveOfEratosthenes class that should return true if the number is prime, and false otherwise. Consider building the sieve in the class constructor. CODE GIVEN public class SieveOfEratosthenes { public SieveOfEratosthenes(int maxValue) { // Build the sieve here } public boolean isPrime(int value) { // Write...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values. This program should be in assembly language.
Consider sorting n numbers stored in array A by first finding the smallest element of A...
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A and exchange it with A[2]. Continue in this manner for the first n-1 elements of A. a) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm. b) (10 points) What loop invariant does this algorithm maintain? Note: You do not...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax. (no [ ]'s or (ptr+1) stuff. input will be the number of input values to enter followed by the sum to compare with. print out the continuous sub-array of values that are equal to sum or the message 'no sum ofund.' there may be more than one sub-array to be found in...
Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
Suppose we have an array A that contains a prime numbers from 2 to 200 in...
Suppose we have an array A that contains a prime numbers from 2 to 200 in sorted order. How many items of the array A would the binary search algorithm have to examine before concluding that 60 is not in the array A? 30 200 100 6 2- Suppose we have an array that contains 182 village name. These names are sorted alphabetically. How many names would binary search algorithm examine to locate a particular name in the array, in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT