forked from flintlib/flint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum_of_squares.c
137 lines (113 loc) · 3.23 KB
/
sum_of_squares.c
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
/*=============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2011 Fredrik Johansson
******************************************************************************/
#include "fmpz.h"
#include "arith.h"
static void
sum_of_two_squares(fmpz_t r, const fmpz_t n)
{
fmpz_factor_t fac;
slong i;
fmpz_factor_init(fac);
fmpz_factor(fac, n);
fmpz_one(r);
for (i = 0; i < fac->num; i++)
{
const int res = fmpz_fdiv_ui(fac->p + i, 4);
if (res == 1)
{
fac->exp[i]++;
fmpz_mul_ui(r, r, fac->exp[i]);
}
else if (res == 3)
{
if (fac->exp[i] % 2)
{
fmpz_zero(r);
break;
}
}
}
fmpz_mul_ui(r, r, 4);
fmpz_factor_clear(fac);
}
static void
sum_of_four_squares(fmpz_t r, const fmpz_t n)
{
const mp_bitcnt_t v = fmpz_val2(n);
if (v == 0)
{
arith_divisor_sigma(r, n, 1);
fmpz_mul_ui(r, r, 8);
}
else
{
fmpz_tdiv_q_2exp(r, n, v);
arith_divisor_sigma(r, r, 1);
fmpz_mul_ui(r, r, 24);
}
}
static void
sum_of_squares_recursive(fmpz_t r, slong k, ulong n)
{
fmpz_t t, u;
slong i, j;
fmpz_init(t);
fmpz_init(u);
fmpz_zero(r);
for (i = j = 0; j <= n; i++)
{
fmpz_set_ui(u, n - j);
arith_sum_of_squares(t, k - 1, u);
if (j > 0)
fmpz_mul_ui(t, t, 2);
fmpz_add(r, r, t);
j += 2 * i + 1;
}
fmpz_clear(t);
fmpz_clear(u);
}
static void
sum_of_squares_series(fmpz_t r, ulong k, slong n)
{
fmpz * t;
t = _fmpz_vec_init(n + 1);
arith_sum_of_squares_vec(t, k, n + 1);
fmpz_set(r, t + n);
_fmpz_vec_clear(t, n + 1);
}
void
arith_sum_of_squares(fmpz_t r, ulong k, const fmpz_t n)
{
if (fmpz_sgn(n) <= 0 || k == 0)
fmpz_set_ui(r, fmpz_is_zero(n) != 0);
else if (k == 1)
fmpz_set_ui(r, 2 * (fmpz_is_square(n) != 0));
else if (k == 2)
sum_of_two_squares(r, n);
else if (k == 4)
sum_of_four_squares(r, n);
else if (k == 3 || k == 5)
sum_of_squares_recursive(r, k, fmpz_get_ui(n));
else if (fmpz_fits_si(n))
sum_of_squares_series(r, k, fmpz_get_ui(n));
else
{
flint_printf("Exception (arith_sum_of_squares). n is too large.\n");
abort();
}
}