Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » The shortest FizzBuzz YOU can make!
- bybb
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
This is a challenge for yourself to make the shortest FizzBuzz you can!
Here's my one in Python 3:
I'm interested in the different language people will choose.
Here's my one in Python 3:
[(str(i) if (i%3 and i%5) else "")+("Fizz" if not i%3 else "")+("Buzz" if not i%5 else "") for i in range(1, 100)]
- Wettining
-
Scratcher
500+ posts
The shortest FizzBuzz YOU can make!
–snip–Wouldn't you have to put
print
Or is outputting not really necessary, just the calculation?
- bybb
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
Depends on how you run it.–snip–Wouldn't you have to putin the beginning of your code for it to output the test though?
Or is outputting not really necessary, just the calculation?
If you run it as a py files, yes you'd need print.
If you run it in IDLE (or in python) it prints the output by default.
- novice27b
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
BRB, gonna write my solution in PiedPiper… https://scratch.mit.edu/projects/117951730/
It's been a while, I'm not sure if I have any documentation for it even…
In the meantime, here's my python3 solution:
It's been a while, I'm not sure if I have any documentation for it even…
In the meantime, here's my python3 solution:
["Fizz"*(1-x%3)+"Buzz"*(1-x%5)or str(x)for x in range(1,100)]
Last edited by novice27b (June 9, 2018 15:48:24)
- Wettining
-
Scratcher
500+ posts
The shortest FizzBuzz YOU can make!
A version made in GP
(To run this you have to run the project in GP, download the project here)
setMy 'Output' ''
for i 100 {
if ((i % 3) == 0) {
setMy 'Output' (join 'Fizz' (join ',' (newline)) (toString (my 'Output')))
} ((i % 5) == 0) {
setMy 'Output' (join 'Buzz' (join ',' (newline)) (toString (my 'Output')))
} else {
setMy 'Output' (join (toString i) (join ',' (newline)) (toString (my 'Output')))
}
}
(To run this you have to run the project in GP, download the project here)
- Sheep_maker
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
JavaScript:
new Array(100).fill('').map((v,i)=>(++i%3?v:'Fizz')+(i%5?v:'Buzz')||i)
- Wettining
-
Scratcher
500+ posts
The shortest FizzBuzz YOU can make!
C:
C++:
#include <stdio.h> int main(boop) { int i; for(i = 1; i <= 100; ++i) { if (i % 3 == 0) printf("Fizz"); if (i % 5 == 0) printf("Buzz"); if ((i % 3 != 0) && (i % 5 != 0)) printf("%d", i); printf("\n"); } return 0; }
#include <iostream> int main() { for (int i = 1; i <= 100; i++) { if (i%5==0) std::cout << "Buzz" << std::endl; else if (i%3==0) std::cout << "Fizz" << std::endl; else std::cout << i << std::endl; } return 0; }
Last edited by Wettining (June 9, 2018 20:24:17)
- bybb
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
C:printf is slow cout is better.#include <stdio.h> int main(boop) { int i; for(i = 1; i <= 100; ++i) { if (i % 3 == 0) printf("Fizz"); if (i % 5 == 0) printf("Buzz"); if ((i % 3 != 0) && (i % 5 != 0)) printf("%d", i); printf("\n"); } return 0; }
#include <iostream>
using namespace std;
int main() {
for (int i=1;i<100;i++) {
if (i%3==0)
cout << "Fizz";
if (i%5==0)
cout << "Buzz";
if (i%3&&i%5)
cout << i;
cout << "\n";
}
return 0;
}
- bybb
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
C:That fizzbuzz isn't correct since it will never output “FizzBuzz” on one line and also std::endl causes a delay just use \n in the stringC++:#include <stdio.h> int main(boop) { int i; for(i = 1; i <= 100; ++i) { if (i % 3 == 0) printf("Fizz"); if (i % 5 == 0) printf("Buzz"); if ((i % 3 != 0) && (i % 5 != 0)) printf("%d", i); printf("\n"); } return 0; }#include <iostream> int main() { for (int i = 1; i <= 100; i++) { if (i%5==0) std::cout << "Buzz" << std::endl; else if (i%3==0) std::cout << "Fizz" << std::endl; else std::cout << i << std::endl; } return 0; }

- MegaApuTurkUltra
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
also std::endl causes a delay“a delay”
- bybb
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
https://stackoverflow.com/a/35581029also std::endl causes a delay“a delay”
stream << std::endl is actually stream << ‘\n’ << std::flush. Explicit flushing has performance drawbacks, and this is why you shouldn't use it in performance-critical situations. You rarely think about such negligible performance issues while debugging, so it actualy is a good practice to explicitly flush debug output.
- MegaApuTurkUltra
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
Sorry, I assumed you didn't actually know what endl was actually doing when you said it was just “a delay” so I was commenting on that. Looks like you do know what you're talking about thoughsnipalso std::endl causes a delay“a delay”

