-
Notifications
You must be signed in to change notification settings - Fork 34
/
number_of_partitions_into_2_positive_squares.pl
executable file
·54 lines (39 loc) · 1.48 KB
/
number_of_partitions_into_2_positive_squares.pl
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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 24 September 2019
# https://github.com/trizen
# Count the number of representations of n as the sum of two non-zero squares, ignoring order and signs (not necesarily distinct).
# See also:
# https://oeis.org/A025426 -- Number of partitions of n into 2 nonzero squares.
# https://oeis.org/A000161 -- Number of partitions of n into 2 squares.
# https://mathworld.wolfram.com/SumofSquaresFunction.html
# https://en.wikipedia.org/wiki/Fermat%27s_theorem_on_sums_of_two_squares
use 5.020;
use strict;
use warnings;
use experimental qw(signatures);
use ntheory qw(divisors valuation factor_exp vecsum vecprod);
# Number of solutions to `n = a^2 + b^2, with 0 < a <= b.
sub r2_positive($n) {
my $B = 1;
my $a0 = 0;
if ($n % 2 == 0) {
$a0 = valuation($n, 2);
$n >>= $a0;
}
foreach my $p (factor_exp($n)) {
my $r = $p->[0] % 4;
if ($r == 3) {
$p->[1] % 2 == 0 or return 0;
}
if ($r == 1) {
$B *= $p->[1] + 1;
}
}
($B % 2 == 0) ? ($B >> 1) : (($B - (-1)**$a0) >> 1);
}
foreach my $n (1 .. 100) {
print(r2_positive($n), ", ");
}
__END__
0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1,