-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
54 lines (41 loc) · 1.22 KB
/
main.c
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
51
52
53
54
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
//arg
unsigned int max = atoi(argv[1]);
unsigned int count, i, div_count, divisor;
//time var
double timeStart, timeEnd, Texec;
struct timeval tp;
//start time
gettimeofday(&tp, NULL);
timeStart = (double) (tp.tv_sec) + (double) (tp.tv_usec) / 1e6;
//iterate between min to max
//optimize loop by going 100 to 1 instead of the inverse
for(i = 1; i <= max; i++)
{
//check if current number has height divisors. Actually, check for 6 because a number will always has 1 and itself as divisors
div_count = 0;
divisor = 1;
while(divisor <= i)
{
if (i % divisor == 0){
div_count += 1;
}
divisor += 1;
}
if(div_count == 8)
{
count += 1;
}
}
//end timer
gettimeofday(&tp, NULL);
timeEnd = (double) (tp.tv_sec) + (double) (tp.tv_usec) / 1e6;
Texec = timeEnd - timeStart;
printf("\nExecution time : %f seconds", Texec);
printf("\nCount : %d\n", count);
return 0;
}