uxn/projects/examples/exercises/fizzbuzz.tal

21 lines
647 B
Tal
Raw Normal View History

2024-03-10 13:54:28 -04:00
( Print the first 30 numbers using the following rules:
| If n is divisible by 3, print "fizz"
| If n is divisible by 5, print "buzz"
| If n is divisible by both, print "fizzbuzz"
| Else, print the number )
2021-10-12 21:38:45 -04:00
2024-03-10 13:54:28 -04:00
@fizzbuzz ( -> )
#1e00
&>loop ( length i -- )
DUP fizz OVR buzz ORA ?{ DUP <dec> }
2022-11-10 12:08:21 -05:00
#0a18 DEO
2024-03-10 13:54:28 -04:00
INC GTHk ?&>loop
POP2 BRK
2021-10-12 21:38:45 -04:00
2024-03-10 13:54:28 -04:00
@fizz ( n -- ) #03 DIVk MUL SUB ?{ #01 ;Dict/fizz !<str> } #00 JMP2r
@buzz ( n -- ) #05 DIVk MUL SUB ?{ #01 ;Dict/buzz !<str> } #00 JMP2r
@<dec> ( n -- ) DUP #0a DIV /d #0a DIVk MUL SUB &d #30 ADD #18 DEO JMP2r
@<str> ( s* -- ) LDAk #18 DEO INC2 LDAk ?<str> POP2 JMP2r
@Dict &fizz "Fizz $1 &buzz "Buzz $1
2021-10-12 21:38:45 -04:00