Anti-primes: Difference between revisions
Content deleted Content added
{{header|Pascal}} added pascal |
Thundergnat (talk | contribs) →{{header|Perl 6 }}: Add a Perl 6 example |
||
Line 63:
end.</lang>;Output:<pre>1(1),2(2),4(3),6(4),12(6),24(8),36(9),48(10),60(12),120(16),180(18),240(20),
360(24),720(30),840(32),1260(36),1680(40),2520(48),5040(60),7560(64)</pre>
=={{header|Perl 6}}==
{{works with|Rakudo|2018.11}}
At its heart, this task is almost exactly the same as [[Proper_Divisors]], it is just asking for slightly different results. Much of this code is lifted straight from there.
Implemented as an auto-extending lazy list. Displaying the count of anti-primes less than 5e5 also because... why not.
<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
}
my atomicint $last = 0;
my @antiprimes = lazy 1, |(|(2..989), 990, *+30 …^ *).hyper.grep: -> $c {
my \mx = +propdiv($c);
next if mx <= $last;
$last = mx;
$c;
}
my $upto = 5e5;
put "First 20 antiprimes:\n{ @antiprimes[^20] }";
put "\nCount of antiprimes <= $upto: {+@antiprimes[^(@antiprimes.first: * > $upto, :k)]}";</lang>
{{out}}
<pre>First 20 antiprimes:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Count of antiprimes <= 500000: 36</pre>
=={{header|Python}}==
Uses the fast prime function from [[Factors of an integer#Python]]
|