-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimes.c
77 lines (60 loc) · 1.58 KB
/
primes.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* @file error.c
* @author Samuel Stolarik (xstola03@stud.fit.vutbr.cz), FIT
*
* @brief IJC-DU1, part A
* prints last 10 primes less than 300000000
* uses Sieve of Eratosthenes implemented in eratosthenes.c
* Used compiler: gcc (GCC) 11.2.0
*
* @date 2022-03-03
*/
#include <stdio.h>
#include <time.h>
#include "bitset.h"
#include "error.h"
//300000000
#define CNT_PRIMES 300000000//primes less than
#define PRT_PRIMES 10 //how many primes to print
//---prototypes---
static void print_primes(const bitset_t primes, int count);
extern void Eratosthenes(bitset_t array);
int main()
{
clock_t start = clock();
bitset_create(primes, CNT_PRIMES);
Eratosthenes(primes);
print_primes(primes, PRT_PRIMES);
fprintf(stderr, "Time=%.3g\n", (double)(clock()-start)/CLOCKS_PER_SEC);
return 0;
}
/**
* @brief print last "count" primes from bitset "primes"
* if there is less then "count" primes, prints only as much as possible
*
* @param primes
* @param count
*/
static void print_primes(const bitset_t primes, int count)
{
bitset_t primes_to_print = calloc(sizeof(unsigned long int), count);
if (primes_to_print == NULL)
{
error_exit("Ran out of memory.");
}
short cur = count -1;
for (bitset_index_t i = bitset_size(primes) -1; i >= 2; i--)
{
if (!bitset_getbit(primes, i) && cur >= 0)
{
primes_to_print[cur--] = i;
}
}
for(short i = 0; i < count; i++)
{
if (primes_to_print[i])
printf("%lu\n",primes_to_print[i]);
}
free(primes_to_print);
return;
}