Computer Science, asked by kenchanna6714, 10 months ago

Multithreaded program that outputs prime numbers in c

Answers

Answered by MichMich0945
2

01

#include <iostream>

02

using namespace std;

03

 

04

int main(){

05

clock_t start = clock();

06

 int count = 0;

07

 

08

 for(int i = 2; i < 5000000; i++){

09

   bool prime = true;

10

   for(int j = 2; j*j <= i; j++){

11

     if(i % j == 0){

12

       prime = false;

13

       break;

14

     }

15

   }

16

   if(prime){

17

     count++;

18

     if(count <= 100)

19

       cout << i << " ";

20

     else if(count > 348413)

21

       cout << i << " ";

22

   }

23

 }

24

 

25

 clock_t end = clock();

26

 cout << "\n\nThere are " << count << " primes under 5,000,000." << endl;

27

 cout << "Program took " << (double)(end  - start) / CLOCKS_PER_SEC << "seconds." << endl;

28

}

Similar questions