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
/*
Copyright (C) 2011 Fredrik Johansson
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "longlong.h"
#include "fmpz.h"
static inline ulong rfac(ulong x, ulong b)
{
ulong i, c = x;
for (i = 1; i < b; i++)
c *= x + i;
return c;
}
/* Assumes x positive, b > a. b must also be small enough to
avoid integer overflow, which is no problem if the result
is to fit in memory. */
void
_fmpz_rfac_ui(fmpz_t r, const fmpz_t x, ulong a, ulong b)
{
if (b - a == 1)
{
fmpz_add_ui(r, x, a);
}
else if ((*x <= COEFF_MAX) && (b - a < 60))
{
ulong step, bits, factors_per_limb;
ulong y = *x;
/* Bound size of largest factor */
bits = FLINT_BIT_COUNT(y + a + b - 1);
/* The result fits in a single limb */
if ((b - a) * bits < FLINT_BITS)
step = factors_per_limb = b - a;
else
{
factors_per_limb = FLINT_BITS / bits;
step = FLINT_MIN(b - a, factors_per_limb);
}
fmpz_set_ui(r, rfac(y + a, step));
a += step;
while (a < b)
{
step = FLINT_MIN(b - a, factors_per_limb);
fmpz_mul_ui(r, r, rfac(y + a, step));
a += step;
}
}
else
{
fmpz_t t, u;
ulong m = (a + b) / 2;
fmpz_init(t);
fmpz_init(u);
_fmpz_rfac_ui(t, x, a, m);
_fmpz_rfac_ui(u, x, m, b);
fmpz_mul(r, t, u);
fmpz_clear(t);
fmpz_clear(u);
}
}
void
fmpz_rfac_ui(fmpz_t r, const fmpz_t x, ulong n)
{
if (n == 0)
{
fmpz_one(r);
}
else if (n == 1)
{
fmpz_set(r, x);
}
else if (fmpz_is_zero(x))
{
fmpz_zero(r);
}
else if (fmpz_sgn(x) < 0)
{
fmpz_t t;
fmpz_init(t);
fmpz_add_ui(t, x, n - 1);
if (fmpz_sgn(t) >= 0)
{
fmpz_zero(r);
}
else
{
fmpz_neg(t, t);
fmpz_rfac_ui(r, t, n);
if (n % 2 == 1)
fmpz_neg(r, r);
}
fmpz_clear(t);
}
else
{
_fmpz_rfac_ui(r, x, 0, n);
}
}
void
fmpz_rfac_uiui(fmpz_t r, ulong x, ulong n)
{
if (n == 0)
{
fmpz_one(r);
}
else if (n == 1)
{
fmpz_set_ui(r, x);
}
else if (x == 0)
{
fmpz_zero(r);
}
else if (x <= COEFF_MAX)
{
_fmpz_rfac_ui(r, (fmpz *) &x, 0, n);
}
else
{
fmpz_t tmp;
fmpz_init_set_ui(tmp, x);
fmpz_rfac_ui(r, tmp, n);
fmpz_clear(tmp);
}
}