-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathgaussian_factors.pl
More file actions
130 lines (100 loc) · 3.04 KB
/
Copy pathgaussian_factors.pl
File metadata and controls
130 lines (100 loc) · 3.04 KB
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
#!/usr/bin/perl
# Author: Trizen
# Date: 13 June 2022
# https://github.com/trizen
# Find the factors of a Gaussian integer.
# See also:
# https://www.alpertron.com.ar/GAUSSIAN.HTM
# https://en.wikipedia.org/wiki/Table_of_Gaussian_integer_factorizations
use 5.020;
use strict;
use warnings;
use ntheory qw(:all);
use experimental qw(signatures);
sub gaussian_mul ($xa, $xb, $ya, $yb) {
($xa * $ya - $xb * $yb, $xa * $yb + $xb * $ya)
}
sub gaussian_div ($xa, $xb, $ya, $yb) { # floor division
my $t = $ya * $ya + $yb * $yb;
(
divint($ya * $t * $xa - $t * -$yb * $xb, $t * $t),
divint($ya * $t * $xb + $t * -$yb * $xa, $t * $t)
);
}
sub gaussian_is_div ($xa, $xb, $ya, $yb) {
my ($ta, $tb) = gaussian_mul($ya, $yb, gaussian_div($xa, $xb, $ya, $yb));
$xa - $ta == 0 and $xb - $tb == 0;
}
sub primitive_sum_of_two_squares ($p) {
if ($p == 2) {
return (1, 1);
}
my $s = sqrtmod(-1, $p) || return;
my $q = $p;
while ($s * $s > $p) {
($s, $q) = ($q % $s, $s);
}
($s, $q % $s);
}
sub gaussian_factors ($x, $y = 0) {
return if ($x == 0 and $y == 0);
my $n = ($x * $x + $y * $y);
my @factors;
foreach my $pe (factor_exp($n)) {
my ($p, $e) = @$pe;
if ($p == 2) {
while (gaussian_is_div($x, $y, 1, 1)) {
push @factors, [1, 1];
($x, $y) = gaussian_div($x, $y, 1, 1);
}
}
elsif ($p % 4 == 3) {
while (gaussian_is_div($x, $y, $p, 0)) {
push @factors, [$p, 0];
($x, $y) = gaussian_div($x, $y, $p, 0);
}
}
elsif ($p % 4 == 1) {
my ($a, $b) = primitive_sum_of_two_squares($p);
while (gaussian_is_div($x, $y, $a, $b)) {
push @factors, [$a, $b];
($x, $y) = gaussian_div($x, $y, $a, $b);
}
while (gaussian_is_div($x, $y, $a, -$b)) {
push @factors, [$a, -$b];
($x, $y) = gaussian_div($x, $y, $a, -$b);
}
}
}
if ($x == 1 and $y == 0) {
## ok
}
else {
push @factors, [$x, $y];
}
@factors = sort {
($a->[0] <=> $b->[0]) ||
($a->[1] <=> $b->[1])
} @factors;
my %count;
$count{join(' ', @$_)}++ for @factors;
my %seen;
my @factor_exp =
map { [$_, $count{join(' ', @$_)}] }
grep { !$seen{join(' ', @$_)}++ } @factors;
return @factor_exp;
}
my $z = [440, -55];
my @factors = gaussian_factors($z->[0], $z->[1]);
say join(' ', map { '[' . join(', ', @{$_->[0]}) . ']' . ($_->[1] > 1 ? ('^' . $_->[1]) : '') } @factors);
my ($x, $y) = (1, 0);
foreach my $pe (@factors) {
my ($p, $e) = @$pe;
for (1 .. $e) {
($x, $y) = gaussian_mul($x, $y, $p->[0], $p->[1]);
}
}
say "Product of factors: [$x, $y]";
__END__
[2, -1] [2, 1]^2 [3, -2] [11, 0]
Product of factors: [440, -55]