Arithmetic derivative: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl}}: avoid special-case for zero)
Line 238: Line 238:
use bigint;
use bigint;
no warnings 'uninitialized';
no warnings 'uninitialized';
use List::Util <sum max>;
use List::Util 'max';
use ntheory 'factor';
use ntheory 'factor';


Line 244: Line 244:


sub D ($n) {
sub D ($n) {
my %f;
my(%f, $s);
return 0 if $n == 0;
$f{$_}++ for factor max 1, my $nabs = abs $n;
$f{$_}++ for factor (my $nabs = abs $n);
map { $s += $nabs * $f{$_} / $_ } keys %f;
my $s = sum map { $nabs * $f{$_} / $_ } keys %f;
$n > 0 ? $s : -$s;
$n > 0 ? $s : -$s;
}
}