Primality by trial division: Difference between revisions
Content added Content deleted
(Dialects of BASIC moved to the BASIC section.) |
|||
Line 795: | Line 795: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BASIC256}}=== |
|||
{{works with|QBasic|1.1}} |
|||
{{trans|FreeBASIC}} |
|||
{{works with|QuickBasic|4.5}} |
|||
<syntaxhighlight lang="freebasic">for i = 1 to 99 |
|||
Returns 1 for prime, 0 for non-prime |
|||
if isPrime(i) then print string(i); " "; |
|||
<syntaxhighlight lang="qbasic">FUNCTION prime% (n!) |
|||
next i |
|||
STATIC i AS INTEGER |
|||
end |
|||
IF n = 2 THEN |
|||
prime = 1 |
|||
function isPrime(v) |
|||
ELSEIF n <= 1 OR n MOD 2 = 0 THEN |
|||
if v < 2 then return False |
|||
prime = 0 |
|||
if v mod 2 = 0 then return v = 2 |
|||
ELSE |
|||
if v mod 3 = 0 then return v = 3 |
|||
prime = 1 |
|||
d = 5 |
|||
FOR i = 3 TO INT(SQR(n)) STEP 2 |
|||
while d * d <= v |
|||
if v mod d = 0 then return False else d += 2 |
|||
end while |
|||
return True |
|||
end function</syntaxhighlight> |
|||
NEXT i |
|||
END IF |
|||
==={{header|BBC BASIC}}=== |
|||
<syntaxhighlight lang="bbcbasic"> FOR i% = -1 TO 100 |
|||
IF FNisprime(i%) PRINT ; i% " is prime" |
|||
NEXT |
|||
END |
|||
DEF FNisprime(n%) |
|||
IF n% <= 1 THEN = FALSE |
|||
IF n% <= 3 THEN = TRUE |
|||
IF (n% AND 1) = 0 THEN = FALSE |
|||
LOCAL t% |
|||
FOR t% = 3 TO SQR(n%) STEP 2 |
|||
IF n% MOD t% = 0 THEN = FALSE |
|||
NEXT |
|||
= TRUE</syntaxhighlight> |
|||
{{out}} |
|||
<pre>2 is prime |
|||
3 is prime |
|||
5 is prime |
|||
7 is prime |
|||
11 is prime |
|||
13 is prime |
|||
17 is prime |
|||
19 is prime |
|||
23 is prime |
|||
29 is prime |
|||
31 is prime |
|||
37 is prime |
|||
41 is prime |
|||
43 is prime |
|||
47 is prime |
|||
53 is prime |
|||
59 is prime |
|||
61 is prime |
|||
67 is prime |
|||
71 is prime |
|||
73 is prime |
|||
79 is prime |
|||
83 is prime |
|||
89 is prime |
|||
97 is prime</pre> |
|||
==={{header|FBSL}}=== |
|||
The second function (included by not used) I would have thought would be faster because it lacks the SQR() function. As it happens, the first is over twice as fast. |
|||
<syntaxhighlight lang="qbasic">#APPTYPE CONSOLE |
|||
FUNCTION ISPRIME(n AS INTEGER) AS INTEGER |
|||
IF n = 2 THEN |
|||
RETURN TRUE |
|||
ELSEIF n <= 1 ORELSE n MOD 2 = 0 THEN |
|||
RETURN FALSE |
|||
ELSE |
|||
FOR DIM i = 3 TO SQR(n) STEP 2 |
|||
IF n MOD i = 0 THEN |
|||
RETURN FALSE |
|||
END IF |
|||
NEXT |
|||
RETURN TRUE |
|||
END IF |
|||
END FUNCTION |
|||
FUNCTION ISPRIME2(N AS INTEGER) AS INTEGER |
|||
IF N <= 1 THEN RETURN FALSE |
|||
DIM I AS INTEGER = 2 |
|||
WHILE I * I <= N |
|||
IF N MOD I = 0 THEN |
|||
RETURN FALSE |
|||
END IF |
|||
I = I + 1 |
|||
WEND |
|||
RETURN TRUE |
|||
END FUNCTION |
END FUNCTION |
||
' Test and display primes 1 .. 50 |
' Test and display primes 1 .. 50 |
||
DIM n AS INTEGER |
|||
DECLARE FUNCTION prime% (n!) |
|||
FOR n = 1 TO 50 |
FOR n = 1 TO 50 |
||
IF |
IF ISPRIME(n) THEN |
||
PRINT n, " "; |
|||
NEXT n</syntaxhighlight> |
|||
END IF |
|||
NEXT |
|||
PAUSE</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
|||
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 |
|||
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 |
|||
Press any key to continue... |
|||
</pre> |
|||
==={{header|FreeBASIC}}=== |
|||
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
|||
Function isPrime(n As Integer) As Boolean |
|||
If n < 2 Then Return False |
|||
If n = 2 Then Return True |
|||
If n Mod 2 = 0 Then Return False |
|||
Dim limit As Integer = Sqr(n) |
|||
For i As Integer = 3 To limit Step 2 |
|||
If n Mod i = 0 Then Return False |
|||
Next |
|||
Return True |
|||
End Function |
|||
' To test this works, print all primes under 100 |
|||
For i As Integer = 1 To 99 |
|||
If isPrime(i) Then |
|||
Print Str(i); " "; |
|||
End If |
|||
Next |
|||
Print : Print |
|||
Print "Press any key to quit" |
|||
Sleep</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|FutureBasic}}=== |
|||
<syntaxhighlight lang="futurebasic">window 1, @"Primality By Trial Division", (0,0,480,270) |
|||
local fn isPrime( n as long ) as Boolean |
|||
long i |
|||
Boolean result |
|||
if n < 2 then result = NO : exit fn |
|||
if n = 2 then result = YES : exit fn |
|||
if n mod 2 == 0 then result = NO : exit fn |
|||
result = YES |
|||
for i = 3 to int( n^.5 ) step 2 |
|||
if n mod i == 0 then result = NO : break |
|||
next i |
|||
end fn = result |
|||
long i, j = 0 |
|||
print "Prime numbers between 0 and 1000:" |
|||
for i = 0 to 1000 |
|||
if ( fn isPrime(i) != _false ) |
|||
printf @"%3d\t",i : j++ |
|||
if j mod 10 == 0 then print |
|||
end if |
|||
next |
|||
HandleEvents</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Prime numbers between 0 and 1000: |
|||
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|Gambas}}=== |
|||
'''[https://gambas-playground.proko.eu/?gist=85fbc7936b17b3009af282752aa29df7 Click this link to run this code]''' |
|||
<syntaxhighlight lang="gambas">'Reworked from the BBC Basic example |
|||
Public Sub Main() |
|||
Dim iNum As Integer |
|||
For iNum = 1 To 100 |
|||
If FNisprime(iNum) Then Print iNum & " is prime" |
|||
Next |
|||
End |
|||
'___________________________________________________ |
|||
Public Sub FNisprime(iNum As Integer) As Boolean |
|||
Dim iLoop As Integer |
|||
Dim bReturn As Boolean = True |
|||
If iNum <= 1 Then bReturn = False |
|||
If iNum <= 3 Then bReturn = True |
|||
If (iNum And 1) = 0 Then bReturn = False |
|||
For iLoop = 3 To Sqr(iNum) Step 2 |
|||
If iNum Mod iLoop = 0 Then bReturn = False |
|||
Next |
|||
Return bReturn |
|||
End</syntaxhighlight> |
|||
{{out}} |
|||
<pre>1 is prime |
|||
3 is prime |
|||
5 is prime |
|||
7 is prime |
|||
11 is prime |
|||
...... |
|||
73 is prime |
|||
79 is prime |
|||
83 is prime |
|||
89 is prime |
|||
97 is prime</pre> |
|||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
Line 840: | Line 1,040: | ||
240 END IF |
240 END IF |
||
250 END DEF</syntaxhighlight> |
250 END DEF</syntaxhighlight> |
||
==={{header|Liberty BASIC}}=== |
|||
{{works with|Just BASIC}} |
|||
<syntaxhighlight lang="lb">print "Rosetta Code - Primality by trial division" |
|||
print |
|||
[start] |
|||
input "Enter an integer: "; x |
|||
if x=0 then print "Program complete.": end |
|||
if isPrime(x) then print x; " is prime" else print x; " is not prime" |
|||
goto [start] |
|||
function isPrime(p) |
|||
p=int(abs(p)) |
|||
if p=2 then isPrime=1: exit function 'prime |
|||
if p=0 or p=1 or (p mod 2)=0 then exit function 'not prime |
|||
for i=3 to sqr(p) step 2 |
|||
if (p mod i)=0 then exit function 'not prime |
|||
next i |
|||
isPrime=1 |
|||
end function</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Rosetta Code - Primality by trial division |
|||
Enter an integer: 1 |
|||
1 is not prime |
|||
Enter an integer: 2 |
|||
2 is prime |
|||
Enter an integer: |
|||
Program complete.</pre> |
|||
==={{header|PureBasic}}=== |
|||
<syntaxhighlight lang="purebasic">Procedure.i IsPrime(n) |
|||
Protected k |
|||
If n = 2 |
|||
ProcedureReturn #True |
|||
EndIf |
|||
If n <= 1 Or n % 2 = 0 |
|||
ProcedureReturn #False |
|||
EndIf |
|||
For k = 3 To Int(Sqr(n)) Step 2 |
|||
If n % k = 0 |
|||
ProcedureReturn #False |
|||
EndIf |
|||
Next |
|||
ProcedureReturn #True |
|||
EndProcedure</syntaxhighlight> |
|||
==={{header|QuickBASIC}}=== |
|||
{{works with|QBasic|1.1}} |
|||
{{works with|QuickBasic|4.5}} |
|||
Returns 1 for prime, 0 for non-prime |
|||
<syntaxhighlight lang="qbasic">' Primality by trial division |
|||
' Test and display primes 1 .. 50 |
|||
DECLARE FUNCTION prime% (n!) |
|||
FOR n = 1 TO 50 |
|||
IF prime(n) = 1 THEN PRINT n; |
|||
NEXT n |
|||
FUNCTION prime% (n!) |
|||
STATIC i AS INTEGER |
|||
IF n = 2 THEN |
|||
prime = 1 |
|||
ELSEIF n <= 1 OR n MOD 2 = 0 THEN |
|||
prime = 0 |
|||
ELSE |
|||
prime = 1 |
|||
FOR i = 3 TO INT(SQR(n)) STEP 2 |
|||
IF n MOD i = 0 THEN |
|||
prime = 0 |
|||
EXIT FUNCTION |
|||
END IF |
|||
NEXT i |
|||
END IF |
|||
END FUNCTION</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 |
|||
</pre> |
|||
==={{header|Run BASIC}}=== |
|||
{{works with|Just BASIC}} |
|||
<syntaxhighlight lang="runbasic">' Test and display primes 1 .. 50 |
|||
for i = 1 TO 50 |
|||
if prime(i) <> 0 then print i;" "; |
|||
next i |
|||
FUNCTION prime(n) |
|||
if n < 2 then prime = 0 : goto [exit] |
|||
if n = 2 then prime = 1 : goto [exit] |
|||
if n mod 2 = 0 then prime = 0 : goto [exit] |
|||
prime = 1 |
|||
for i = 3 to int(n^.5) step 2 |
|||
if n mod i = 0 then prime = 0 : goto [exit] |
|||
next i |
|||
[exit] |
|||
END FUNCTION</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 3 5 7 11 13 17 19 23 25 29 31 37 41 43 47 |
|||
</pre> |
|||
==={{header|S-BASIC}}=== |
|||
<syntaxhighlight lang="s-basic"> |
|||
$lines |
|||
$constant FALSE = 0 |
|||
$constant TRUE = 0FFFFH |
|||
rem - return p mod q |
|||
function mod(p, q = integer) = integer |
|||
end = p - q * (p / q) |
|||
rem - return true (-1) if n is prime, otherwise false (0) |
|||
function isprime(n = integer) = integer |
|||
var i, limit, result = integer |
|||
if n = 2 then |
|||
result = TRUE |
|||
else if (n < 2) or (mod(n,2) = 0) then |
|||
result = FALSE |
|||
else |
|||
begin |
|||
limit = int(sqr(n)) |
|||
i = 3 |
|||
while (i <= limit) and (mod(n, i) <> 0) do |
|||
i = i + 2 |
|||
result = not (i <= limit) |
|||
end |
|||
end = result |
|||
rem - test by looking for primes in range 1 to 50 |
|||
var i = integer |
|||
for i = 1 to 50 |
|||
if isprime(i) then print using "#####";i; |
|||
next i |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 |
|||
</pre> |
|||
==={{header|TI-83 BASIC}}=== |
|||
Prompt A |
|||
If A=2:Then |
|||
Disp "PRIME" |
|||
Stop |
|||
End |
|||
If (fPart(A/2)=0 and A>0) or A<2:Then |
|||
Disp "NOT PRIME" |
|||
Stop |
|||
End |
|||
1→P |
|||
For(B,3,int(√(A))) |
|||
If FPart(A/B)=0:Then |
|||
0→P |
|||
√(A)→B |
|||
End |
|||
B+1→B |
|||
End |
|||
If P=1:Then |
|||
Disp "PRIME" |
|||
Else |
|||
Disp "NOT PRIME" |
|||
End |
|||
==={{header|Tiny BASIC}}=== |
|||
{{works with|TinyBasic}} |
|||
<syntaxhighlight lang="basic">10 REM Primality by trial division |
|||
20 PRINT "Enter a number " |
|||
30 INPUT P |
|||
40 GOSUB 1000 |
|||
50 IF Z = 1 THEN PRINT "It is prime." |
|||
60 IF Z = 0 THEN PRINT "It is not prime." |
|||
70 END |
|||
990 REM Primality of the number P by trial division |
|||
1000 IF P < 2 THEN RETURN |
|||
1010 LET Z = 1 |
|||
1020 IF P < 4 THEN RETURN |
|||
1030 LET I = 2 |
|||
1040 IF (P / I) * I = P THEN LET Z = 0 |
|||
1050 IF Z = 0 THEN RETURN |
|||
1060 LET I = I + 1 |
|||
1070 IF I * I <= P THEN GOTO 1040 |
|||
1080 RETURN</syntaxhighlight> |
|||
==={{header|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
{{trans| |
{{trans|QuickBASIC}} |
||
<syntaxhighlight lang="qbasic">FUNCTION isPrime (n) |
<syntaxhighlight lang="qbasic">FUNCTION isPrime (n) |
||
IF n = 2 THEN |
IF n = 2 THEN |
||
Line 863: | Line 1,257: | ||
NEXT n |
NEXT n |
||
END</syntaxhighlight> |
END</syntaxhighlight> |
||
==={{header|uBasic/4tH}}=== |
|||
<syntaxhighlight lang="text">10 LET n=0: LET p=0 |
|||
20 INPUT "Enter number: ";n |
|||
30 LET p=0 : IF n>1 THEN GOSUB 1000 |
|||
40 IF p=0 THEN PRINT n;" is not prime!" |
|||
50 IF p#0 THEN PRINT n;" is prime!" |
|||
60 GOTO 10 |
|||
1000 REM *************** |
|||
1001 REM * PRIME CHECK * |
|||
1002 REM *************** |
|||
1010 LET p=0 |
|||
1020 IF (n%2)=0 THEN RETURN |
|||
1030 LET p=1 : PUSH n,0 : GOSUB 9030 |
|||
1040 FOR i=3 TO POP() STEP 2 |
|||
1050 IF (n%i)=0 THEN LET p=0: PUSH n,0 : GOSUB 9030 : LET i=POP() |
|||
1060 NEXT i |
|||
1070 RETURN |
|||
9030 Push ((10^(Pop()*2))*Pop()) : @(255)=Tos() |
|||
9040 Push (@(255) + (Tos()/@(255)))/2 |
|||
If Abs(@(255)-Tos())<2 Then @(255)=Pop() : If Pop() Then Push @(255) : Return |
|||
@(255) = Pop() : Goto 9040 |
|||
REM ** This is an integer SQR subroutine. Output is scaled by 10^(TOS()).</syntaxhighlight> |
|||
==={{header|VBA}}=== |
|||
<syntaxhighlight lang="vb">Option Explicit |
|||
Sub FirstTwentyPrimes() |
|||
Dim count As Integer, i As Long, t(19) As String |
|||
Do |
|||
i = i + 1 |
|||
If IsPrime(i) Then |
|||
t(count) = i |
|||
count = count + 1 |
|||
End If |
|||
Loop While count <= UBound(t) |
|||
Debug.Print Join(t, ", ") |
|||
End Sub |
|||
Function IsPrime(Nb As Long) As Boolean |
|||
If Nb = 2 Then |
|||
IsPrime = True |
|||
ElseIf Nb < 2 Or Nb Mod 2 = 0 Then |
|||
Exit Function |
|||
Else |
|||
Dim i As Long |
|||
For i = 3 To Sqr(Nb) Step 2 |
|||
If Nb Mod i = 0 Then Exit Function |
|||
Next |
|||
IsPrime = True |
|||
End If |
|||
End Function</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71 |
|||
</pre> |
|||
==={{header|VBScript}}=== |
|||
{{trans|QuickBASIC}} |
|||
<syntaxhighlight lang="vb">Function IsPrime(n) |
|||
If n = 2 Then |
|||
IsPrime = True |
|||
ElseIf n <= 1 Or n Mod 2 = 0 Then |
|||
IsPrime = False |
|||
Else |
|||
IsPrime = True |
|||
For i = 3 To Int(Sqr(n)) Step 2 |
|||
If n Mod i = 0 Then |
|||
IsPrime = False |
|||
Exit For |
|||
End If |
|||
Next |
|||
End If |
|||
End Function |
|||
For n = 1 To 50 |
|||
If IsPrime(n) Then |
|||
WScript.StdOut.Write n & " " |
|||
End If |
|||
Next</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 |
|||
</pre> |
|||
==={{header|Yabasic}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="yabasic">for i = 1 to 99 |
|||
if isPrime(i) print str$(i), " "; |
|||
next i |
|||
print |
|||
end |
|||
sub isPrime(v) |
|||
if v < 2 return False |
|||
if mod(v, 2) = 0 return v = 2 |
|||
if mod(v, 3) = 0 return v = 3 |
|||
d = 5 |
|||
while d * d <= v |
|||
if mod(v, d) = 0 then return False else d = d + 2 : fi |
|||
wend |
|||
return True |
|||
end sub</syntaxhighlight> |
|||
==={{header|ZX Spectrum Basic}}=== |
==={{header|ZX Spectrum Basic}}=== |
||
Line 888: | Line 1,385: | ||
119 is not prime! |
119 is not prime! |
||
137 is prime!</pre> |
137 is prime!</pre> |
||
=={{header|BASIC256}}== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="freebasic">for i = 1 to 99 |
|||
if isPrime(i) then print string(i); " "; |
|||
next i |
|||
end |
|||
function isPrime(v) |
|||
if v < 2 then return False |
|||
if v mod 2 = 0 then return v = 2 |
|||
if v mod 3 = 0 then return v = 3 |
|||
d = 5 |
|||
while d * d <= v |
|||
if v mod d = 0 then return False else d += 2 |
|||
end while |
|||
return True |
|||
end function</syntaxhighlight> |
|||
=={{header|BBC BASIC}}== |
|||
<syntaxhighlight lang="bbcbasic"> FOR i% = -1 TO 100 |
|||
IF FNisprime(i%) PRINT ; i% " is prime" |
|||
NEXT |
|||
END |
|||
DEF FNisprime(n%) |
|||
IF n% <= 1 THEN = FALSE |
|||
IF n% <= 3 THEN = TRUE |
|||
IF (n% AND 1) = 0 THEN = FALSE |
|||
LOCAL t% |
|||
FOR t% = 3 TO SQR(n%) STEP 2 |
|||
IF n% MOD t% = 0 THEN = FALSE |
|||
NEXT |
|||
= TRUE</syntaxhighlight> |
|||
{{out}} |
|||
<pre>2 is prime |
|||
3 is prime |
|||
5 is prime |
|||
7 is prime |
|||
11 is prime |
|||
13 is prime |
|||
17 is prime |
|||
19 is prime |
|||
23 is prime |
|||
29 is prime |
|||
31 is prime |
|||
37 is prime |
|||
41 is prime |
|||
43 is prime |
|||
47 is prime |
|||
53 is prime |
|||
59 is prime |
|||
61 is prime |
|||
67 is prime |
|||
71 is prime |
|||
73 is prime |
|||
79 is prime |
|||
83 is prime |
|||
89 is prime |
|||
97 is prime</pre> |
|||
=={{header|bc}}== |
=={{header|bc}}== |
||
Line 1,910: | Line 2,347: | ||
]?]?%]p:</syntaxhighlight> |
]?]?%]p:</syntaxhighlight> |
||
=={{header|FBSL}}== |
|||
The second function (included by not used) I would have thought would be faster because it lacks the SQR() function. As it happens, the first is over twice as fast. |
|||
<syntaxhighlight lang="qbasic">#APPTYPE CONSOLE |
|||
FUNCTION ISPRIME(n AS INTEGER) AS INTEGER |
|||
IF n = 2 THEN |
|||
RETURN TRUE |
|||
ELSEIF n <= 1 ORELSE n MOD 2 = 0 THEN |
|||
RETURN FALSE |
|||
ELSE |
|||
FOR DIM i = 3 TO SQR(n) STEP 2 |
|||
IF n MOD i = 0 THEN |
|||
RETURN FALSE |
|||
END IF |
|||
NEXT |
|||
RETURN TRUE |
|||
END IF |
|||
END FUNCTION |
|||
FUNCTION ISPRIME2(N AS INTEGER) AS INTEGER |
|||
IF N <= 1 THEN RETURN FALSE |
|||
DIM I AS INTEGER = 2 |
|||
WHILE I * I <= N |
|||
IF N MOD I = 0 THEN |
|||
RETURN FALSE |
|||
END IF |
|||
I = I + 1 |
|||
WEND |
|||
RETURN TRUE |
|||
END FUNCTION |
|||
' Test and display primes 1 .. 50 |
|||
DIM n AS INTEGER |
|||
FOR n = 1 TO 50 |
|||
IF ISPRIME(n) THEN |
|||
PRINT n, " "; |
|||
END IF |
|||
NEXT |
|||
PAUSE</syntaxhighlight> |
|||
{{out}} |
|||
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 |
|||
Press any key to continue... |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<syntaxhighlight lang="forth">: prime? ( n -- f ) |
<syntaxhighlight lang="forth">: prime? ( n -- f ) |
||
dup 2 < if drop false |
dup 2 < if drop false |
||
Line 1,990: | Line 2,382: | ||
END IF |
END IF |
||
END FUNCTION</syntaxhighlight> |
END FUNCTION</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
|||
Function isPrime(n As Integer) As Boolean |
|||
If n < 2 Then Return False |
|||
If n = 2 Then Return True |
|||
If n Mod 2 = 0 Then Return False |
|||
Dim limit As Integer = Sqr(n) |
|||
For i As Integer = 3 To limit Step 2 |
|||
If n Mod i = 0 Then Return False |
|||
Next |
|||
Return True |
|||
End Function |
|||
' To test this works, print all primes under 100 |
|||
For i As Integer = 1 To 99 |
|||
If isPrime(i) Then |
|||
Print Str(i); " "; |
|||
End If |
|||
Next |
|||
Print : Print |
|||
Print "Press any key to quit" |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
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 |
|||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
Line 2,033: | Line 2,397: | ||
return true |
return true |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
Line 2,049: | Line 2,412: | ||
10000000033 |
10000000033 |
||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic">window 1, @"Primality By Trial Division", (0,0,480,270) |
|||
local fn isPrime( n as long ) as Boolean |
|||
long i |
|||
Boolean result |
|||
if n < 2 then result = NO : exit fn |
|||
if n = 2 then result = YES : exit fn |
|||
if n mod 2 == 0 then result = NO : exit fn |
|||
result = YES |
|||
for i = 3 to int( n^.5 ) step 2 |
|||
if n mod i == 0 then result = NO : break |
|||
next i |
|||
end fn = result |
|||
long i, j = 0 |
|||
print "Prime numbers between 0 and 1000:" |
|||
for i = 0 to 1000 |
|||
if ( fn isPrime(i) != _false ) |
|||
printf @"%3d\t",i : j++ |
|||
if j mod 10 == 0 then print |
|||
end if |
|||
next |
|||
HandleEvents</syntaxhighlight> |
|||
Output: |
|||
Prime numbers between 0 and 1000: |
|||
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 |
|||
=={{header|Gambas}}== |
|||
'''[https://gambas-playground.proko.eu/?gist=85fbc7936b17b3009af282752aa29df7 Click this link to run this code]''' |
|||
<syntaxhighlight lang="gambas">'Reworked from the BBC Basic example |
|||
Public Sub Main() |
|||
Dim iNum As Integer |
|||
For iNum = 1 To 100 |
|||
If FNisprime(iNum) Then Print iNum & " is prime" |
|||
Next |
|||
End |
|||
'___________________________________________________ |
|||
Public Sub FNisprime(iNum As Integer) As Boolean |
|||
Dim iLoop As Integer |
|||
Dim bReturn As Boolean = True |
|||
If iNum <= 1 Then bReturn = False |
|||
If iNum <= 3 Then bReturn = True |
|||
If (iNum And 1) = 0 Then bReturn = False |
|||
For iLoop = 3 To Sqr(iNum) Step 2 |
|||
If iNum Mod iLoop = 0 Then bReturn = False |
|||
Next |
|||
Return bReturn |
|||
End</syntaxhighlight> |
|||
Output: |
|||
<pre>1 is prime |
|||
3 is prime |
|||
5 is prime |
|||
7 is prime |
|||
11 is prime |
|||
...... |
|||
73 is prime |
|||
79 is prime |
|||
83 is prime |
|||
89 is prime |
|||
97 is prime</pre> |
|||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
Line 2,462: | Line 2,738: | ||
<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> |
<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|Liberty BASIC}}== |
|||
<syntaxhighlight lang="lb">print "Rosetta Code - Primality by trial division" |
|||
print |
|||
[start] |
|||
input "Enter an integer: "; x |
|||
if x=0 then print "Program complete.": end |
|||
if isPrime(x) then print x; " is prime" else print x; " is not prime" |
|||
goto [start] |
|||
function isPrime(p) |
|||
p=int(abs(p)) |
|||
if p=2 or then isPrime=1: exit function 'prime |
|||
if p=0 or p=1 or (p mod 2)=0 then exit function 'not prime |
|||
for i=3 to sqr(p) step 2 |
|||
if (p mod i)=0 then exit function 'not prime |
|||
next i |
|||
isPrime=1 |
|||
end function</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Rosetta Code - Primality by trial division |
|||
Enter an integer: 1 |
|||
1 is not prime |
|||
Enter an integer: 2 |
|||
2 is prime |
|||
Enter an integer: |
|||
Program complete.</pre> |
|||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
Line 3,096: | Line 3,345: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{trans| |
{{trans|QuickBASIC}} |
||
<syntaxhighlight lang="pascal">program primes; |
<syntaxhighlight lang="pascal">program primes; |
||
Line 3,451: | Line 3,700: | ||
?-</pre> |
?-</pre> |
||
=={{header|PureBasic}}== |
|||
<syntaxhighlight lang="purebasic">Procedure.i IsPrime(n) |
|||
Protected k |
|||
If n = 2 |
|||
ProcedureReturn #True |
|||
EndIf |
|||
If n <= 1 Or n % 2 = 0 |
|||
ProcedureReturn #False |
|||
EndIf |
|||
For k = 3 To Int(Sqr(n)) Step 2 |
|||
If n % k = 0 |
|||
ProcedureReturn #False |
|||
EndIf |
|||
Next |
|||
ProcedureReturn #True |
|||
EndProcedure</syntaxhighlight> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 3,960: | Line 4,188: | ||
primep3?: 0.4 i/s - 1.97x slower |
primep3?: 0.4 i/s - 1.97x slower |
||
</pre> |
</pre> |
||
=={{header|Run BASIC}}== |
|||
<syntaxhighlight lang="runbasic">' Test and display primes 1 .. 50 |
|||
for i = 1 TO 50 |
|||
if prime(i) <> 0 then print i;" "; |
|||
next i |
|||
FUNCTION prime(n) |
|||
if n < 2 then prime = 0 : goto [exit] |
|||
if n = 2 then prime = 1 : goto [exit] |
|||
if n mod 2 = 0 then prime = 0 : goto [exit] |
|||
prime = 1 |
|||
for i = 3 to int(n^.5) step 2 |
|||
if n mod i = 0 then prime = 0 : goto [exit] |
|||
next i |
|||
[exit] |
|||
END FUNCTION</syntaxhighlight> |
|||
2 3 5 7 11 13 17 19 23 25 29 31 37 41 43 47 49 |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 4,000: | Line 4,210: | ||
<pre>2 3 5 7 11 13 17 19 23 29 </pre> |
<pre>2 3 5 7 11 13 17 19 23 29 </pre> |
||
=={{header|S-BASIC}}== |
|||
<syntaxhighlight lang="s-basic"> |
|||
$lines |
|||
$constant FALSE = 0 |
|||
$constant TRUE = 0FFFFH |
|||
rem - return p mod q |
|||
function mod(p, q = integer) = integer |
|||
end = p - q * (p / q) |
|||
rem - return true (-1) if n is prime, otherwise false (0) |
|||
function isprime(n = integer) = integer |
|||
var i, limit, result = integer |
|||
if n = 2 then |
|||
result = TRUE |
|||
else if (n < 2) or (mod(n,2) = 0) then |
|||
result = FALSE |
|||
else |
|||
begin |
|||
limit = int(sqr(n)) |
|||
i = 3 |
|||
while (i <= limit) and (mod(n, i) <> 0) do |
|||
i = i + 2 |
|||
result = not (i <= limit) |
|||
end |
|||
end = result |
|||
rem - test by looking for primes in range 1 to 50 |
|||
var i = integer |
|||
for i = 1 to 50 |
|||
if isprime(i) then print using "#####";i; |
|||
next i |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 |
|||
</pre> |
|||
=={{header|S-lang}}== |
=={{header|S-lang}}== |
||
Line 4,389: | Line 4,559: | ||
return true |
return true |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|TI-83 BASIC}}== |
|||
Prompt A |
|||
If A=2:Then |
|||
Disp "PRIME" |
|||
Stop |
|||
End |
|||
If (fPart(A/2)=0 and A>0) or A<2:Then |
|||
Disp "NOT PRIME" |
|||
Stop |
|||
End |
|||
1→P |
|||
For(B,3,int(√(A))) |
|||
If FPart(A/B)=0:Then |
|||
0→P |
|||
√(A)→B |
|||
End |
|||
B+1→B |
|||
End |
|||
If P=1:Then |
|||
Disp "PRIME" |
|||
Else |
|||
Disp "NOT PRIME" |
|||
End |
|||
=={{header|Tiny BASIC}}== |
|||
<syntaxhighlight lang="tinybasic"> PRINT "ENTER A NUMBER " |
|||
INPUT P |
|||
GOSUB 100 |
|||
IF Z = 1 THEN PRINT "It is prime." |
|||
IF Z = 0 THEN PRINT "It isn't prime." |
|||
END |
|||
100 REM PRIMALITY OF THE NUMBER P BY TRIAL DIVISION |
|||
IF P < 2 THEN RETURN |
|||
LET Z = 1 |
|||
IF P < 4 THEN RETURN |
|||
LET I = 2 |
|||
110 IF (P/I)*I = P THEN LET Z = 0 |
|||
IF Z = 0 THEN RETURN |
|||
LET I = I + 1 |
|||
IF I*I <= P THEN GOTO 110 |
|||
RETURN</syntaxhighlight> |
|||
=={{header|uBasic/4tH}}== |
|||
<syntaxhighlight lang="text">10 LET n=0: LET p=0 |
|||
20 INPUT "Enter number: ";n |
|||
30 LET p=0 : IF n>1 THEN GOSUB 1000 |
|||
40 IF p=0 THEN PRINT n;" is not prime!" |
|||
50 IF p#0 THEN PRINT n;" is prime!" |
|||
60 GOTO 10 |
|||
1000 REM *************** |
|||
1001 REM * PRIME CHECK * |
|||
1002 REM *************** |
|||
1010 LET p=0 |
|||
1020 IF (n%2)=0 THEN RETURN |
|||
1030 LET p=1 : PUSH n,0 : GOSUB 9030 |
|||
1040 FOR i=3 TO POP() STEP 2 |
|||
1050 IF (n%i)=0 THEN LET p=0: PUSH n,0 : GOSUB 9030 : LET i=POP() |
|||
1060 NEXT i |
|||
1070 RETURN |
|||
9030 Push ((10^(Pop()*2))*Pop()) : @(255)=Tos() |
|||
9040 Push (@(255) + (Tos()/@(255)))/2 |
|||
If Abs(@(255)-Tos())<2 Then @(255)=Pop() : If Pop() Then Push @(255) : Return |
|||
@(255) = Pop() : Goto 9040 |
|||
REM ** This is an integer SQR subroutine. Output is scaled by 10^(TOS()).</syntaxhighlight> |
|||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 4,523: | Line 4,624: | ||
|4 prime? |
|4 prime? |
||
=false</syntaxhighlight> |
=false</syntaxhighlight> |
||
=={{header|VBA}}== |
|||
<syntaxhighlight lang="vb">Option Explicit |
|||
Sub FirstTwentyPrimes() |
|||
Dim count As Integer, i As Long, t(19) As String |
|||
Do |
|||
i = i + 1 |
|||
If IsPrime(i) Then |
|||
t(count) = i |
|||
count = count + 1 |
|||
End If |
|||
Loop While count <= UBound(t) |
|||
Debug.Print Join(t, ", ") |
|||
End Sub |
|||
Function IsPrime(Nb As Long) As Boolean |
|||
If Nb = 2 Then |
|||
IsPrime = True |
|||
ElseIf Nb < 2 Or Nb Mod 2 = 0 Then |
|||
Exit Function |
|||
Else |
|||
Dim i As Long |
|||
For i = 3 To Sqr(Nb) Step 2 |
|||
If Nb Mod i = 0 Then Exit Function |
|||
Next |
|||
IsPrime = True |
|||
End If |
|||
End Function</syntaxhighlight> |
|||
{{out}} |
|||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71 |
|||
=={{header|VBScript}}== |
|||
{{trans|BASIC}} |
|||
<syntaxhighlight lang="vb">Function IsPrime(n) |
|||
If n = 2 Then |
|||
IsPrime = True |
|||
ElseIf n <= 1 Or n Mod 2 = 0 Then |
|||
IsPrime = False |
|||
Else |
|||
IsPrime = True |
|||
For i = 3 To Int(Sqr(n)) Step 2 |
|||
If n Mod i = 0 Then |
|||
IsPrime = False |
|||
Exit For |
|||
End If |
|||
Next |
|||
End If |
|||
End Function |
|||
For n = 1 To 50 |
|||
If IsPrime(n) Then |
|||
WScript.StdOut.Write n & " " |
|||
End If |
|||
Next</syntaxhighlight> |
|||
{{out}} |
|||
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 4,636: | Line 4,680: | ||
0 |
0 |
||
not prime</pre> |
not prime</pre> |
||
=={{header|Yabasic}}== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="yabasic">for i = 1 to 99 |
|||
if isPrime(i) print str$(i), " "; |
|||
next i |
|||
print |
|||
end |
|||
sub isPrime(v) |
|||
if v < 2 return False |
|||
if mod(v, 2) = 0 return v = 2 |
|||
if mod(v, 3) = 0 return v = 3 |
|||
d = 5 |
|||
while d * d <= v |
|||
if mod(v, d) = 0 then return False else d = d + 2 : fi |
|||
wend |
|||
return True |
|||
end sub</syntaxhighlight> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |