52 lines
840 B
Plaintext
52 lines
840 B
Plaintext
EnableExplicit
|
|
Define.i i
|
|
|
|
If OpenConsole("")
|
|
PrintN("Stern-Brocot_sequence")
|
|
Else
|
|
End 1
|
|
EndIf
|
|
|
|
Procedure.i f(n.i)
|
|
If n<2
|
|
ProcedureReturn n
|
|
ElseIf n&1
|
|
ProcedureReturn f(n/2)+f(n/2+1)
|
|
Else
|
|
ProcedureReturn f(n/2)
|
|
EndIf
|
|
EndProcedure
|
|
|
|
Procedure.i gcd(a.i,b.i)
|
|
If b : ProcedureReturn gcd(b,a%b) : EndIf
|
|
ProcedureReturn a
|
|
EndProcedure
|
|
|
|
Procedure.i ind(m.i)
|
|
Define.i i=1
|
|
While f(i)<>m : i+1 : Wend
|
|
ProcedureReturn i
|
|
EndProcedure
|
|
|
|
Print("First 15 elements: ")
|
|
For i=1 To 15
|
|
Print(Str(f(i))+Space(3))
|
|
Next
|
|
PrintN(~"\n")
|
|
|
|
For i=1 To 10
|
|
PrintN(RSet(Str(i),3)+" is at pos. #"+Str(ind(i)))
|
|
Next
|
|
PrintN("100 is at pos. #"+Str(ind(100)))
|
|
PrintN("")
|
|
|
|
i=1
|
|
While i<1000 And gcd(f(i),f(i+1))=1 : i+1 : Wend
|
|
If i=1000
|
|
PrintN("All GCDs are 1.")
|
|
Else
|
|
PrintN("GCD of "+Str(i)+" and "+Str(i+1)+" is not 1")
|
|
EndIf
|
|
|
|
Input()
|