Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / mfactor.cpp
blob6ef1ff91f47c30e3c4d82e7f55aa02dcb0640ef1
1 // For odd n, returns the largest factor less than or equal to sqrt(n)
3 #include "stdafx.h"
4 #include "defs.h"
6 #if 0 // not used anymore
8 unsigned int *
9 mfactor(unsigned int *n)
11 unsigned int *r, *root, *t, *two, *x, *y;
13 two = mint(2);
15 root = msqrt(n);
17 // y = 1;
19 y = mint(1);
21 // x = 2 isqrt(n) + 1
23 t = madd(root, root);
24 x = madd(t, y);
25 mfree(t);
27 // r = isqrt(n) ^ 2 - n
29 t = mmul(root, root);
30 r = msub(t, n);
31 mfree(t);
33 mfree(root);
35 while (1) {
37 if (MZERO(r)) {
39 // n = (x - y) / 2
41 t = msub(x, y);
42 n = mdiv(t, two);
43 mfree(t);
45 mfree(r);
46 mfree(x);
47 mfree(y);
48 mfree(two);
50 return n;
53 // r = r + x
55 t = madd(r, x);
56 mfree(r);
57 r = t;
59 // x = x + 2
61 t = madd(x, two);
62 mfree(x);
63 x = t;
65 while (1) {
67 // r = r - y
69 t = msub(r, y);
70 mfree(r);
71 r = t;
73 // y = y + 2
75 t = madd(y, two);
76 mfree(y);
77 y = t;
79 if (MSIGN(r) == -1 || MZERO(r))
80 break;
85 void
86 test_mfactor(void)
88 unsigned int *n;
89 n = mint(377);
90 n = mfactor(n);
91 printf("%d", n[0]);
94 #endif