Proper divisors: Difference between revisions

Add Perl
(Add Perl)
Line 113:
10: [1, 2, 5]
15120: 79</pre>
 
=={{header|Perl}}==
===Using a module for divisors===
{{libheader|ntheory}}
<lang perl>use ntheory qw/divisors/;
# Uses the task definition of pd(1) = 1, not the OEIS A032741 definition.
sub proper_divisors {
my $n = shift;
return if $n == 0;
return 1 if $n == 1;
my @d = divisors($n);
pop @d;
@d;
}
say "$_: ", join " ", proper_divisors($_) for 1..10;
# 1. For the max, we can do a traditional loop.
my($max,$ind) = (0,0);
for (1..20000) {
my $nd = scalar proper_divisors($_);
($max,$ind) = ($nd,$_) if $nd > $max;
}
say "$max $ind";
# 2. Or we can use List::Util's max with decoration (this exploits its implementation)
{
use List::Util qw/max/;
no warnings 'numeric';
say max(map { scalar(proper_divisors($_)) . " $_" } 1..20000);
}</lang>
{{out}}
<pre>1: 1
2: 1
3: 1
4: 1 2
5: 1
6: 1 2 3
7: 1
8: 1 2 4
9: 1 3
10: 1 2 5
79 15120
79 18480</pre>
 
Note that the first code will choose the first max, while the second chooses the last.
 
 
=={{header|Python}}==
Line 129 ⟶ 173:
79
>>> </lang>
 
 
===Python: From prime factors===
Anonymous user