How to resolve the algorithm FizzBuzz step by step in the Forth programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm FizzBuzz step by step in the Forth programming language

Table of Contents

Problem Statement

Write a program that prints the integers from   1   to   100   (inclusive).

But:

The   FizzBuzz   problem was presented as the lowest level of comprehension required to illustrate adequacy.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm FizzBuzz step by step in the Forth programming language

Source code in the forth programming language

: fizz ( n -- ) drop ." Fizz" ;
: buzz ( n -- ) drop ." Buzz" ;
: fb   ( n -- ) drop ." FizzBuzz" ;
: vector create does> ( n -- )
  over 15 mod cells + @ execute ;
vector .fizzbuzz
  ' fb   , ' . ,    ' . ,
  ' fizz , ' . ,    ' buzz ,
  ' fizz , ' . ,    ' . ,
  ' fizz , ' buzz , ' . ,
  ' fizz , ' . ,    ' . ,

: .fizzbuzz ( n -- )
  0 pad c!
  dup 3 mod 0= if s" Fizz" pad  place then
  dup 5 mod 0= if s" Buzz" pad +place then
  pad c@ if drop pad count type else . then ;

: zz ( n -- )
  1+ 1 do i .fizzbuzz cr loop ;
100 zz

SYNONYM NOT INVERT \ Bitwise boolean not

: Fizz?  ( n -- ? )  3 MOD 0=  DUP IF ." Fizz" THEN ;
: Buzz?  ( n -- ? )  5 MOD 0=  DUP IF ." Buzz" THEN ;
: ?print  ( n ? -- )  IF . THEN ;
: FizzBuzz  ( -- )
   101 1 DO CR  I  DUP Fizz? OVER Buzz? OR  NOT ?print  LOOP ;

FizzBuzz

: n     ( n -- n+1 )    dup .         1+ ;
: f     ( n -- n+1 )    ." Fizz "     1+ ;
: b     ( n -- n+1 )    ." Buzz "     1+ ;
: fb    ( n -- n+1 )    ." FizzBuzz " 1+ ;
: fb10  ( n -- n+10 )   n n f n b f n n f b ;
: fb15  ( n -- n+15 )   fb10 n f n n fb ;
: fb100 ( n -- n+100 )  fb15 fb15 fb15 fb15 fb15 fb15 fb10 ;
: .fizzbuzz ( -- )      1 fb100 drop ;

  

You may also check:How to resolve the algorithm Speech synthesis step by step in the C programming language
You may also check:How to resolve the algorithm Balanced ternary step by step in the Phix programming language
You may also check:How to resolve the algorithm Casting out nines step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Program name step by step in the Elena programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the LDPL programming language