Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » The shortest FizzBuzz YOU can make!
- TheUltimatum
- Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
very long and inefficient
_=[print(("fizzbuzz","fizz","buzz",i)[next((j for j,k in enumerate(map(lambda x:i%x<1,(15,3,5)))if k),-1)]) for i in range(1,101)]
- LastContinue
- Scratcher
500+ posts
The shortest FizzBuzz YOU can make!
Bringing this thing back to life (Necromancy)
Also I forgot to do the OCaml for 2 months so it's really bad
for i = 0 to 100 do let fizz = min 1 (i mod 3) in let buzz = min 1 (i mod 5) * 10 in match fizz + buzz with 0 -> print_endline "FizzBuzz" | 10 -> print_endline "Fizz" | 1 -> print_endline "Buzz" | 11 -> print_endline (string_of_int i) ; done ;;
Also I forgot to do the OCaml for 2 months so it's really bad
Last edited by LastContinue (Oct. 9, 2019 19:15:26)
- rdococ
- Scratcher
500+ posts
The shortest FizzBuzz YOU can make!
Lua, in 92 characters.
for i=1,100 do print(i%15==0 and"FizzBuzz"or (i%3==0 and"Fizz"or (i%5==0 and"Buzz"or i)))end
The royal blue colour scheme. Be aesthetic
Links: Search the forums.
Posts: Make cloud variables suck less | Recursive broadcasts | Sprite-local pen
Projects: YaSL tiny LISP with first-class functions & macros | Object-oriented language in Scratch | Complex, split-complex & dual numbers visualized | node logic simulator
- ylem303
- New to Scratch
1 post
The shortest FizzBuzz YOU can make!
C++
135 chars
#include <cstdio>
#define S printf(
int main(){for(int i=1;i<101;i++){!(i%3)?S"Fizz"):0;!(i%5)?S"Buzz"):0;i%3&&i%5?S"%d",i):0;S"\n");}}
135 chars
Last edited by ylem303 (Nov. 22, 2019 07:20:55)
- Wettining
- Scratcher
500+ posts
The shortest FizzBuzz YOU can make!
You can make it shorter C++#include <cstdio>
#define S printf(
int main(){for(int i=1;i<101;i++){!(i%3)?S"Fizz"):0;!(i%5)?S"Buzz"):0;i%3&&i%5?S"%d",i):0;S"\n");}}
135 chars
- TheAspiringHacker
- Scratcher
100+ posts
The shortest FizzBuzz YOU can make!
You don't need to do the hackery with the storing of the mod results as a two-digit number. You can do: Bringing this thing back to life (Necromancy)for i = 0 to 100 do let fizz = min 1 (i mod 3) in let buzz = min 1 (i mod 5) * 10 in match fizz + buzz with 0 -> print_endline "FizzBuzz" | 10 -> print_endline "Fizz" | 1 -> print_endline "Buzz" | 11 -> print_endline (string_of_int i) ; done ;;
Also I forgot to do the OCaml for 2 months so it's really bad
for i = 0 to 100 do match i mod 3, i mod 5 with | 0, 0 -> print_endline "FizzBuzz" | 0, _ -> print_endline "Fizz" | _, 0 -> print_endline "Buzz" | _, _ -> print_endline (string_of_int i) done;;
Aside from the general hackiness of the digit solution, this version is better because the pattern match is statically known to handle all cases. Although in the context of your code, 0, 10, 1, and 11 are the only possible values that can appear, those aren't the only values that an int can hold. In the new version, the combination of all patterns covers every possible value. An OCaml motto is “make illegal states unrepresentable,” and idiomatic OCaml takes advantage of the expressive type system, as opposed to compressing data into an int when the int type doesn't reflect the data's structure.
Here is a version that puts the print_endline outside the match cases:
for i = 0 to 100 do print_endline ( match i mod 3, i mod 5 with | 0, 0 -> "FizzBuzz" | 0, _ -> "Fizz" | _, 0 -> "Buzz" | _, _ -> string_of_int i ) done;;
Last edited by TheAspiringHacker (Nov. 22, 2019 22:23:15)
Long live Kyoto Animation!
- Wettining
- Scratcher
500+ posts
The shortest FizzBuzz YOU can make!
That's a really cool language feature of OCaml, do you know if any other languages have that? (The match case with the multi-input case)for i = 0 to 100 do print_endline ( match i mod 3, i mod 5 with | 0, 0 -> "FizzBuzz" | 0, _ -> "Fizz" | _, 0 -> "Buzz" | _, _ -> string_of_int i ) done;;
Last edited by Wettining (Nov. 24, 2019 15:51:59)
- LastContinue
- Scratcher
500+ posts
The shortest FizzBuzz YOU can make!
Python with Tuples maybe?That's a really cool language feature of OCaml, do you know if any other languages have that? (The match case with the multi-input case)for i = 0 to 100 do print_endline ( match i mod 3, i mod 5 with | 0, 0 -> "FizzBuzz" | 0, _ -> "Fizz" | _, 0 -> "Buzz" | _, _ -> string_of_int i ) done;;
- TheAspiringHacker
- Scratcher
100+ posts
The shortest FizzBuzz YOU can make!
The match case thing is called pattern matching, and the multi-input is just pattern matching on a tuple. Pattern matching is extremely common in typed functional languages.That's a really cool language feature of OCaml, do you know if any other languages have that? (The match case with the multi-input case)for i = 0 to 100 do print_endline ( match i mod 3, i mod 5 with | 0, 0 -> "FizzBuzz" | 0, _ -> "Fizz" | _, 0 -> "Buzz" | _, _ -> string_of_int i ) done;;
According to Wikipedia, pattern matching originated in the NPL and Hope programming languages. Now, pattern matching has become a hallmark of the ML family, which OCaml, F#, and Standard ML are part of. Pattern matching is also a feature of Haskell (which shares some features with the ML family, but isn't really an ML), Swift, Rust, and Haxe.
The dependently typed languages Coq, Agda, and Idris have a more powerful feature called dependent pattern matching, in which the types of later patterns and the result may be refined by earlier matches.
Last edited by TheAspiringHacker (Nov. 25, 2019 04:36:20)
Long live Kyoto Animation!
- ScratchCatHELLO
- Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
bump
Python 3.9.5, 65 chars, and 65 bytes??
breakdown:
demo of that last trick:
Python 3.9.5, 65 chars, and 65 bytes??
for a in range(100):print('Fizz'*(a%3//2)+'Buzz'*(a%5//4) or a+1)
breakdown:
for a in range(1, 100): #numbers from 1 to 100
print() #duh
'Fizz'*(a%3//2) #weird hack that adds one "Fizz" if a is divisible by three
+'Buzz'*(a%5//4) #ditto
or a+1 #my new favourite trick. if the rest of the string is empty (which happens when the number is not divisible by 3 or 5), the string is interpreted as False, which causes the string to be equal to a+1, because a+1 is True (I think)
demo of that last trick:
>>> a = 5 >>> 'abc'*(int(a==5))or'xyz' 'abc' >>> a += 1 >>> 'abc'*(int(a==5))or'xyz' 'xyz'
ScratchCatHELLO
I have 5600+ posts, I've been on scratch for 5 1/2 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust
Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y
- Greg8128
- Scratcher
500+ posts
The shortest FizzBuzz YOU can make!
Here's my Haskell version:
The first part is the loop while the second part declares the helper function “q”.
I included all the boilerplate for a compiled program (everything before “forM_” is arguably boilerplate) so that you can choose whether to count it towards the total length
module Main where import Control.Monad main=forM_[1..100]$ putStrLn. q;q n|mod n 15<1="fizzbuzz"|mod n 3<1="fizz"|mod n 5<1="buzz"|1>0=show n
I included all the boilerplate for a compiled program (everything before “forM_” is arguably boilerplate) so that you can choose whether to count it towards the total length
Last edited by Greg8128 (June 30, 2021 01:53:45)
- PkmnQ
- Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
You can shave off one byte by removing the space before the or. Also, Pyth. bump
Python 3.9.5, 65 chars, and 65 bytes??for a in range(100):print('Fizz'*(a%3//2)+'Buzz'*(a%5//4) or a+1)
breakdown:for a in range(1, 100): #numbers from 1 to 100
print() #duh
'Fizz'*(a%3//2) #weird hack that adds one "Fizz" if a is divisible by three
+'Buzz'*(a%5//4) #ditto
or a+1 #my new favourite trick. if the rest of the string is empty (which happens when the number is not divisible by 3 or 5), the string is interpreted as False, which causes the string to be equal to a+1, because a+1 is True (I think)
demo of that last trick:>>> a = 5 >>> 'abc'*(int(a==5))or'xyz' 'abc' >>> a += 1 >>> 'abc'*(int(a==5))or'xyz' 'xyz'
Pyth, 34 chars/bytes
V100|+*"Fizz"/%N3 2*"Buzz"/%N5 4hN
This is an account that exists.
Here, have a useful link:
The Official List of Rejected Suggestions by Za-Chary
- Cabbage_2020
- Scratcher
500+ posts
The shortest FizzBuzz YOU can make!
Just create a programming language
yay, 0 characters
Don't click here. Just don't. Trust me. And if you do, just don't start the thing and play it. Don't click here. please. PLEASE. just don't click. Don't click here, don't click here, and if you do, don't run the thing.Please. PLEEEAAAAAAAAASSSSSSSSEEEEEEEEE don;t click here.
yes i know it is 2023
test: :mrgreen: :cry: :neutral: :arrow: :idea: :wink: :roll: :twisted: :???: :shock: :eek: :grin: ops: :smile: :!: :evil: :sad: :razz: :?:
- mybearworld
- Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
what is a fizzbuzz? xD
Signatures are the only place where assets links still work.
- gosoccerboy5
- Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
It's a basic coding interview question, where you iterate through the numbers 1-100 what is a fizzbuzz? xD
if the current number is divisible by 3 it prints “fizz”, if its divisible by 5 it prints “buzz” and if both it prints “fizzbuzz” (I think). otherwise it prints the number
for (let i = 1; i <= 100; i++) { if (i % 3 === 0) { if (i % 5 === 0) { console.log("FizzBuzz"); continue; } else console.log("Fizz"); } else if (i % 5 === 0) { console.log("Buzz"); } else console.log(i); }
Last edited by gosoccerboy5 (July 2, 2021 13:49:27)
- ScratchCatHELLO
- Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
Just create a programming languageyay, 0 characters
actually, if you build an entire programming language to do this, you should count the number of characters in the program that compiles/interprets the language.
what is a fizzbuzz? xD
you take the numbers from 1 to 100. if the number is divisible by three, print “Fizz”. if the number is divisible by five, print “Buzz”. if the number is divisible by three and five, print “FizzBuzz”. if the number is neither divisible by three or five, print the number.
ScratchCatHELLO
I have 5600+ posts, I've been on scratch for 5 1/2 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust
Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y
- gosoccerboy5
- Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
Javascript, 60 bytes (if `alert` is allowed)
explanation:
My fizzbuzz uses a lot of hacks.
- type coercion to boolean
- not declaring a variable before assigning it
- not using semicolons
- not using curly brackets on the for loop
(I guess the last two aren't too hacky)
I'm keeping this thing for job interviews in the future lol
(assuming I even get job interviews )
for(i=1;i<=100;i++)alert((i%3?"":"Fizz")+(i%5?"":"Buzz")||i)
explanation:
for (i=1; i<=100; i++) // well duh we have to loop alert( // well duh we have to print the results (i % 3 ? "" : "Fizz") // if i%3 is not 0, it's truthy, so it prints "" but if it's 0 it will be falsy and print "fizz" + (i % 5 ? "" : "Buzz") // same principle, we also concatenate this to the previous one || i // if the last value was equal to "", then it's falsy, so we print the number instead ) // end of program
- type coercion to boolean
- not declaring a variable before assigning it
- not using semicolons
- not using curly brackets on the for loop
(I guess the last two aren't too hacky)
I'm keeping this thing for job interviews in the future lol
(assuming I even get job interviews )
Last edited by gosoccerboy5 (July 1, 2021 17:21:05)
- gosoccerboy5
- Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
you can remove whitespace between operators and assignments. Since it's Java, you can condense the entire code onto one line if you want to:public class FizzBuzz {public static void main(String[] args) {for (int i = 1; i < 101; i++) {System.out.println(i % 3 == 0 && i % 5 == 0 ? "FizzBuzz" : (i % 3 == 0 ? "Fizz" : (i % 5 == 0 ? "Buzz" : i))); }}}
public class FizzBuzz{public static void main(String[]args){for(int i=1;i<101;i++){System.out.println(i%3==0&&i%5==0?"FizzBuzz":(i%3==0?"Fizz":(i%5==0?"Buzz":i)));}}}
(Minified java never feels very minified because of all the whitespace between the keywords!)
Last edited by gosoccerboy5 (July 1, 2021 16:29:45)
- ScratchCatHELLO
- Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
python would be shorter if it used different syntax (for _ in _ vs for(){}), but weirdly the range function that python uses is actually shorter than the javascript equivalent because two of the arguments are optional and you don’t need to use i
ScratchCatHELLO
I have 5600+ posts, I've been on scratch for 5 1/2 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust
Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y
- ScratchCatHELLO
- Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
Javascript, 60 bytes (if `alert` is allowed)for(i=1;i<=100;i++)alert((i%3?"":"Fizz")+(i%5?"":"Buzz")||i)
explanation:I'm keeping this thing for job interviews in the future lolfor(i=1; i<=100; i++) // well duh we have to loop alert( // well duh we have to print the results (i % 3 ? "" : "Fizz") // if i%3 is not 0, it's truthy, so it prints "" but if it's 0 it will be falsy and print "fizz" + (i % 5 ? "" : "Buzz") // same principle, we also concatenate this to the previous one || i // if the last value was equal to "", then it's falsy, so we print the number instead ) // end of program
(assuming I even get job interviews )
the biggest advantage that js has here over python is the ternary operator:
#python’s ternary operator is really easy to understand at a glance, at the cost of length “a”if b==c else“b”
//JS’ is much shorter, but confusing if you don’t know the syntax a==b?”b”:”a”
…||i
ScratchCatHELLO
I have 5600+ posts, I've been on scratch for 5 1/2 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust
Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y
- Discussion Forums
- » Advanced Topics
- » The shortest FizzBuzz YOU can make!