Skip to content

Instantly share code, notes, and snippets.

@nfunato
Last active June 24, 2022 23:09
Show Gist options
  • Select an option

  • Save nfunato/5f513aa4cd729b9c473c to your computer and use it in GitHub Desktop.

Select an option

Save nfunato/5f513aa4cd729b9c473c to your computer and use it in GitHub Desktop.
FizzBuzz in gforth (for code golf)
\ FizzBuzz in Forth by @nfunato on 2015-04-30
\ http://golf.shinh.org/p.rb?FizzBuzz#Forth
\ Execution rule:
\ 1. invoke shell
\ 2. exec gforth this-file -e bye
\ : x
\ 101 1 do
\ 1
\ i 3 mod 0= if ." Fizz" 1- then
\ i 5 mod if if i . then else ." Buzz" then
\ cr
\ loop ;
\ 99 bytes in total
: x 101 1 do 1 i 3 mod 0= if ." Fizz" 1- then i 5 mod if if i . then else ." Buzz" then cr loop ; x
\ Revised version on 2015-05-01 which is 98 bytes in total, though still have fallen behind the top one (96 bytes)
\ the code gains the one byte in compensation for leaving some garbages on the parameter stack
: y 101 1 do 1 i 3 mod 0= if ." Fizz" 0 then i 5 mod if if i . then else ." Buzz" then cr loop ; y
s" FizzBuzz " 2constant fb
s" Fizz " 2constant f
s" Buzz " 2constant b
create names f , b , fb ,
\ : fb2
\ cr
\ 100 1 do
\ loop ;
\ 31 words (not including -- : fb0 ; ) ※ ただし xxx " をワードと数えない
: fb1
cr
100 1 do
i 15 mod 0= if ." FizzBuzz " else
i 3 mod 0= if ." Fizz " else
i 5 mod 0= if ." Buzz " else
i .
then then then
loop ;
\ 34 words (not including -- : fb0 ; ) ※ ただし xxx" をワードと数えない
: fb0
cr
100 1 do
i 3 mod 0= i 5 mod 0= or if
i 3 mod 0= if
." Fizz"
then
i 5 mod 0= if
." Buzz"
then
space
else
i .
then
loop ;
\ https://gist.github.com/nfunato/5f513aa4cd729b9c473c
\ 99 bytes in total
: fb-1
cr
100 1 do
0
i 3 mod 0= if ." Fizz" 1+ then
i 5 mod if
if space else i . then
else
drop ." Buzz "
then
loop ;
: %3 3 mod 0= ;
: %5 5 mod 0= ;
: %15 15 mod 0= ;
: fb-2
cr
100 1 do
i %15 if ." FizzBuzz " else
i %3 if ." Fizz " else
i %5 if ." Buzz " else
i .
then then then
loop ;
\ i %15 if
\ ." FizzBuzz "
\ else
\ i %3 if
\ ." Fizz "
\ else
\ i %5 if
\ ." Buzz "
\ else
\ i .
\ then
\ then
\ then
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment