-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorization.cpp
56 lines (38 loc) · 1.12 KB
/
factorization.cpp
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
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "factorize.h"
#include "bignum/wideint.h"
#ifdef __SIZEOF_INT128__
using int128_t = __int128_t;
#else
using int128_t = wideint<128>;
#endif
static PyObject *factorize(PyObject* self, PyObject* args)
{
PyObject *py_n1, *py_n2;
int128_t pq;
int128_t p, q;
if (!PyArg_ParseTuple(args, "O", &py_n1))
return NULL;
_PyLong_AsByteArray((PyLongObject*)py_n1, (uint8_t *)&pq, sizeof(pq), 1, 0);
p = BrentRichard(pq);
q = pq / p;
py_n1 = _PyLong_FromByteArray((const uint8_t *)&p, sizeof(p), 1, 0);
py_n2 = _PyLong_FromByteArray((const uint8_t *)&q, sizeof(q), 1, 0);
return (p < q) ? Py_BuildValue("OO", py_n1, py_n2) : Py_BuildValue("OO", py_n2, py_n1);
}
static PyMethodDef methods[] = {
{"factorize", factorize, METH_VARARGS, "Factorizes the given large integer."},
{NULL, NULL, 0, NULL}
};
static PyModuleDef module = {
PyModuleDef_HEAD_INIT,
"factorization",
"factorize Calculation",
-1,
methods
};
PyMODINIT_FUNC PyInit_factorization(void)
{
return PyModule_Create(&module);
}