Anti-primes: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 1,279: | Line 1,279: | ||
{{out}} |
{{out}} |
||
<pre>(1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560)</pre> |
<pre>(1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560)</pre> |
||
=={{header|Prolog}}== |
|||
{{trans|Erlang}}<lang prolog> |
|||
divcount(N, Count) :- divcount(N, 1, 0, Count). |
|||
divcount(N, D, C, C) :- D*D > N, !. |
|||
divcount(N, D, C, Count) :- |
|||
succ(D, D2), |
|||
divs(N, D, A), plus(A, C, C2), |
|||
divcount(N, D2, C2, Count). |
|||
divs(N, D, 0) :- N mod D =\= 0, !. |
|||
divs(N, D, 1) :- D*D =:= N, !. |
|||
divs(_, _, 2). |
|||
antiprimes(N, L) :- antiprimes(N, 1, 0, [], L). |
|||
antiprimes(0, _, _, L, R) :- reverse(L, R), !. |
|||
antiprimes(N, M, Max, L, R) :- |
|||
divcount(M, Count), |
|||
succ(M, M2), |
|||
(Count > Max |
|||
-> succ(N0, N), antiprimes(N0, M2, Count, [M|L], R) |
|||
; antiprimes(N, M2, Max, L, R)). |
|||
main :- |
|||
antiprimes(20, X), |
|||
write("The first twenty semi-primes are "), write(X), nl, |
|||
halt. |
|||
?- main. |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
The first twenty semi-primes are [1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560] |
|||
</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |