Proper divisors: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎version 3: added a comment to the REXX section header.)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 696:
There are 2 numbers with this trait, and they are (18480 15120)
</lang>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
Line 895 ⟶ 896:
 
</pre>
 
=={{header|AWK}}==
<lang AWK>
Line 1,180 ⟶ 1,182:
10: {1, 2, 5}
15120: 79</pre>
 
=={{header|C++}}==
<lang cpp>#include <vector>
Line 1,335 ⟶ 1,338:
18480 has 79 divisors
</pre>
 
=={{header|Common Lisp}}==
Ideally, the smallest-divisor function would only try prime numbers instead of odd numbers.
Line 1,465 ⟶ 1,469:
18480
</pre>
 
=={{header|D}}==
{{trans|Python}}
Line 1,704 ⟶ 1,709:
</pre>
 
=={{Headerheader|Erlang}}==
 
<lang erlang>-module(properdivs).
Line 3,010 ⟶ 3,015:
ReadChar
END ProperDivisors.</lang>
 
=={{header|Nim}}==
{{trans|C}}
Line 3,067 ⟶ 3,073:
10: 1 2 5
18480 with 79 divisors
</pre>
 
=={{header|Objeck}}==
<lang objeck>use Collection;
 
class Proper{
function : Main(args : String[]) ~ Nil {
for(x := 1; x <= 10; x++;) {
Print(x, ProperDivs(x));
};
x := 0;
count := 0;
for(n := 1; n <= 20000; n++;) {
if(ProperDivs(n)->Size() > count) {
x := n;
count := ProperDivs(n)->Size();
};
};
"{$x}: {$count}"->PrintLine();
}
 
function : ProperDivs(n : Int) ~ IntVector {
divs := IntVector->New();
if(n = 1) {
return divs;
};
divs->AddBack(1);
for(x := 2; x < n; x++;) {
if(n % x = 0) {
divs->AddBack(x);
};
};
divs->Sort();
return divs;
}
function : Print(x : Int, result : IntVector) ~ Nil {
"{$x}: "->Print();
result->ToArray()->ToString()->PrintLine();
}
}
</lang>
 
Output:
<pre>
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]
15120: 79
</pre>
 
Line 3,245 ⟶ 3,190:
15120
18480
</pre>
 
=={{header|Objeck}}==
<lang objeck>use Collection;
 
class Proper{
function : Main(args : String[]) ~ Nil {
for(x := 1; x <= 10; x++;) {
Print(x, ProperDivs(x));
};
x := 0;
count := 0;
for(n := 1; n <= 20000; n++;) {
if(ProperDivs(n)->Size() > count) {
x := n;
count := ProperDivs(n)->Size();
};
};
"{$x}: {$count}"->PrintLine();
}
 
function : ProperDivs(n : Int) ~ IntVector {
divs := IntVector->New();
if(n = 1) {
return divs;
};
divs->AddBack(1);
for(x := 2; x < n; x++;) {
if(n % x = 0) {
divs->AddBack(x);
};
};
divs->Sort();
return divs;
}
function : Print(x : Int, result : IntVector) ~ Nil {
"{$x}: "->Print();
result->ToArray()->ToString()->PrintLine();
}
}
</lang>
 
Output:
<pre>
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]
15120: 79
</pre>
 
Line 3,557 ⟶ 3,563:
 
Note that the first code will choose the first max, while the second chooses the last.
 
=={{header|Perl 6}}==
{{Works with|rakudo|2018.10}}
Once your threshold is over 1000, the maximum proper divisors will always include 2, 3 and 5 as divisors, so only bother to check multiples of 2, 3 and 5.
 
There really isn't any point in using concurrency for a limit of 20_000. The setup and bookkeeping drowns out any benefit. Really doesn't start to pay off until the limit is 50_000 and higher. Try swapping in the commented out race map iterator line below for comparison.
<lang perl6>sub propdiv (\x) {
my @l = 1 if x > 1;
(2 .. x.sqrt.floor).map: -> \d {
unless x % d { @l.push: d; my \y = x div d; @l.push: y if y != d }
}
@l
}
 
put "$_ [{propdiv($_)}]" for 1..10;
 
my @candidates;
loop (my int $c = 30; $c <= 20_000; $c += 30) {
#(30, *+30 …^ * > 500_000).race.map: -> $c {
my \mx = +propdiv($c);
next if mx < @candidates - 1;
@candidates[mx].push: $c
}
 
say "max = {@candidates - 1}, candidates = {@candidates.tail}";</lang>
{{out}}
<pre>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]
max = 79, candidates = 15120 18480</pre>
 
=={{header|Phix}}==
Line 4,422 ⟶ 4,391:
 
The output is the same as the short version above.
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2018.10}}
Once your threshold is over 1000, the maximum proper divisors will always include 2, 3 and 5 as divisors, so only bother to check multiples of 2, 3 and 5.
 
There really isn't any point in using concurrency for a limit of 20_000. The setup and bookkeeping drowns out any benefit. Really doesn't start to pay off until the limit is 50_000 and higher. Try swapping in the commented out race map iterator line below for comparison.
<lang perl6>sub propdiv (\x) {
my @l = 1 if x > 1;
(2 .. x.sqrt.floor).map: -> \d {
unless x % d { @l.push: d; my \y = x div d; @l.push: y if y != d }
}
@l
}
 
put "$_ [{propdiv($_)}]" for 1..10;
 
my @candidates;
loop (my int $c = 30; $c <= 20_000; $c += 30) {
#(30, *+30 …^ * > 500_000).race.map: -> $c {
my \mx = +propdiv($c);
next if mx < @candidates - 1;
@candidates[mx].push: $c
}
 
say "max = {@candidates - 1}, candidates = {@candidates.tail}";</lang>
{{out}}
<pre>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]
max = 79, candidates = 15120 18480</pre>
 
=={{header|REXX}}==
10,327

edits