Attachment 'primes.c'
Download 1 #include <stdio.h>
2 #include <time.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <errno.h>
6
7 #define MAX_PRIME 10000000
8
9 int main(int argc, char ** argv)
10 {
11 int seconds = 5;
12 time_t start, end, run_time, end1, run_time1;
13 unsigned long i, num, primes = 0;
14 start = time(NULL);
15 printf("%u\n", 1);
16 for (num = 1; num <= MAX_PRIME; ++num)
17 {
18 for (i = 2; (i <= num) && (num % i != 0); ++i);
19 if (i == num)
20 {
21 ++primes;
22 printf("%u\n", i);
23 }
24 end1 = time(NULL);
25 run_time1 = (end1 - start);
26 if ( run_time1 >= seconds ) break;
27 }
28 end = time(NULL);
29 run_time = (end - start);
30 printf("This program calculated %d prime numbers under %d "
31 "in %d seconds\n", primes, num, run_time);
32 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.