I found some code by DK_PETER dating back to 2013 (here: https://www.purebasic.fr/english/viewto ... ci#p423313)
giving various calculation formulas, including one for obtaining the Fibonacci sequence. However, the formula is limited.
Although the calculations are performed with whole numbers, using PB(x86), the errors start at the 46th occurrence (0 to 45) and then give negative and inconsistent results.
As for the calculation with PB(x64), the inconsistencies start at the 92nd occurrence.
Unless I missed something, I suspect this is due to the size of the numbers (up to 19 digits with x64, and up to only 10 digits with x86).
Do you have a solution to overcome these limitations so that we can calculate much higher Fibonacci numbers?
Here's some code that illustrates the issue: (Windows 11 pro; PB 6.21b2 x86/x64)
Code: Select all
Procedure.f NombreOR()
Nor.f = (1+Sqr(5))/2
ProcedureReturn ValF(StrF(Nor,10))
EndProcedure
Procedure Calcul_Fibonacci(y.l)
TheNext.d = 0
TheLast.d = 1
For x = 0 To y
If TheNext<>0
Fnor.f = Thelast/TheNext ; nombre d'or sur chaque nombre de la suite de Fibonacci
EndIf
If Thelast<>0
Dnor.f = TheNext/Thelast ; nombre d'or moins un sur chaque nombre de la suite de Fibonacci
EndIf
AddGadgetItem(4, -1, Str(x)+Chr(10)+Str(TheLast)+Chr(10)+StrF(Fnor,15)+Chr(10)+StrF(Dnor,15))
sum = TheNext + Thelast ;: Debug "TheNext = " + Str(TheNext) + " Thelast = " + Str(Thelast) + " Somme des 2 = " + Str(sum)
TheNext = Thelast ;: Debug "TheNext = " + Str(TheNext) + " = Thelast = " + Str(Thelast)
TheLast = sum ;: Debug "Thelast = " + Str(Thelast) + " = sum = " + Str(sum)
; Debug "============================================================"
Next x
EndProcedure
If OpenWindow(0, 0, 0, 800, 600, "Fibonacci sequence", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget (1, 5, 5, 60, 25, "Démarrer") : GadgetToolTip(1,"Lancer le calcul/Start the calculation")
ButtonGadget (2, 65, 5, 60, 25, "Fermer") : GadgetToolTip(2,"Fermer la fenêtre/Close window")
TextGadget (3, 550, 10, 250, 20, "") : SetGadgetText(3,"Nombre d'or/Golden ratio = "+NombreOR())
ListIconGadget(4, 5, 35, 790, 560, "N°", 50, #PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect)
AddGadgetColumn(4, 1, "Nombre (n1+n2)", 200)
AddGadgetColumn(4, 2, "deduction of the golden ratio (n2/n1)", 200)
AddGadgetColumn(4, 3, "golden ratio -1 (n1/n2)", 200)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
Calcul_Fibonacci(91) ; uniquement en mode 64 bit, au-delà de 91 le calcul est négatif
;Calcul_Fibonacci(45) ; en mode 32 bit, au-delà de 45 le calcul est négatif
;exemple pour résultat négatifs (32 ou 64 bit)
;Calcul_Fibonacci(100)
Case 2
CloseWindow(0)
End
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
EndIf


