Semiprime: Difference between revisions
Content deleted Content added
Line 2,023: | Line 2,023: | ||
</pre> |
</pre> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
works with swi-prolog |
|||
<syntaxhighlight lang="prolog"> |
<syntaxhighlight lang="prolog"> |
||
factors(N, FList):- |
|||
factors(N, 2, 0, FList). |
|||
factors(1, _, _Count, []). |
|||
factors(_, _, Count, []):- Count > 1. % break on 2 factors reached |
|||
factors(N, Start, Count, [Fac|FList]):- |
|||
N1 is floor(sqrt(N)), |
|||
between(Start, N1, Fac), |
|||
N mod Fac =:= 0,!, |
|||
N2 is N div Fac, |
|||
Count1 is Count + 1, |
|||
factors(N2, Fac, Count1, FList). |
|||
factors(N, _, _, [N]):- N >= 2. |
|||
semiPrimeList(Limit, List):- |
|||
findall(N, semiPrimes(2, Limit, N), List). |
|||
semiPrimes(Start, Limit, N):- |
|||
between(Start, Limit, N), |
|||
factors(N, [F1, F2]), |
|||
N =:= F1 * F2. % correct factors break |
|||
do:- semiPrimeList(100, SemiPrimes), |
|||
writeln(SemiPrimes), |
|||
findall(N, semiPrimes(1675, 1685, N), SemiPrimes2), |
|||
writeln(SemiPrimes2). |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{out}} |
|||
<pre> |
|||
?- do. |
|||
[4,6,9,10,14,15,21,22,25,26,33,34,35,38,39,46,49,51,55,57,58,62,65,69,74,77,82,85,86,87,91,93,94,95] |
|||
[1678,1679,1681,1685] |
|||
true. |
|||
</pre> |
|||
=={{header|PROMAL}}== |
=={{header|PROMAL}}== |