RosettaCodeData/Task/Stern-Brocot-sequence/FreeBASIC/stern-brocot-sequence.basic

83 lines
1.3 KiB
Plaintext

' version 02-03-2019
' compile with: fbc -s console
#Define max 2000
Dim Shared As UInteger stern(max +2)
Sub stern_brocot
stern(1) = 1
stern(2) = 1
Dim As UInteger i = 2 , n = 2, ub = UBound(stern)
Do While i < ub
i += 1
stern(i) = stern(n) + stern(n -1)
i += 1
stern(i) = stern(n)
n += 1
Loop
End Sub
Function gcd(x As UInteger, y As UInteger) As UInteger
Dim As UInteger t
While y
t = y
y = x Mod y
x = t
Wend
Return x
End Function
' ------=< MAIN >=------
Dim As UInteger i
stern_brocot
Print "The first 15 are: " ;
For i = 1 To 15
Print stern(i); " ";
Next
Print : Print
Print " Index First nr."
Dim As UInteger d = 1
For i = 1 To max
If stern(i) = d Then
Print Using " ######"; i; stern(i)
d += 1
If d = 11 Then d = 100
If d = 101 Then Exit For
i = 0
End If
Next
Print : Print
d = 0
For i = 1 To 1000
If gcd(stern(i), stern(i +1)) <> 1 Then
d = gcd(stern(i), stern(i +1))
Exit For
End If
Next
If d = 0 Then
Print "GCD of two consecutive members of the series up to the 1000th member is 1"
Else
Print "The GCD for index "; i; " and "; i +1; " = "; d
End If
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End