-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfastcount.c
More file actions
61 lines (44 loc) · 1.2 KB
/
fastcount.c
File metadata and controls
61 lines (44 loc) · 1.2 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
#include <Python.h>
int c_prime_counter(int frm, int til) {
int primecount = 0;
for (int num = frm; num <= til; num++) {
int flag = 0;
if (num > 1) {
for (int candidate = 2; candidate < num; candidate++) {
if ((num % candidate) == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
primecount++;
}
}
}
return primecount;
}
static PyObject *py_primecounter(PyObject *self, PyObject *args) {
// Declare two pointers
int *n_frm, *n_til = NULL;
// Parse arguments - expects two integers that will be mapped to n_frm and n_til
if (!PyArg_ParseTuple(args, "ii", &n_frm, &n_til)) {
return NULL;
}
// Call c-function
int found_primes = c_prime_counter(n_frm, n_til);
return PyLong_FromLong(found_primes);
}
static PyMethodDef CountingMethods[] = {
{"primecounter", py_primecounter, METH_VARARGS, "Function for counting primes in a range in c"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef fastcountmodule = {
PyModuleDef_HEAD_INIT,
"Fastcount", // module name
"C library for counting fast",
-1,
CountingMethods
};
PyMODINIT_FUNC PyInit_Fastcount(void) {
return PyModule_Create(&fastcountmodule);
};