Factor-perfect numbers: Difference between revisions

m
→‎{{header|Raku}}: use 'state' variable instead of global, syntax twiddles
m (→‎{{header|Raku}}: use 'state' variable instead of global, syntax twiddles)
Line 480:
<syntaxhighlight lang="raku" line># 20221029 Raku programming solution
 
sub propdiv (\x) {
my ($n,@fpns,%cache) = 4, 0,1;
 
sub propdiv (\x) { # https://rosettacode.org/wiki/Proper_divisors#Raku
my @l = 1 if x > 1;
for (2 .. x.sqrt.floor) -> \d {
Line 492 ⟶ 490:
sub moreMultiples (@toSeq, @fromSeq) {
my @oneMores = gather for @fromSeq -> \j {
take @toSeq.clone.push(j) if j > @toSeq[*-1] &&and j %% @toSeq[*-1]
}
return []() unless @oneMores.Bool;
for (0..^+@oneMores) {
@oneMores.append: moreMultiples @oneMores[$_], @fromSeq
}
return @oneMores
}
 
sub erdosFactorCount (\n) {
state %cache;
my ($sum,@divs) = 0, |(propdiv n)[1..*];
for @divs -> \d {
unless %cache{my \t = n div d}:exists { %cache{t} = erdosFactorCount(t) }
$sum += %cache{t}
}
return ++$sum + 1
}
 
my @listing = moreMultiples [1], propdiv(48);
#`[[[[[ sub custom (\l1,\l2) {
for l1 Z l2 -> [\v1,\v2] { return True if v1 < v2; return False if v1 > v2 }
return +l1 < +l2 ?? True !! False
}
#given @listing { $_ .= sort: &custom; $_.map: *.push: 48; $_.push: [1,48] }
#given @listing { $_ .= sort: {$^b cmp $^a};$_.map: *.push: 48;$_.push: [1,48] }
]]]]]
given @listing { $_.map: *.push: 48; $_.push: [1,48] }
say @listing.elems," sequences using first definition:";
Line 525 ⟶ 517:
my @seq = |@listing[j];
@seq.append: 48 if @seq[*-1] != 48;
take (1..^+@seq).map: { @seq[$_] div @seq[$_-1] }
}
say "\n{@listing2.elems} sequences using second definition:";
Line 531 ⟶ 523:
 
say "\nOEIS A163272:";
my ($n,@fpns,%cache) = 4, 0,1;
while (+@fpns < 7) { @fpns.push($n) if erdosFactorCount($n) == $n; $n += 4 }
say ~@fpns;</syntaxhighlight>
{{out}}
2,392

edits