-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsieve_eratosthenes.c
More file actions
177 lines (165 loc) · 4.94 KB
/
sieve_eratosthenes.c
File metadata and controls
177 lines (165 loc) · 4.94 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* @file = sieve_eratosthenes.c
* @author = https://github.com/TotallyNotChase
* @licence = public domain
*/
/*
* The following sieve of eratosthenes is a C implementation of an
* improved sieve of eratosthenes algorithm written by Kim Wilsch in C++
* View the documentation of the algo here - [https://github.com/kimwalisch/primesieve/wiki/Segmented-sieve-of-Eratosthenes]
* ///////////////////////////////////////////////////////////////////////
* COPYRIGHT NOTICE:-
* BSD 2-Clause License
*
* Copyright (c) 2010 - 2019, Kim Walisch.
* All rights reserved.
* ///////////////////////////////////////////////////////////////////////
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<stdint.h>
#include<inttypes.h>
#include<math.h>
#include<time.h>
// Defining custom structs to imitate vectors in C++
typedef struct uint64_vector
{
// For integer values
uint64_t* data;
uint64_t capacity, size;
} uint64vec_t;
typedef struct bool_vector
{
// For boolean values
bool* data;
uint64_t size, count;
} boolvec_t;
// The Level 1 Data cache for the user's CPU (must be per core)
uint64_t L1D_CACHE;
uint64vec_t create_uint64_vector(uint64_t size)
{
// Creates a uint64_t vector and returns it
uint64vec_t vector;
vector.data = malloc(size * sizeof(uint64_t));
if (vector.data == NULL)
{
printf("\nAn error occured while allocating memory for uint64_vector\n");
exit(1);
}
vector.capacity = size;
vector.size = 0;
return vector;
}
boolvec_t create_bool_vector(uint64_t size)
{
// Creates a bool vector, assigns all slots to True, and returns it
boolvec_t vector;
vector.data = malloc(size * sizeof(bool));
if (vector.data == NULL)
{
printf("\nAn error occured while allocating memory for bool_vector\n");
exit(1);
}
memset(vector.data, true, sizeof(bool) * size);
vector.size = size;
vector.count = 0;
return vector;
}
size_t approximate_size(uint64_t limit)
{
int i;
float x = 1;
for (i = log10(limit); i > 0; i--)
{
x *= 2.4;
}
return x;
}
void uint64_vector_append(uint64vec_t *vector, uint64_t data)
{
// Appends values to uint64 vector, reallocates if necessary
if ((vector->size + 1) < vector->capacity)
{
vector->data[vector->size++] = data;
return;
}
vector->data = realloc(vector->data, (vector->capacity *= 2) * sizeof(uint64_t));
if (vector->data == NULL)
{
printf("\nAn error occured while re-allocating memory for uint64_vector\n");
exit(1);
}
vector->data[vector->size++] = data;
}
void segmented_sieve(uint64_t limit)
{
int count = 1;
// A detailed explaination of this algo can be found in the wiki mentioned above
int64_t low, high, i = 3, j, k, n = 3, s = 3;
size_t i_size, approx_arr_size = approximate_size(limit);
uint64_t sqrtval = (uint64_t) sqrt(limit);
uint64_t segment_size = sqrtval < L1D_CACHE ? L1D_CACHE : sqrtval; // This is a imitation of std::max()
uint64vec_t prime_arr = create_uint64_vector(approx_arr_size); // An assumption on approx size
uint64vec_t multiples = create_uint64_vector(approx_arr_size);
boolvec_t sieve = create_bool_vector(segment_size);
boolvec_t is_prime = create_bool_vector(sqrtval + 1);
printf("2 ");
for (low = 0; low <= limit; low += segment_size)
{
memset(sieve.data, true, sizeof(bool) * sieve.size);
high = low + segment_size - 1;
high = high < limit ? high : limit;
for (; i * i <= high; i += 2)
{
if (is_prime.data[i])
{
for (j = i * i; j <= sqrtval; j += i)
{
is_prime.data[j] = false;
}
}
}
for (; s * s <= high; s += 2)
{
if (is_prime.data[s])
{
uint64_vector_append(&prime_arr, s);
uint64_vector_append(&multiples, s * s - low);
}
}
for (i_size = 0; i_size < prime_arr.size; i_size++)
{
j = multiples.data[i_size];
for (k = prime_arr.data[i_size] * 2; j < segment_size; j += k)
{
sieve.data[j] = false;
}
multiples.data[i_size] = j - segment_size;
}
for (; n <= high; n += 2)
{
if (sieve.data[n - low])
{
printf("%" SCNu64 " ", n);
count++;
}
}
}
printf("\nFound primes: %d", count);
}
int main()
{
uint64_t N;
printf("Enter your CPU's L1D_CACHE per thread (in bytes): ");
scanf("%" SCNu64, &L1D_CACHE);
printf("Enter upper limit for prime check: ");
scanf("%" SCNu64, &N);
clock_t t0 = clock();
segmented_sieve(N);
clock_t t1 = clock();
double time_taken = (double) (t1 - t0) / CLOCKS_PER_SEC;
printf("\nDone! Time taken: %f\n", time_taken);
return 0;
}