Write the most efficient FizzBuzz program. Any language you want

Write the most efficient FizzBuzz program. Any language you want.

Other urls found in this thread:

en.wikipedia.org/wiki/Loop_unrolling
github.com/iamevn/fizzy
github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition
twitter.com/AnonBabble

print '\n'.join("Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i) for i in range(1,101))

python

/thread

print 1
print 2
print fizz
..
print 98
print fizz
print buzz

no conditionals
no branching
#include
int main()
{
for (int i=0;++i

g fizzbuzz
Ctrl-C Ctrl-V

This is a great argument for string immutability

This, it's not elegant but that's the most efficient way to do this as it doesn't contain anything unnecessary

Strings are immutable in Python. It literally creates a new string every time.

well strg(i) is not a string containing "i" for i in range, it's the string value of "i".

cat fizzbuzz.txt

it doesn't even "create" a string, just returns the string value of "i"

could you explain your invention young man?

>str(i)
The str() method returns the "informal" or nicely printable representation of a given object.
it's not a string

define fizzbuzz(n):
i=0
while (i

Sir how did you learn of this teach us mere negroes

Just make a LUT of FizzBuzz values.

github.com/AbstractBeliefs/BadBuzz

>mutates a string every cycle
>doesn't fully buffer the output, only line buffered
6/10

I'm impressed

I'll use jquery and
fizzbuzz(0,100);

#!/usr/bin/tail -15
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz

Perl 6 (from rosetta, not my coding)

say "Fizz"x$_%%3~"Buzz"x$_%%5||$_ for 1..100

for n in range(50):
print(str(n), end='\r')
if n % 3 == 0:
print("fizz", end='')
if n % 5 == 0:
print("buzz", end='')
print()

the two only legit answers ITT

#include

int main(void)
{
int cnt3 = 0;
int cnt5 = 0;

for(size_t index = 0; index < 101; ++index, ++cnt3, ++cnt5) {
if(cnt3 == 3 && cnt5 == 5) {
puts("FizzBuzz");
cnt3 = 0;
cnt5 = 0;
} else if(cnt3 == 3) {
puts("Fizz");
cnt3 = 0;
} else if(cnt5 == 5) {
puts("Buzz");
cnt5 = 0;
} else {
printf("%d\n", index);
}
}
return 0;
}

def fizzbuzz(n):
for i in range(n):
if (i % 3 == 0):
print ("fizz")
elif (i % 5 == 0):
print ("buzz")
else:
print (i)


fizzbuzz(69)

print('1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,FizzBuzz,16,17,Fizz,19,Buzz,Fizz,...'); Et Cetera, you get the idea

>plebs

print "1\n2\nfizz\n...98\nfizz\nbuzz..."

>Most efficient

The program has the same result each time. Just pre-calculating the final result and printing that is indubitably the most efficient solution.

Also any optimization you could do by using counters instead of modulus is completely outweighed by the overhead of printing the result.

Thus:

#include

int main()
{
printf("1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz\n");

return 0;
}

>no branching

>The program has the same result each time.

That's a baseless assumption, one that can easily be proven false.

It is not a baseless assumption you fucking mongoloid. The fizzbuzz assignment explicitly states for 1 to 100 inclusive.
If your solution to the assignment does not have the same result each time, it is broken.

Genuine question from non-prog. Would this method use more or less processing power?

My guess is less since the computer doesn't actually have to do any math, just spit out what it's told. Like the difference between playing a video file and live rendering it. Just on a much more insignificant scale.

In short - yes. What he did there is referred to as loop unrolling.

en.wikipedia.org/wiki/Loop_unrolling

less, but the processing power of a simple mathematical expression such as 23 % 3 == 0 is extremely insignificant

>23 % 3 == 0 is extremely insignificant
The modulus operator IS pretty slow, but it is nothing in comparison to the overhead that happens every time you call print.
It would be best if you could collapse it all like though that is an ugly solution.

Simply printing the list is very efficient if you account for processor time but very inefficient for the programmer. Especially if you want to later adapt the program for another range of "i", or other words than fizz/buzz, fizzbuzz.

One of the advantages of a program is it can be saved, edited, adapted for various purposes.

Simply printing the list is of very low value in terms of demonstrating your programming skills, which is the only point when you get asked for fizzbuzz in the first place.

#include
int main() {
printf("1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz\n")
}

Look mah, no semicolon
Look ma, no return value

>still does modulo arithmetics

Here, have a Fizzbuzz solved at compile time:

#include

template struct Value;

template< int n, typename = Value >
struct fizzbuzz
{
fizzbuzz()
{
fizzbuzz fb;
printf("%d\n", n);
}
};

template
struct fizzbuzz< n, Value >
{
fizzbuzz() { }
};

template
struct fizzbuzz< n, Value 0 && n % 15 == 0)> >
{
fizzbuzz()
{
fizzbuzz fb;
puts("fizzbuzz");
}
};

template
struct fizzbuzz< n, Value 0 && n % 3 == 0 && n % 5 != 0)> >
{
fizzbuzz()
{
fizzbuzz fb;
puts("fizz");
}
};

template
struct fizzbuzz< n, Value 0 && n % 5 == 0 && n % 3 != 0)> >
{
fizzbuzz()
{
fizzbuzz fb;
puts("buzz");
}
};

int main()
{
fizzbuzz fb;
return 0;
}

I love it and I hate it at the same time.

puts() is faster.
#include

int main(void)
{

puts("1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz");

return 0;
}

Can anyone make one in JavaScript?

scripting languages like python or pearl are more elegant than brute C, van't deny that.

Empty file is shorthand for
3:fizz
5:buzz
1,100
in fizzy, a DSL (alternatively DSE for domain specific esolang as I like to call it) I've designed for this sort of challenge.
github.com/iamevn/fizzy

program fixbux;

var
i : integer;

begin
for i := 1 to 100 do
begin
end;
write(1);
writeln();
write('2');
writeln();
write('Fiz');
writeln();
write(4);
writeln();
writeln('buzz');
write('fizz');
write(LineEnding);
writeln(3+4);
...
write(100)
end.

Me too, and I wrote that garbage.

> not using memes for variable names

object FizzBuzz extends App {
implicit class Nigger[A](a: A) {
def IF(p: => Boolean) = Some(a).filter(_ => p)
}

implicit def XD(i: Int): Boolean = i == 0

trait Negroid[A] {
def subhuman: A
def chimpOutTime(poo: A, loo: A): A
}

implicit object WhiteBoy extends Negroid[String] {
def subhuman = ""
def chimpOutTime(poo: String, loo: String) = poo + loo
}

implicit def negroidIsNotMonoid[InnerGroid](implicit whitey: Negroid[InnerGroid]): Negroid[Option[InnerGroid]] =
new Negroid[Option[InnerGroid]] {
def subhuman = None
def chimpOutTime(poo: Option[InnerGroid], loo: Option[InnerGroid]): Option[InnerGroid] = (poo, loo) match {
case (None, None) => None
case (Some(w), Some(l)) => Some(whitey.chimpOutTime(w, l))
case (myemailaddress @ Some(dotcom), None) => myemailaddress
case _ => chimpOutTime(loo, poo) // DRY amirite boys
}
}

implicit class WeWazKingz[A: Negroid](jamal: A) {
def +(sheniqua: A) = implicitly[Negroid[A]].chimpOutTime(jamal, sheniqua)
}

def fizzbuzz(i: Int): String =
("fizz".IF(i % 3) + "buzz".IF(i % 5)).getOrElse(i.toString)

1 to 100 map fizzbuzz foreach println
}

...

`Can sombody explain this meme to me?

My brother and law, who teaches computer science at a school, told this scratch BS was going to be the future? Is it a meme or what am I missing?

What the fuck is this ugly piece of shit?

what was the point of this shit again?
you can literally replace all that ugly unredable shit with python and code will look the same so why even bother trying to explain this mess to someone?

>efficient

I assume he was referring to visual coding in general, not Scratch specifically. Or so I hope.

>mfw people on Sup Forums this dumb exist.

perl -E 'say$_%15?$_%3?$_%5?$_:Buzz:Fizz:Fizzbuzz for 1..100'

no loop
int main(int argc, char* argv[]){
if(!(argc % 3)) printf("Fizz");
if(!(argc % 5)) printf("Buzz");
if(argc % 3 && argc % 5) printf("%i",argc);
printf("\n");
return argc == 100 ? 0 : main(++argc,0);
}

string FizzBuzz(int k)
{
string v = "";
if(k % 3 == 0) {v += "fizz";}
if(k % 5 == 0) {v += "buzz";}

if(v == "") {return k.toString();}
return v;
}

for (int k = 0; k

No, he was saying that visual programming was the future.

What's wrong with doing the stupid obvious solution of a bunch of if-else if-else statements?

Napier we out here

prevents exposure of ones epenis

nothing wrong with that if that works
but it's not cool or interesting at all

what is else (if)

cause you're supposed to be lazy, writing 100 lines even if you copy pasta most of it is boring

it's by design you mong

printf still has some slight overhead because it has to do a little vaargs checking. You could bypass that and just invoke the write syscall instead. But at that point, you may as well use assembly, although the compiler and user-generated code for such a trivial program will probably be the same.

> implicit def XD(i: Int): Boolean = i == 0
fucking disgusting

non-autistic version of this:
import cats.syntax._
import cats.implicits._

object FizzBuzz extends App {
implicit class ToOption(b: Boolean) {
def toOption[A](a: A): Option[A] = Option(b).collect { case true => a }
}

implicit class Divides(divisor: Int) {
def divides(dividend: Int): Boolean = dividend % divisor == 0
}

def fizzbuzz(i: Int): String =
((3 divides i).toOption("Fizz") |+| (5 divides i).toOption("Buzz")).getOrElse(i.show)

1 to 100 map fizzbuzz foreach println
}

The fastest is probably assembly with the write syscall. See: Even puts has a function call overhead.
.data
fizzbuzz: .asciiz "1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz"

.text
.globl __start

__start:
li $2, 4004
li $4, 1
la $5, fizzbuzz
li $6, 412
syscall
li $2, 4001
li $4, 0
syscall

Or something like this.

my dude
bits 32
section .data
fizzbuzz db `1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz\n`
len equ $ - fizzbuzz

section .text
global _start
_start:
mov edx, len
mov ecx, fizzbuzz
mov ebx, 1
mov eax, 4
int 0x80

mov ebx, 0
mov eax, 1
int 0x80

(too lazy to write in gas syntax)

for i = 1, 100 do
io.write(tostring(i) .. ": ")
if i % 3 == 0 then io.write("Fizz") end
if i % 5 == 0 then io.write("Buzz") end
io.write("\r\n")
end

No modulus or division. J
,/@:>@(}.^:(-.@(a: = {.@}.)))"1 (,.:i.100)),.(|.^:(a: = {.))"1 (3 (100&$@(a:&,@]^:(

...

IDENTIFICATION DIVISION.
PROGRAM-ID. fizzbuzz.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CNT PIC 9(03) VALUE 1.
01 REM PIC 9(03) VALUE 0.
01 QUOTIENT PIC 9(03) VALUE 0.
PROCEDURE DIVISION.
*
PERFORM UNTIL CNT > 100
DIVIDE 15 INTO CNT GIVING QUOTIENT REMAINDER REM
IF REM = 0
THEN
DISPLAY "FizzBuzz " WITH NO ADVANCING
ELSE
DIVIDE 3 INTO CNT GIVING QUOTIENT REMAINDER REM
IF REM = 0
THEN
DISPLAY "Fizz " WITH NO ADVANCING
ELSE
DIVIDE 5 INTO CNT GIVING QUOTIENT REMAINDER REM
IF REM = 0
THEN
DISPLAY "Buzz " WITH NO ADVANCING
ELSE
DISPLAY CNT " " WITH NO ADVANCING
END-IF
END-IF
END-IF
ADD 1 TO CNT
END-PERFORM
DISPLAY ""
STOP RUN.

>repeat until x=101
>not repeat until x>=100
this infuriates me more than the fact that this is done in scrach

Runs in 0.189 milliseconds with printing, less than 0.0015 with printing suppressed (on repl.it, which definitely isn't the fastest)
package main

import ("fmt
"time")

func main() {
start := time.Now()
var fzbz [100]string
for i := 2; i < 100 ; i += 3 {
fzbz[i] = "Fizz"
}
for j := 4 ; j < 100 ; j += 5 {
fzbz[j] += "Buzz"
}
for n, fz := range fzbz {
if fz == "" {
fmt.Println(n+1)
} else {
fmt.Println(fz)
}
}
fmt.Println(time.Since(start))
}

Forgot to close the quote while importing fmt :(

const x = [0,""];
while(true){
x[0]++;
x[1]="";
if(x[0]/3 == Math.round(x[0]/3)){
x[1]+="Fizz";
}
if(x[0]/5 == Math.round(x[0]/6)){
x[1] += "Buzz";
}
if(x[1] === ""){
console.log(x[0]);
} else {
console.log(x[1]);
}
if(x[0] > 99){
break;
}
}

Doesn't work after x[0] gets to 15

33oFizzqqaBuzz5kq19@q:%s#^$#\=line('.')

Vim?

Sorry family but this wouldn't work

?

Any collisions of 3 and 5 would just output fizz, so 15 would just be fizz

fzbz = ["", "", "Fizz", "", "Buzz", "Fizz", "", "", "Fizz", "Buzz", "", "Fizz", "", "", "FizzBuzz"]*7
print("\n".join(fzbz[n] or str(n) for n in range(0, 100)))

Takes 29μs
Up to 1000 takes 200μs, 1000000 takes 0.2 seconds. Linear time, niggas.
A slightly less (more?) autistic solution based somewhat loosely on this one:
from timeit import default_timer
def fizzbuzz(sounds, limit):
fzbz = [""]*limit
soundmaps = []
for num in sounds:
soundmaps.append((1+ limit//num)*([""]*(num-1) + [sounds[num]]))
print("\n".join(("".join([sound[n] for sound in soundmaps]) or str(n+1) for n in range(1, limit))))

start = default_timer()
fizzbuzz({3:"Fizz", 5:"Buzz", 7:"Bang"}, 100)
print(default_timer() - start)

Which also appears to be linear time with respect to limit, though I'm sure it's more complicated with the number of number-sound pairs.
Takes 0.8 seconds to do "FizzBuzzBang" up to a million.

I combined this two shits
#include
int main(int argc) {
char f[] = "FizzBuzz%d";
f[8-argc%5&12] = 0;
printf (f+(-argc%3&4+f[8]/8), argc);
return argc == (99+puts("")) ? 0 : main(++argc);
}

if (i%3==0 and i%5==0)

Retard.

Even less autistic

scala> def from(n: Int): Stream[Int] = n #:: from(n+1)
from: (n: Int)Stream[Int]

scala> val fizzbuzz = from(1).map(n => if( (n%3) == 0) "fizz" else if ((n%5) == 0) "buzz" else n)
fizzbuzz: scala.collection.immutable.Stream[Any] = Stream(1, ?)

scala> fizzbuzz.take(100).toList
res3: List[Any] = List(1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizz, 16, 17, fizz, 19, buzz, fizz, 22, 23, fizz, buzz, 26, fizz, 28, 29, fizz, 31, 32, fizz, 34, buzz, fizz, 37, 38, fizz, buzz, 41, fizz, 43, 44, fizz, 46, 47, fizz, 49, buzz, fizz, 52, 53, fizz, buzz, 56, fizz, 58, 59, fizz, 61, 62, fizz, 64, buzz, fizz, 67, 68, fizz, buzz, 71, fizz, 73, 74, fizz, 76, 77, fizz, 79, buzz, fizz, 82, 83, fizz, buzz, 86, fizz, 88, 89, fizz, 91, 92, fizz, 94, buzz, fizz, 97, 98, fizz, buzz)

Oh, a number that has mod 3 and 5 == 0 is "fizz buzz".
Forgot it, too lazy to rewrite. Whatever

The only correct answer: github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition

that's not efficient, that's short. having to fire up the python interpreter, converting that to bytecode and interpreting that bytecode is not efficient.

For numbers with more than 4 digits, this results in "fizz45354".

if you put it as if statements instead of using elseif you dont even need the first statement since it will output fizz and buzz

if you put it as if statements instead of using elseif you dont even need the first statement since it will output fizz and buzz since it will output for mod 3 and mod 5

fuck, s/6/5

try adding more numbers to fizz on.
that is a hell on if-else statements, but really easy in for example or

Scratch > Python
go fuck yourself