(fib.tal) Added tail-recursive version

This commit is contained in:
neauoire 2023-06-06 11:49:28 -07:00
parent 8d90298e57
commit 6fc314b5a0
1 changed files with 18 additions and 2 deletions

View File

@ -1,5 +1,5 @@
( Fibonacci: ( Fibonacci:
A series of numbers where the next number A series of numbers where the next number
is made of the two numbers before it ) is made of the two numbers before it )
|100 |100
@ -7,13 +7,16 @@
#0019 #0000 #0019 #0000
&l &l
DUP2 pdec #2018 DEO DUP2 pdec #2018 DEO
DUP2 fib pdec #0a18 DEO DUP2 fib pdec #2018 DEO
DUP2 #0000 #0001 ROT2 fibr pdec #0a18 DEO POP2 POP2
INC2 GTH2k ?&l INC2 GTH2k ?&l
POP2 POP2 POP2 POP2
#010f DEO #010f DEO
BRK BRK
( recursive )
@fib ( num -- numfib* ) @fib ( num -- numfib* )
#0001 GTH2k ?&ok #0001 GTH2k ?&ok
POP2 JMP2r &ok POP2 JMP2r &ok
@ -22,6 +25,19 @@ BRK
ADD2 ADD2
JMP2r JMP2r
( tail-recursive )
@fibr ( a* b* num* -- a* b* num* )
ORAk ?&no-0
POP2 OVR2 JMP2r &no-0
DUP2 #0001 NEQ2 ?&no-1
POP2 DUP2 JMP2r &no-1
#0001 SUB2 STH2
SWP2 ADD2k NIP2 STH2r
!fibr
( print routine )
@pdec ( short* -- ) @pdec ( short* -- )
#2710 LIT2r 00fb #2710 LIT2r 00fb