- bybb
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
I first discovered it had some sort of delay when i was making a C++ program I could call from within a batch file to print coloured text within Windows, any text printed without the newline argument passed to my program would complete and print the next bit of text instantly, but when the newline flag was passed it had a delay, I did some research and learnt about how it was the equivalent of a newline and a buffer flush, rather than just a newline.Sorry, I assumed you didn't actually know what endl was actually doing when you said it was just “a delay” so I was commenting on that. Looks like you do know what you're talking about thoughsnipalso std::endl causes a delay“a delay”
This is one of the best examples of my why I love programming so much, you can be doing something just for fun and end up learning half a dozen new things you wouldn't have otherwise.
- xn--cr8h
-
New Scratcher
48 posts
The shortest FizzBuzz YOU can make!
you can remove a fair bit of whitespace from that…[(str(i) if (i%3 and i%5) else "")+("Fizz" if not i%3 else "")+("Buzz" if not i%5 else "") for i in range(1, 100)]
anyway here's mine in Dolphin Smalltalk 7 (179 bytes)
Transcript show.1to:100do:[:i|Transcript show:(i\\3=0ifTrue:['Fizz'])displayString;show:(i\\5=0ifTrue:['Buzz'])displayString;show:(((i\\3>0)and:[i\\5>0])ifTrue:[i])displayString;cr]
ungolfed:
Transcript show. 1 to: 100 do: [:i | Transcript show: (i \\ 3 = 0 ifTrue: ['Fizz']) displayString; show: (i \\ 5 = 0 ifTrue: ['Buzz']) displayString; show: (((i \\ 3 > 0) and: [i \\ 5 > 0]) ifTrue: [i]) displayString; cr ]
if we remove the need to show the transcript and print newlines we can reduce it to 161 bytes!
1to:100do:[:i|Transcript show:(i\\3=0ifTrue:['Fizz'])displayString;show:(i\\5=0ifTrue:['Buzz'])displayString;show:(((i\\3>0)and:[i\\5>0])ifTrue:[i])displayString]
edit: this is shorter
Transcript show.1to:100do:[:i|Transcript show:(i\\15=0ifTrue:['FizzBuzz']ifFalse:[i\\5=0ifTrue:['Buzz']ifFalse:[i\\3=0ifTrue:['Fizz']ifFalse:[i displayString]]]);cr]
Last edited by xn--cr8h (June 10, 2018 03:30:03)
- Wettining
-
Scratcher
500+ posts
The shortest FizzBuzz YOU can make!
It's C not C++ lolC:printf is slow cout is better.#include <stdio.h> int main(boop) { int i; for(i = 1; i <= 100; ++i) { if (i % 3 == 0) printf("Fizz"); if (i % 5 == 0) printf("Buzz"); if ((i % 3 != 0) && (i % 5 != 0)) printf("%d", i); printf("\n"); } return 0; }#include <iostream>
using namespace std;
int main() {
for (int i=1;i<100;i++) {
if (i%3==0)
cout << "Fizz";
if (i%5==0)
cout << "Buzz";
if (i%3&&i%5)
cout << i;
cout << "\n";
}
return 0;
}
- bybb
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
I wanted to do it in Batch but I didn't realise Batch actually had a modulo operator… Oh well.
@echo off
goto start
:modulo
set /a return=%1
set /a divide=%2
:loop_2
if %return% gtr 0 (
set /a return=%return%-%divide%
goto loop_2
)
exit /b
:start
set /a i=1
set /a return=0
:loop_1
set "fb="
set setted=0
call :modulo %i% 3
if %return% equ 0 (
set fb=%fb%Fizz
set setted=1
)
call :modulo %i% 5
if %return% equ 0 (
set fb=%fb%Buzz
set setted=1
)
if %setted% neq 1 set fb=%i%
echo %fb%
set /a i=%i%+1
if %i% equ 101 goto eol_1
goto loop_1
:eol_1
exit /b
- MegaApuTurkUltra
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
I wanted to do it in Batch but I didn't realise Batch actually had a modulo operator… Oh well.Just Windows Things™
#!/bin/bash for i in {1..100}; do case "$((i % 3))$((i % 5))" in 00) echo FizzBuzz;; 0*) echo Fizz;; *0) echo Buzz;; *) echo $i esac done
- TheAspiringHacker
-
Scratcher
100+ posts
The shortest FizzBuzz YOU can make!
You can make this shorter by replacing `using namespace std` with `using std::cout`.C:printf is slow cout is better.#include <stdio.h> int main(boop) { int i; for(i = 1; i <= 100; ++i) { if (i % 3 == 0) printf("Fizz"); if (i % 5 == 0) printf("Buzz"); if ((i % 3 != 0) && (i % 5 != 0)) printf("%d", i); printf("\n"); } return 0; }#include <iostream>
using namespace std;
int main() {
for (int i=1;i<100;i++) {
if (i%3==0)
cout << "Fizz";
if (i%5==0)
cout << "Buzz";
if (i%3&&i%5)
cout << i;
cout << "\n";
}
return 0;
}
- xn--cr8h
-
New Scratcher
48 posts
The shortest FizzBuzz YOU can make!
you can also make this shorter by not using 8 HECKING SPACES to indentYou can make this shorter by replacing `using namespace std` with `using std::cout`.C:printf is slow cout is better.#include <stdio.h> int main(boop) { int i; for(i = 1; i <= 100; ++i) { if (i % 3 == 0) printf("Fizz"); if (i % 5 == 0) printf("Buzz"); if ((i % 3 != 0) && (i % 5 != 0)) printf("%d", i); printf("\n"); } return 0; }#include <iostream>
using namespace std;
int main() {
for (int i=1;i<100;i++) {
if (i%3==0)
cout << "Fizz";
if (i%5==0)
cout << "Buzz";
if (i%3&&i%5)
cout << i;
cout << "\n";
}
return 0;
}
- Sheep_maker
-
Scratcher
1000+ posts
The shortest FizzBuzz YOU can make!
Those are tab characters, not sets of 8 spaces.snipyou can also make this shorter by not using 8 HECKING SPACES to indent
- Discussion Forums
- » Advanced Topics
-
» The shortest FizzBuzz YOU can make!