-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.cpp
More file actions
50 lines (40 loc) · 1.4 KB
/
primes.cpp
File metadata and controls
50 lines (40 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//============================================================================
// Name : Primes.cpp
// Author : Carlos Lopes
// Version :
// Copyright : Your copyright notice
// Description : isPrime
//============================================================================
#include <iostream>
#include <time.h>
using namespace std;
long diff_nano(struct timespec *start, struct timespec *end)
{
/* ns */
return ( (end->tv_sec * 1000000000 ) + (end->tv_nsec) -
(start->tv_sec * 1000000000) - (start->tv_nsec) );
}
bool isPrime(int num)
{
if (num <= 1) return false;
if (num <= 3) return true;
if (((num % 2) == 0) || ((num % 3) == 0)) return false;
for (int c = 5; c*c <= num; c += 6)
if (((num % c) == 0) || ((num % (c+2)) == 0)) return false;
return true;
}
int main(int argc, char* argv[])
{
struct timespec tstart, tend;
int minprime = 0, maxprime = 20000000, num, count = 0;
std::cout << "C++\n";
clock_gettime(CLOCK_MONOTONIC, &tstart);
for (num = minprime; num <= maxprime; num++)
if (isPrime(num)) count++;
clock_gettime(CLOCK_MONOTONIC, &tend);
double time_spent = diff_nano(&tstart, &tend) / 1000000000.;
std::cout << "Number of primes in the interval ["
<< minprime << "," << maxprime << "]: " << count
<< "\nTime elapsed: " << time_spent << " seconds.\n";
return 0;
}