DEV Community

FizzBuzz challenge in as many languages as possible

Theo on August 11, 2019

I was recently reading this post inwhich the amazing Ady Ngom was disscussing different ways to solve the common FizzBizz challenge given in interv...
Collapse
 
believer profile image
Rickard Natt och Dag

Language: ReasonML
Sketch: sketch.sh/s/XABe2ghxBqncDWTTKpNK8n/

module FizzBuzz = { let get = fun | (0, 0, _) => "FizzBuzz" | (0, _, _) => "Fizz" | (_, 0, _) => "Buzz" | (_, _, value) => string_of_int(value); }; for (index in 1 to 100) { print_endline(FizzBuzz.get((index mod 3, index mod 5, index))); }; 
Collapse
 
thomasthespacefox profile image
Thomas Leathers

Language: SSTNPL
Code:

var remain3=+ var remain5=+ uiter fbnum,fbloop,@1,@100 stop label fbloop divmod fbnum,@3 set remain3 divmod fbnum,@5 set remain5 if remain3,@0 gsub fizz if remain3,@0 return if remain5,@0 gsub buzz if remain5,@0 return dumpd fbnum newline return label fizz if remain5,@0 goto fizzbuzz prline Fizz return label fizzbuzz prline FizzBuzz return label buzz prline Buzz return 

Other/Info: Figured I'd contribute something a bit more obscure. :) SSTNPL is an architecture-specialized programming language thats used with my SBTCVM ternary computer simulator. Yes, its a bit clunkier-looking than most of the examples here, but mainly down to it using labeled GOTOs...

Collapse
 
jeddevs profile image
Theo

Very interesing, thank you for sharing. Never heard of SSTNPL and even a quick google search doesn't render many results. 👍 Thanks!

Collapse
 
thomasthespacefox profile image
Thomas Leathers

