-
Notifications
You must be signed in to change notification settings - Fork 33
/
bi-unitary_divisors.pl
79 lines (64 loc) · 1.62 KB
/
bi-unitary_divisors.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
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
#!/usr/bin/perl
# Author: Trizen
# Date: 13 September 2023
# https://github.com/trizen
# Generate the bi-unitary divisors of n.
# See also:
# https://oeis.org/A188999
# https://oeis.org/A222266
use 5.036;
use ntheory qw(:all);
sub gcud (@list) { # greatest common unitary divisor
my $g = gcd(@list);
foreach my $n (@list) {
next if ($n == 0);
while (1) {
my $t = gcd($g, divint($n, $g));
last if ($t == 1);
$g = divint($g, $t);
}
last if ($g == 1);
}
return $g;
}
sub bi_unitary_divisors ($n) {
my @d = (1);
foreach my $pp (factor_exp($n)) {
my ($p, $e) = @$pp;
my @t;
my $r = 1;
foreach my $j (1 .. $e) {
$r = mulint($r, $p);
if (gcud($r, divint($n, $r)) == 1) {
push @t, map { mulint($r, $_) } @d;
}
}
push @d, @t;
}
return sort { $a <=> $b } @d;
}
foreach my $n (1 .. 20) {
my @biudivisors = bi_unitary_divisors($n);
say "bi-udivisors of $n: [@biudivisors]";
}
__END__
bi-udivisors of 1: [1]
bi-udivisors of 2: [1 2]
bi-udivisors of 3: [1 3]
bi-udivisors of 4: [1 4]
bi-udivisors of 5: [1 5]
bi-udivisors of 6: [1 2 3 6]
bi-udivisors of 7: [1 7]
bi-udivisors of 8: [1 2 4 8]
bi-udivisors of 9: [1 9]
bi-udivisors of 10: [1 2 5 10]
bi-udivisors of 11: [1 11]
bi-udivisors of 12: [1 3 4 12]
bi-udivisors of 13: [1 13]
bi-udivisors of 14: [1 2 7 14]
bi-udivisors of 15: [1 3 5 15]
bi-udivisors of 16: [1 2 8 16]
bi-udivisors of 17: [1 17]
bi-udivisors of 18: [1 2 9 18]
bi-udivisors of 19: [1 19]
bi-udivisors of 20: [1 4 5 20]