RosettaCodeData/Task/Pierpont-primes/FreeBASIC/pierpont-primes.basic
2023-07-01 13:44:08 -04:00

58 lines
1.6 KiB
Text

#define NPP 50
Function isPrime(Byval n As Ulongint) As Boolean
If n < 2 Then Return false
If n = 2 Then Return true
If n Mod 2 = 0 Then Return false
For i As Uinteger = 3 To Int(Sqr(n))+1 Step 2
If n Mod i = 0 Then Return false
Next i
Return true
End Function
Function is_23(Byval n As Uinteger) As Boolean
While n Mod 2 = 0
n /= 2
Wend
While n Mod 3 = 0
n /= 3
Wend
Return Iif(n=1, true, false)
End Function
Function isPierpont(n As Uinteger) As Uinteger
If Not isPrime(n) Then Return 0 'not prime
Dim As Uinteger p1 = is_23(n+1), p2 = is_23(n-1)
If p1 And p2 Then Return 3 'pierpont prime of both kinds
If p1 Then Return 1 'pierpont prime of the 1st kind
If p2 Then Return 2 'pierpont prime of the 2nd kind
Return 0 'prime, but not pierpont
End Function
Dim As Uinteger pier(1 To 2, 1 To NPP), np(1 To 2) = {0, 0}
Dim As Uinteger x = 1, j
While np(1) <= NPP Or np(2) <= NPP
x += 1
j = isPierpont(x)
If j > 0 Then
If j Mod 2 = 1 Then
np(1) += 1
If np(1) <= NPP Then pier(1, np(1)) = x
End If
If j > 1 Then
np(2) += 1
If np(2) <= NPP Then pier(2, np(2)) = x
End If
End If
Wend
Print "First 50 Pierpoint primes of the first kind:"
For j = 1 To NPP
Print Using " ########"; pier(2, j);
If j Mod 10 = 0 Then Print
Next j
Print !"\nFirst 50 Pierpoint primes of the secod kind:"
For j = 1 To NPP
Print Using " ########"; pier(1, j);
If j Mod 10 = 0 Then Print
Next j