Erdős-Nicolas numbers: Difference between revisions

→‎{{header|Perl}}: better performance (~3x faster)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(→‎{{header|Perl}}: better performance (~3x faster))
Line 1,103:
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>use v5.36;
use enum qw(False True);
use ntheory 'divisors';
use enumntheory qw(Falsedivisors Truedivisor_sum);
use List::AllUtils <firstidx sum>;
 
sub proper_divisors ($n) {
return 1 if $n == 0;
my @d = divisors($n);
pop @d;
@d;
}
 
sub is_Erdos_Nicolas ($n) {
 
my @divisors = proper_divisors($n);
divisor_sum($n) > 2*$n or return False;
return False unless sum(@divisors) > $n;
 
my $sum;
my $keysum = firstidx { $_ == $n } map { $sum += $_ } @divisors0;
$key ? 1 +my $keycount := False0;
 
foreach my @$d = (divisors($n);) {
++$count; $sum += $d;
last if ($sum == $n);
return False unlessif sum(@divisors)$sum > $n);
}
 
return 1False if ($nsum =!= 0$n);
return $count;
}
 
my ($n, $count) = (2, 0);
until ($count == 8) {
next unless 0 == ++$n % 2;
if (my $key = is_Erdos_Nicolas $n) {
printf "%8d == sum of its first %3d divisors\n", $n, $key;
$count++;
}
}</syntaxhighlight>
2,747

edits