SSTNPL is somthing i put together for SBTCVM specifically. its kinda somehwhere between an assembler (as it compiles to SBTCVM's assembly language), and C, with a bit of a primitive syntax, and a fairly static structure. Its simple, but it does have a few neat features like 2-axis array-like tables, and a module system.

as far as SBTCVM itself, the blog's about page has a good overview of it:
sbtcvm.blogspot.com/p/about.html

Collapse
 
mattonem profile image
maxmattone

Language: Smalltalk
Code:

1 to: 100 do: [ :i | Transcript show: ((i isDivisibleBy: 15) ifTrue: [ 'FizzBuzz' ] ifFalse: [ (i isDivisibleBy: 3) ifTrue: [ 'Fizz' ] ifFalse: [ (i isDivisibleBy: 5) ifTrue: [ 'Buzz' ] ifFalse: [ i ] ] ]); cr ] 
Collapse
 
jeddevs profile image
Theo

👏👏👏, never used that language.

Collapse
 
mattonem profile image
maxmattone • Edited

Language: Emacs Lisp
Code:

(cl-defun fizzbuzz(&optional (n 100)) (cl-loop for i from 1 to n do (print (cond ((= (mod i 15) 0) "FizzBuzz") ((= (mod i 3) 0) "Fizz") ((= (mod i 5) 0) "Buzz") (t i) )) )) (fizzbuzz) 
Collapse
 
oscherler profile image
Olivier “Ölbaum” Scherler

Language: Erlang
Code:

Short version, generate a list (of strings and integers), don’t bother printing:

lists:map( fun( X ) -> case { X rem 3, X rem 5 } of { 0, 0 } -> "FizzBuzz"; { 0, _ } -> "Fizz"; { _, 0 } -> "Buzz"; _ -> X end end, lists:seq( 1, 100 ) ). 

Full version, print one term per line. Here we have to convert integers to strings:

lists:foreach( fun( X ) -> io:format( "~s~n", [ case { X rem 3, X rem 5 } of { 0, 0 } -> "FizzBuzz"; { 0, _ } -> "Fizz"; { _, 0 } -> "Buzz"; _ -> integer_to_list(X) end ] ) end, lists:seq( 1, 100 ) ). 
Collapse
 
jeddevs profile image
Theo

Language: Lua
Code:

function FizzBuzz() for i = 1,100 do --print(i) if i%3 == 0 and i%5 == 0 then print("FizzBuzz") elseif i%3 == 0 then print("Fizz") elseif i%5 == 0 then print("Buzz") else print(i) end end end FizzBuzz() 
Enter fullscreen mode Exit fullscreen mode

Other/info: I guess I'll start the challenge off.
This is a pretty simple function in lua of the FizzBuzz challenge.

Collapse
 
vrotaru profile image
Vasile Rotaru

Language: OCaml
Code:

let fizzbuzz n = let rec loop i max div3 div5 = if i < max then match i = div3, i = div5 with | true, true -> print_endline "FizzBuzz"; loop (i + 1) max (div3 + 3) (div5 + 5) | true, false -> print_endline "Fizz"; loop (i + 1) max (div3 + 3) div5 | false, true -> print_endline "Buzz"; loop (i + 1) max div3 (div5 + 5) | false, false -> print_endline (string_of_int i); loop (i + 1) max div3 div5 in loop 1 n 3 5 let _ = fizzbuzz 100 
Collapse
 
jeddevs profile image
Theo

Interesting! What kind of work is OCaml used for? 😊

Thread Thread
 
vrotaru profile image
Vasile Rotaru

Compilers, Proof Assistants, Static Verification Tools. Facebook uses it a lot under the hood.

Thread Thread
 
jeddevs profile image
Theo

Interesting.
Welcome to dev.to btw (saw you joined today).

Collapse
 
jeddevs profile image
Theo

I've learnt a lot since this old thread, here's how i'd attempt this question in an interview today:

 -- FizzBuzz local function isDivisible(num: number, by: integer) return num % by == 0 end local function result(number) local str = "" if isDivisible(number, 3) then str = str.."Fizz" end if isDivisible(number, 5) then str = str.."Buzz" end return (str ~= "" and str) or number end for i = 1, 100 do print(result(i)) end 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
brianverm profile image
Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻

Language: Java (8 and above)
Code:

 public void fizzBuzz() { IntStream.rangeClosed(1,100) .mapToObj(this::fizzBuzzer) .forEach(System.out::println); } private String fizzBuzzer(int i){ if (i % 15 == 0) return "FizzBuzz"; if (i % 3 == 0) return "Fizz"; if (i % 5 == 0) return "Buzz"; return String.valueOf(i); } 
Collapse
 
avalander profile image
Avalander

I did write solutions in Javascript and Racket for a similar challenge a while ago.

Since nobody has done Javascript yet, here's a crazy implementation.

const worder = (predicate, patterner) => (prev, n) => predicate(prev, n) ? patterner(prev, n) : prev const isDivisible = d => (_, n) => n % d === 0 const isEmpty = s => s.length === 0 const append = x => (prev) => prev + x const setNumber = (_, n) => n const fizzer = worder(isDivisible(3), append('fizz')) const buzzer = worder(isDivisible(5), append('buzz')) const numberer = worder(isEmpty, setNumber) const reducer = (...worders) => n => worders.reduce( (prev, w) => w(prev, n), '' ) const fizzbuzzer = reducer( fizzer, buzzer, numberer ) for (let i = 0; i <= 100; i++) { console.log(fizzbuzzer(i)) } 

Consider how easy it is to extend to print 'fazz' for multiples of 7.

const fazzer = worder(isDivisible(7), append('fazz')) const fizzbuzzfazzer = reducer( fizzer, buzzer, fazzer, numberer ) 

Here's my implementation in Racket

(define (mult-15? n) (and (mult-5? n) (mult-3? n))) (define (mult-5? n) (= (modulo n 5) 0)) (define (mult-3? n) (= (modulo n 3) 0)) (define (fizzbuzz n) (cond [(mult-15? n) "FizzBuzz"] [(mult-5? n) "Buzz"] [(mult-3? n) "Fizz"] [else n])) (define (print-list xs) (map displayln xs)) (print-list (map fizzbuzz (range 1 101))) 

The print-list function is a bit redundant, since (map fizzbuzz (range 1 101)) will already print the resulting list to the console.

Collapse
 
ap13p profile image
Afief S

Language: Javascript
Code:

const array = Array.from(Array(100).keys()).map(it => it + 1) .map(it => it % 3 === 0 && it % 5 === 0 ? 'FizzBuzz' : it) .map(it => typeof it === 'number' && it % 3 === 0 ? 'Fizz' : it) .map(it => typeof it === 'number' && it % 5 === 0 ? 'Buzz' : it) console.log(array) 
Collapse
 
brianverm profile image
Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻

Language: Haskell
Code:

fizzBuzzer :: Int -> String fizzBuzzer i | (i `mod` 15) == 0 = "FizzBuzz" | (i `mod` 5) == 0 = "Buzz" | (i `mod` 3) == 0 = "Fizz" | otherwise = show i fizzBuzz :: IO() fizzBuzz = mapM_ print [ fizzBuzzer i | i <- [1..100]] 
Collapse
 
jeddevs profile image
Theo

Language: Python
Code:

def FizzBuzz(): for i in range (1,101): #print(i/3)  if i%3 == 0 and i%5 == 0: print("FizzBuzz") elif i%3 == 0: print("Fizz") elif i%5 == 0: print("Buzz") else: print(i) FizzBuzz() 
Collapse
 
reinhart1010 profile image
Shift / Reinhart Previano K.

Language: JavaScript
Code: gist.github.com/reinhart1010/d2b81...

Other/Info: The above code execute console.log command for each number. And yes, this is compiled on JSFuck based on the original code:

var i; for (i = 1; i <= 100; i++){ var fb = 0; if (i % 3 == 0){ console.log("Fizz"); fb = 1; } if (i % 5 == 0){ console.log("Buzz"); fb = 1; } if (fb == 0) console.log(i); } 

Trying to decode the first snippet on JSUnFuck will likely to cause browsers to hang.

Interestingly, this one can be exported to C with minimal modifications:

#include <stdio.h> int main(){ int i; for (i = 1; i <= 100; i++){ int fb = 0; if (i % 3 == 0){ printf("Fizz"); fb = 1; } if (i % 5 == 0){ printf("Buzz"); fb = 1; } if (fb == 0) printf("%d", i); printf("\n"); } } 

JavaScript should be welcoming to my folks who are currently learning C thanks to similarity in syntaxes. (If you're reading this, good luck in facing your mid-term exams next week!)