RosettaCodeData/Task/Pell-numbers/FreeBASIC/pell-numbers.basic

71 lines
1.9 KiB
Plaintext

#define isOdd(a) (((a) and 1) <> 0)
Function isPrime(Byval ValorEval As Integer) As Boolean
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
If ValorEval Mod 3 = 0 Then Return ValorEval = 3
Dim d As Integer = 5
While d * d <= ValorEval
If ValorEval Mod d = 0 Then Return False Else d += 2
If ValorEval Mod d = 0 Then Return False Else d += 4
Wend
Return True
End Function
Dim As Integer n
Dim As Integer p(0 To 40), pl(0 To 40)
p(0)= 0: p(1) = 1
pl(0) = 2: pl(1) = 2
For n = 2 To 40
p(n) = 2 * p(n-1) + p(n-2)
pl(n) = 2 * pl(n-1) + pl(n-2)
Next n
Print "First 20 Pell numbers: "
For n = 0 To 19 : Print p(n); : Next n
Print !"\n\nFirst 20 Pell-Lucas: "
For n = 0 To 19 : Print pl(n); : Next n
Print !"\n\nFirst 20 rational approximations of sqrt(2) (" & Str(Sqr(2)) & "): "
For n = 1 To 20
Dim As Integer j = pl(n)/2, d = p(n)
Print Using " &/& ~= &"; j; d; j/d
Next n
Print !"\nFirst 6 Pell primes: [for the limitations of the FB standard library]"
Dim as Integer pdx = 2
Dim As Byte c = 0
Dim As Ulongint ppdx(1 to 20)
do
If isPrime(p(pdx)) Then
If isPrime(pdx) Then ppdx(c) = pdx : End If
Print p(pdx)
c += 1
End If
pdx += 1
loop until c = 6
Print !"\nIndices of first 6 Pell primes: [for the limitations of the FB standard library]"
For n = 0 To 5 : Print " "; ppdx(n); : Next n
Dim As Ulongint nsw(0 To 20)
For n = 0 To 19
nsw(n) = p(2*n) + p(2*n+1)
Next n
Print !"\n\nFirst 20 Newman-Shank-Williams numbers: "
For n = 0 To 19 : Print " "; nsw(n); : Next n
Print !"\n\nFirst 20 near isosceles right triangles:"
Dim As Integer i0 = 0, i1 = 1, i2, t = 1, i = 2, found = 0
Do While found < 20
i2 = i1*2 + i0
If isOdd(i) Then
Print Using " [&, &, &]"; t; t+1 ; i2
found += 1
End If
t += i2
i0 = i1 : i1 = i2
i += 1
Loop
Sleep