Primality by trial division: Difference between revisions

PascalABC.NET
(PascalABC.NET)
(9 intermediate revisions by 3 users not shown)
Line 368:
endmethod.
ENDCLASS.</syntaxhighlight>
 
=={{header|ABC}}==
<syntaxhighlight lang="langurABC">valHOW .isPrimeTO =REPORT fprime .i == 2 orn:
REPORT n>=2 AND NO d IN {2..floor root n} HAS n mod d = 0
 
FOR n IN {1..100}:
IF prime n: WRITE n</syntaxhighlight>
{{out}}
<pre>2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</pre>
 
=={{header|ACL2}}==
Line 1,213 ⟶ 1,222:
89 is prime
97 is prime</pre>
 
==={{header|GW-BASIC}}===
{{trans|Craft Basic}}
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|QBasic}}
{{works with|QB64}}
<syntaxhighlight lang="qbasic">100 FOR I = 1 TO 99
110 IF I < 2 THEN P = 0 : GOTO 180
120 IF I = 2 THEN P = 1 : GOTO 180
130 IF I MOD 2 = 0 THEN P = 0 : GOTO 180
140 P = 1
150 FOR J = 3 TO SQR(I) STEP 2
160 IF I MOD J = 0 THEN P = 0 : GOTO 180
170 NEXT J
180 IF P <> 0 THEN PRINT I;
190 NEXT I
200 END</syntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</pre>
 
==={{header|IS-BASIC}}===
Line 1,285 ⟶ 1,314:
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|QB64}}
Returns 1 for prime, 0 for non-prime
<syntaxhighlight lang="qbasic">' Primality by trial division
Line 3,031 ⟶ 3,061:
following the Raku example, which states, "Integer $i is prime if it is greater than one and is divisible by none of 2, 3, up to the square root of $i" (plus an adjustment for the prime number 2)
 
<syntaxhighlight lang="langur">
Below, we use an implied parameter (.i) on the .isPrime function.
val isPrime = fn(i) {
i == 2 or i > 2 and
.i > 2 and not any(fn f(.x) .:i div .x, pseries (2 .. .i ^/ 2))
}
 
writeln filter .(isPrime, series (100</syntaxhighlight>))
<syntaxhighlight lang="langur">val .isPrime = f .i == 2 or
</syntaxhighlight>
.i > 2 and not any f(.x) .i div .x, pseries 2 .. .i ^/ 2
 
writeln filter .isPrime, series 100</syntaxhighlight>
 
=== Recursive ===
{{trans|Go}}
<syntaxhighlight lang="langur">val .isPrime = f(.i) {
val .nisPrime = absfn(.i) {
ifval .n <= 2: return .n == 2abs(i)
if n <= 2: return n == 2
 
val .chkdiv = ffn(.n, .i) {
if .i x* .i <= .n {
return .n ndiv .i and self(.n, .i+2)
}
return true
}
 
return .n ndiv 2 and .chkdiv(.n, 3)
}
 
writeln filter .(isPrime, series (100</syntaxhighlight>))
</syntaxhighlight>
 
{{out}}
Line 3,841 ⟶ 3,875:
;Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
function IsPrime(N: integer): boolean;
begin
if N = 1 then
Result := False
else Result := True;
for var i:=2 to N.Sqrt.Round do
if N.Divs(i) then
begin
Result := False;
exit
end;
end;
 
begin
for var i:=1 to 1000 do
if IsPrime(i) then
Print(i)
end.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997
</pre>
 
 
=={{header|Perl}}==
Line 4,910 ⟶ 4,971:
Original source: [http://seed7.sourceforge.net/algorith/math.htm#is_prime]
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program trial_division;
print({n : n in {1..100} | prime n});
 
op prime(n);
return n>=2 and not exists d in {2..floor sqrt n} | n mod d = 0;
end op;
end program;</syntaxhighlight>
{{out}}
<pre>{2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97}</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func is_prime(a) {
220

edits