Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- int main() {
- /*Declare Variables*/
- clock_t start, end, begin, stop;
- double runTime, time;
- start = clock();
- int i,primes,num,count,total;
- /*Assign Variables*/
- primes=0;
- num=1;
- begin = clock();
- /*Begin program, accept input*/
- system("cls");
- printf("Please enter the highest prime you want to generate to: ");
- scanf("%d",&total);
- printf("\n");
- /*Begin MATHS!*/
- #pragma omp parallel for schedule(dynamic) reduction(+ : primes)
- for (num = 1; num <= total; num++) {
- i=2;
- while(i<=num) {
- if(num%i==0)
- break;
- i++;
- }
- if(i==num) {
- primes++;
- }
- num++;
- stop = clock();
- time = (stop - begin) / (double)CLOCKS_PER_SEC;
- if (time < 0.1) {
- count++;
- }
- else {
- system("cls");
- printf("Performing %d calculations per second; %d prime numbers calculated\n",count,primes);
- begin = clock();
- count=0;
- }
- }
- /*Print results*/
- end = clock();
- runTime = (end - start) / (double) CLOCKS_PER_SEC;
- printf("This machine calculated all %d prime numbers under %d in %g seconds\n",primes,total,runTime);
- return 0;
- }
Add Comment
Please, Sign In to add comment