Code: Select all
;Leonardo de Pisa (aka Fibonacci) PB algorithm (2006-09) by Albert (Psychophanta)
Define.q
Procedure.q fib(n)
Protected r=1,r1=0,negflag=1
If n<0:n=-n:negflag=-(~n&1):negflag*2+1:EndIf
Repeat
If n=0:r=r1:Break:EndIf:r1+r:n-1
If n=0:Break:EndIf:r+r1:n-1
; Delay(20) ; <- lets CPU to take a breath (if you want :-P )
ForEver
ProcedureReturn r*negflag
EndProcedure
;
;prove it:
For t.l=-92 To 92
Debug Str(t)+" -> "+StrU(fib(t),#PB_Quad)
Next
;
If OpenConsole()
Repeat
Print("Enter number to find at fringe [-92,92]: ")
n=Val(Input())
Until n<=92 And n>=-92
answer=fib(n)
PrintN(""):PrintN(StrU(answer,#PB_Quad)+" is the "+StrU(n,#PB_Quad)+"th Fibonacci number."):PrintN("")
PrintN("Press ESC to continue."):PrintN("")
Repeat:key.s=Inkey():Delay(20):Until key=Chr(#ESC) ; catch ESC
CloseConsole()
EndIf
EDIT: also with negative values support.