Practical numbers: Difference between revisions

Content added Content deleted
(No Paddy, you can't endlessly delete approaches which you wish to discourage. We are optimizing different things. Live and let live. Let others show their approach.)
(→‎{{header|Raku}}: Do some smarter filtering. More verbose but MUCH faster for most numbers)
Line 620: Line 620:


sub is-practical ($n) {
sub is-practical ($n) {
return True if $n == 1;
my @proper-sums = $n.&proper-divisors.combinations».sum.unique.sort;
return False if $n % 2;
my @proper = $n.&proper-divisors :sort;
return True if all( @proper.rotor(2 => -1).map: { .[0] / .[1] >= .5 } );
my @proper-sums = @proper.combinations».sum.unique.sort;
+@proper-sums < $n-1 ?? False !! @proper-sums[^$n] eqv (^$n).list ?? True !! False
+@proper-sums < $n-1 ?? False !! @proper-sums[^$n] eqv (^$n).list ?? True !! False
}
}
Line 627: Line 631:
given [ (1..333).hyper(:32batch).grep: { is-practical($_) } ];
given [ (1..333).hyper(:32batch).grep: { is-practical($_) } ];


printf "%5s is practical? %s\n", $_, .&is-practical for 666, 6666, 66666;</lang>
printf "%5s is practical? %s\n", $_, .&is-practical for 666, 6666, 66666, 672, 720;</lang>
{{out}}
{{out}}
<pre>77 matching numbers:
<pre>77 matching numbers:
Line 641: Line 645:
666 is practical? True
666 is practical? True
6666 is practical? True
6666 is practical? True
66666 is practical? False</pre>
66666 is practical? False
672 is practical? True
720 is practical? True</pre>


=={{header|Wren}}==
=={{header|Wren}}==