-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy path07_python.c
More file actions
49 lines (41 loc) · 1.11 KB
/
Copy path07_python.c
File metadata and controls
49 lines (41 loc) · 1.11 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
#include <Python.h>
static PyObject* spam_sum_of_squares(PyObject *self, PyObject
*args){
/* Declare the variables */
int n;
int sum = 0;
/* Parse the arguments */
if(!PyArg_ParseTuple(args, "i", &n)){
return NULL;
}
/* The actual summing code */
for(int i=0; i<n; i++){
if((i * i) < n){
sum += i * i;
}else{
break;
}
}
/* Return the number but convert it to a Python object first
*/
return PyLong_FromLong(sum);
}
static PyMethodDef spam_methods[] = {
/* Register the function */
{"sum_of_squares", spam_sum_of_squares, METH_VARARGS,
"Sum the perfect squares below n"},
/* Indicate the end of the list */
{NULL, NULL, 0, NULL},
};
static struct PyModuleDef spam_module = {
PyModuleDef_HEAD_INIT,
"spam", /* Module name */
NULL, /* Module documentation */
-1, /* Module state, -1 means global. This parameter is
for sub-interpreters */
spam_methods,
};
/* Initialize the module */
PyMODINIT_FUNC PyInit_spam(void){
return PyModule_Create(&spam_module);
}