Factor-perfect numbers: Difference between revisions

Added Perl
m (→‎{{header|Raku}}: use 'state' variable instead of global, syntax twiddles)
(Added Perl)
Line 283:
0, 1, 48, 1280, 2496, 28672, 29808, 454656, 2342912,
</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl" line>use v5.36;
 
sub table (@V) { my $t = 3 * (my $w = 2 + 20); ( sprintf( ('%-'.$w.'s')x@V, @V) ) =~ s/.{1,$t}\K/\n/gr }
 
sub proper_divisors ($x) {
my @l;
@l = 1 if $x > 1;
for my $d (2 .. int sqrt $x) {
if (0 == $x % $d) { push @l, $d; my $y = int($x/$d); push @l, $y if $y != $d }
}
@l
}
 
sub erdosFactorCount ($n) {
my @foo = proper_divisors($n); shift @foo;
state %cache;
my ($sum,@divs) = (0, @foo); #(proper_divisors $n)[1..*]);
for my $d (@divs) {
my $t = int($n/$d);
$cache{$t} = erdosFactorCount($t) unless $cache{$t};
$sum += $cache{$t}
}
++$sum
}
 
sub moreMultiples ($to, $from) {
my @oneMores;
for my $j (@$from) {
push @oneMores, [@$to, $j] if $j > $$to[-1] && 0 == $j % $$to[-1]
}
return unless @oneMores;
for (0 .. $#oneMores) {
push @oneMores, moreMultiples($oneMores[$_], $from);
}
@oneMores
}
 
my @listing = [1];
push @listing, moreMultiples [1], [proper_divisors(48)];
map { push @$_, 48 } @listing;
 
my @lists; map { push @lists, join ' ', @$_ } @listing;
say @listing . " sequences using first definition:\n" . table(@lists);
 
my @listing2;
for my $j (0.. $#listing) {
my @seq = @{$listing[$j]};
push @seq, 48 if $seq[-1] != 48;
push @listing2, join ' ', map { int $seq[$_] / $seq[$_-1] } 1 .. $#seq;
}
 
say @listing2 . " sequences using second definition:\n" . table(@listing2);
 
my($n,@fpns) = (4, 0,1);
while ($#fpns < 6) { push(@fpns, $n) if erdosFactorCount($n) == $n; $n += 4 }
say "OEIS A163272: @fpns";</syntaxhighlight>
{{out}}
<pre>48 sequences using first definition:
1 48 1 2 48 1 24 48
1 3 48 1 16 48 1 4 48
1 12 48 1 6 48 1 8 48
1 2 24 48 1 2 16 48 1 2 4 48
1 2 12 48 1 2 6 48 1 2 8 48
1 2 4 24 48 1 2 4 16 48 1 2 4 12 48
1 2 4 8 48 1 2 4 12 24 48 1 2 4 8 24 48
1 2 4 8 16 48 1 2 12 24 48 1 2 6 24 48
1 2 6 12 48 1 2 6 12 24 48 1 2 8 24 48
1 2 8 16 48 1 3 24 48 1 3 12 48
1 3 6 48 1 3 12 24 48 1 3 6 24 48
1 3 6 12 48 1 3 6 12 24 48 1 4 24 48
1 4 16 48 1 4 12 48 1 4 8 48
1 4 12 24 48 1 4 8 24 48 1 4 8 16 48
1 12 24 48 1 6 24 48 1 6 12 48
1 6 12 24 48 1 8 24 48 1 8 16 48
 
48 sequences using second definition:
48 2 24 24 2
3 16 16 3 4 12
12 4 6 8 8 6
2 12 2 2 8 3 2 2 12
2 6 4 2 3 8 2 4 6
2 2 6 2 2 2 4 3 2 2 3 4
2 2 2 6 2 2 3 2 2 2 2 2 3 2
2 2 2 2 3 2 6 2 2 2 3 4 2
2 3 2 4 2 3 2 2 2 2 4 3 2
2 4 2 3 3 8 2 3 4 4
3 2 8 3 4 2 2 3 2 4 2
3 2 2 4 3 2 2 2 2 4 6 2
4 4 3 4 3 4 4 2 6
4 3 2 2 4 2 3 2 4 2 2 3
12 2 2 6 4 2 6 2 4
6 2 2 2 8 3 2 8 2 3
 
OEIS A163272: 0 1 48 1280 2496 28672 29808</pre>
 
=={{header|Phix}}==
2,392

edits