FizzBuzz challenge

It's time for the FizzBuzz challenge. In a language of your choice, post your FizzBuzz implementation.

Fizzbuzz is a program where you print the numbers from 1 to 100. Numbers that are divisible by 3 should be replaced with "fizz". Numbers that are divisible by 5 should be replaced by "buzz". Numbers that are divisible by both 3 and 5 should be replaced by "fizzbuzz".

Level beginner: Post a working fizzbuzz
Level 0: Recursion only
Level 1: No modulo
Level 2: No variable declarations (except function arguments and functions)
Level 3: No branching conditionals (if, while, etc) on 3 and 5
Level 4: No branching conditional on 15
Level 4: No +1 incrementation on any variable
Level 5: No language built-in arithmetics (+, -, *, /)

Attached: fizzbuzz.png (480x350, 21K)

Other urls found in this thread:

fizzbuzzed.io/to=1000
en.wikipedia.org/wiki/Modular_arithmetic
twitter.com/NSFWRedditImage

>needlessly complicating things
Why?

int i;
const char* arr[] = {"fizzbuzz", "%d", "%d", "fizz", "%d", "buzz", "fizz", %d", "%d", "fizz", "buzz", "%d", "fizz", "%d", "%d"};

for (i = 1; i

Because it's fun

Those restrictions are retarded because the most obvious way to meet them all is to just print a long string with the answers, IE

print("12fizz4buzz...");

did i do it right?

Attached: 687474703a2f2f692e696d6775722e636f6d2f576a6b777033352e706e67.png (735x795, 88K)

That's the point

I think that should be all of them.

eq = $(filter $1,$2)
slice = $(wordlist 2,$(words $1),$1)
repl = $(if $(call eq,$(words $4),$1),$3,$(call repl,$1,$2,$3 $2,$(words $4) $4))
map = $(if $2,$(call map,$1,$(call slice,$2),$3,$4 $(call $1,$(firstword $2),$3,$4)),$4)
rotate = $(call slice,$1) $(firstword $1)
zip3 = $(if $2,$(call zip3,$1,$(call slice,$2),$(call rotate,$3),$(call rotate,$4),$5 $(call $1,$(firstword $2),$(firstword $3),$(firstword $4))),$5)
inc = $(words $(call repl,$1,_) _)

count = $(words $3)
cycle = $(call slice,$(call repl,$1,_)) $2

fizz := $(call cycle,3,fizz)
buzz := $(call cycle,5,buzz)
numbers := $(call map,inc,$(call map,count,$(call repl,100,_)))

select = $(if $1,$1,$2)
fizzbuzz = $(call select,$(if $(call eq,$2,_),,$2)$(if $(call eq,$3,_),,$3),$1)

result := $(call zip3,fizzbuzz,$(numbers),$(fizz),$(buzz))
println = $(info $1)

.PHONY: all
all: ; $(call map,println,$(result))

what the hell is this, is that fucking make?

Attached: puke-cup.gif (300x225, 1.9M)

Post fizzbuzzes

Restrictions should be about making people come up with a clever work-around and demonstrating a mastery of all the available tools.

Programmers should be lazy and smart. If you see a loophole, seize it, for all its worth.

Attached: 2019-06-21-105719_724x1047_scrot.png (724x1047, 99K)

Great, now do 1000.

Also
>printf
>not puts
git gud

its not really difficult to do
333oFizzqqABuzz5kq199@q:%s#^$#\=line('.')qqIprintf("A\n");quu%normal@qoreturn 0;}ggO#include int main(){:wq

Attached: fizzbuzz1000.png (1135x464, 65K)

Now this is meta programming.

#include

int add(int, int);
int divides(int, int);

int main()
{
int i;
for (i = 1; i < 100; i = add(i, 1)) {
if (divides(15, i)) {
printf("FizzBuzz\n");
}
else if (divides(3, i)) {
printf("Fizz\n");
}
else if (divides(5, i)) {
printf("Buzz\n");
}
else {
printf("%d\n", i);
}
}
return 0;
}

int add(int x, int y)
{
return (y == 0) ? x : add((x^y), (x&y)

f x
| x `mod` 3 == 0 && x `mod` 5 == 0 = "FizzBuzz"
| x `mod` 3 == 0 = "Fizz"
| x `mod` 5 == 0 = "Buzz"
| otherwise = show x

map f [1..100]

Fuck it. I rather just copy my old one than to waste time on higher levels.
>Level 5: No language built-in arithmetics (+, -, *, /)
Just imagine I used bc for all calculations.

#!/bin/bash

(( $# == 0 )) && { echo "Usage: $0 START END"; exit; }

declare -i start="$1"
declare -i end="$2"

(( start > end )) && { echo "Error: start ($start) > end ($end)"; exit; }

for (( i=start; i

what language is this

looks like haskell

ty

first fizzbuzz, no bully pls

#include

int main()
{
for (int i = 0; i

it is wrong

why

So you print fizz, buzz and fizzbuzz for numbers like 15?
Shouldn't it be rather something like
#include

int main()
{
for (int i = 1; i

Data Scientist (TM)(R)(C)(Pajeet) incoming.

library(tidyverse)

fizzbuzz %
mutate(., divisablefive = numbers%%5, divisablethree = numbers%%3) %>%
mutate(., Fizz= if_else(divisablethree == 0, "Fizz", "" ),
Buzz = if_else(divisablefive == 0, "Buzz", "") %>%
unite(.,"Result",Fizz,Buzz, sep = "")

print(fizzbuzz$Result)

Attached: 1556311350120.jpg (1080x1080, 105K)

Y'all niggers doing it wrong.
In order to meet all the requirements you should print out manually typed results, but you also have to put it in a recursive function to meet level 0 condition. There's no limitations on number of prints, jus make it an infinite recursive loop

((fn [n] (take n (map (fn [n s] (if (clojure.string/blank? s) n s)) (drop 1 (range)) (map (fn [f b] (clojure.string/join [f b])) (cycle fizz) (cycle buzz))))) 30)

t = {[true] = {[true] = "fizzbuzz", [false] = "fizz"}, [false] = {[true] = "buzz", [false] = false}}

for i=1, 100 do
print(t[i - math.floor(i/3)*3 == 0][i - math.floor(i/5)*5 == 0] or i)
end

Except for the fact that it's a Makefile, it's unironically a good implementation.

What's the difference between = and := ?

nice

One is instantly expanded, the other is lazily expanded.

Bump

Jow Forums is the only community that thinks Fizzbuzz is a programming challenge.

If 15 divides a number, it's supposed to say FizzBuzz. In your loop, when 15 divides a number, it prints "fizz", then "buzz", then "fizzbuzz"

What language is this uwu

for i in range(100):
print({0:'FizzBuzz'}.get(i%15, {0:'Fizz'}.get(i%3, {0:'Buzz'}.get(i%5, i))))

>Jow Forums is the only community that thinks Fizzbuzz is a programming challenge.
How much of a boomer do you need to be if you don't understand what memes are.

>still using var in the year 2018+1 of our lord and savior

Goddammit. Took me a long time to get that one.

saved

Level 5.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using static System.Console;
namespace fizzbuzz
{
class Program
{
static void Main(string[] args)
{
WriteLine
(
Regex.Replace
(
Regex.Replace
(
string.Join(" ", Enumerable.Range(1,100)),
@"(\d+ \d+ )\d+",
"$1Fizz"
),
@"([^ ]+ [^ ]+ [^ ]+ [^ ]+ )(\d+)?([^\d ]+)?",
"$1$3Buzz"
)
);
}
}
}

fizzbuzz

My favorite solution so far
#include
void main() {
const char *vars[] = { "%d ","Fizz ","Buzz ","FizzBuzz " };
for (int i = 1; i < 101; ++i)
printf(vars[(i % 3 == 0) + (i % 5 == 0)*2], i);
}

nice, i come from a statistics background so i am still scared of the stuff that looks more like programming (loops and all).

Attached: 1557379846487.jpg (1280x720, 350K)

#include

int main() {
for (int i = 0; i

I'm past 20, and that's why I think memes aren't jokes by themselves.

Thanks! R is a particularly slow language, so it's good to know how to optimize if you're making specialized stuff for large data sets.

Yeah, JavaScript community thinks it's a math challenge.

Post Sieve of Eratosthenes instead then, using the same limitations.

const int limit = 100;
int i = 1, count = 1;
while (i

Attached: SmugTiddies.png (460x653, 246K)

This doesn't work how you think it works.

Yeah I noticed then fixed it but I already posted this version so whatever

>Post Sieve of Eratosthenes instead then, using the same limitations.
That's simply not possible

i thought it's a challenge for ui designers

Attached: 1431500368342.jpg (1260x910, 380K)

I think it is

talk is cheap
show me the code

Attached: SmugFujiwaraChika.jpg (768x768, 190K)

Fine, as you wish

slice = $(wordlist 2,$(words $1),$1)
repl = $(if $(call eq,$(words $4),$1),$3,$(call repl,$1,$2,$3 $2,$(words $4) $4))
rotate = $(call slice,$1) $(firstword $1)
map = $(if $2,$(call map,$1,$(call slice,$2),$3,$4 $(call $1,$3,$(firstword $2),$4)),$4)
fold = $(if $2,$(call fold,$1,$(call slice,$2),$(call $1,$3,$(firstword $2))),$3)
zip = $(if $2,$(call zip,$1,$(call slice,$2),$(call rotate,$3),$4 $(call $1,$(firstword $2),$(firstword $3))),$4)

inc = $(words $(call repl,$1,_) _)
max = $(words $(subst __,_,$(join $(call repl,$1,_),$(call repl,$2,_))))

eq = $(filter $1,$2)
gt = $(filter-out $2,$(call max,$1,$2))
gte = $(call gt,$1,$2)$(call eq,$1,$2)

add = $(words $(call repl,$1,_) $(call repl,$2,_))
sub = $(words $(filter-out __,$(join $(call repl,$1,_),$(call repl,$2,_))))
mul = $(words $(call repl,$1,$(call repl,$2,_)))
div = $(if $(call gte,$1,$2),$(call add,1,$(call div,$(call sub,$1,$2),$2)),0)
double = $(call mul,2,$1)

isqrt_cand = $(if $(call gt,$(call mul,$(call inc,$1),$(call inc,$1)),$2),$1,$(call inc,$1))
isqrt = $(if $(call gte,$1,2),$(call isqrt_cand,$(call double,$(call isqrt,$(call div,$1,4))),$1),$1)

head = $(firstword $2)
count = $(words $3)

loop = $(call fold,$2,$(call map,count,$(call repl,$1,_)),$3)

skip = $(call loop,$1,slice,$2)

cycle = $(call slice,$(call repl,$1,_)) 0
eliminate = $(if $(call eq,$2,0),0,$1)
sieve = $(call zip,head,$(call repl,$(call double,$1),_),$2) $(call zip,eliminate,$(call skip,$(call double,$1),$2),$(call loop,$(call sub,$1,1),rotate,$(call cycle,$1)))

dispatch = $(if $(call gt,$2,1),$(call sieve,$2,$1),$1)

n := 100
s := $(call isqrt,$(n))
numbers := $(call map,count,$(call repl,$(n),_))
primes := $(filter-out 0,$(call skip,2,$(call loop,$(s),dispatch,$(numbers))))

print = $(info $2)

.PHONY: all
all: ; $(call map,print,$(primes))

but typing out it all beforehand is too much work, being lazy means writing an actual solution

Like or Clever metaprogramming that exploits the loopholes.

#include
template
struct increment
{
static constexpr int apply()
{
if constexpr (N & Bit) {
return increment

4chin didnt allow me to post the full code, replace +1 by increment and you get your answer to all 5 levels at compile time

curl fizzbuzzed.io/to=1000

Done

//here comes the ugly code on long time forgotten c
//i don't remember the task exactly and do now want to
int main (){
for (i=0; i

#include

Nice

Ruby, iterative
def fb_iter(max: 100)
max.times do |n.next|
puts "FizzBuzz" if n % 15 == 0
puts "Fizz" if n % 3 == 0 && n % 5 != 0
puts "Buzz" if n % 3 != 0 && n % 5 == 0
puts n if n % 3 != 0 && n % 5 != 0
end
end

>no branching conditionals
>no +1
>no arithmetic

Ruby, recursive
def fb_rec(num: 1)
return if num > 100
puts "FizzBuzz" if num % 15 == 0
puts "Fizz" if num % 3 == 0 && num % 5 != 0
puts "Buzz" if num % 3 != 0 && num % 5 == 0
puts num if num % 3 != 0 && num % 5 != 0
fb(num: num.next)
end

>recursion only
>no variable declaration
>no branching conditionals
>no +1
>no arithmetic

>if
>no branching conditionals
Pick one

based and redpilled c++

i assumed branching meant nested

for i in range(1,101):
f='fizz'
b='buzz'

if i % 5 ==0 and i % 3==0:
print(f+b)

elif i % 3 == 0:
print(f)
i+=1
elif i % 5 == 0:
print(b)
i+=1

else:
print(str(i))
i+=1


hey guys i literally started learning python last week, modulo was kind of hard for me to figure out but i think i did it. obviously level beginner. this was fun! i am definately still in the honeymoon phase of language learning though... any suggestions?

It's pretty anyway, so I approve.

Learn more math.
en.wikipedia.org/wiki/Modular_arithmetic

i += 1 is completely unnecessary, it's updated by the for loop.

Other than that, pretty good solution. You could always do i % 15 == 0 instead of both 5 and 3, but that's honestly a matter of taste.

OH WOW, dammit man
ok i will try. do you think its going to be necessary for actual projects? people keep saying "its used more often than you think" but then dont give me any examples so im just like uh ok boss

It's used literally everywhere. Algorithms are just expressions of mathematics. You just told us you had trouble with modulo, which is fucking math, to do a fizzbuzz. There's your example right there. All loops, scheduling, conditional branching, is just mathematics applied. If you suck asshole at math you're just making more work for yourself.
You don't need to be a wizard but you should learn discrete, differential, basic calculus, and linear algebra if you're serious.

Discrete is the bare minimum though.

LINQ
Console.WriteLine(
string.Join(",", Enumerable
.Range(1, 100)
.Select(x =>
new[] {3, 6, 9, 12}.Contains(x % 15) ? "Fizz" :
new[] {5, 10}.Contains(x % 15) ? "Buzz" :
x % 15 == 0 ? "FizzBuzz" : x.ToString()
)
));

Attached: GyaruKaho.jpg (1920x2856, 1024K)

>based C++
>static constexpr void run# over and over
I could do this with a couple C macros, jesus.

perl
say "Fizz"x!($_%3)."Buzz"x!($_%5)||$_ for 1..100

const fizzbuzz = (a) => return a % 2 == 0 && a % 3 == 0 ? "Fizzbuzz" : a % 2 == 0 ? "Fizz" : "Buzz"

for(let i = 0; i < 100; i++){
console.log(fizzbuzz(i))
}

>a % 2 == 0

lol, my bad, i'm stupid

>Nesting ternaries
kill yourself.

Y'all faggots using ifs and conditional expressions. Conditional jumps are inefficient. This version uses all arithmetic operators.

#include

int main(void) {
char s[] = "%i\n\0\360\237\215\276\360\237\220\235\n";
short *p = (short*)s, x = p[4] ^ p[6], n, m;

for(m = n = 1; n

>i found this code golf on google, am i cool now?

Neat

Fucking wizardry
How do I become a Cnile?

If it's out there, it's from another time I posted it here.
It doesn't exactly fizzbuzz either, it uses emojis instead of fizz or buzz.

explain this magic please

It's a vim macro.

that's a vim macro, which is meaningless because you could write a script in any language to do this.

It’s a vim macro that replicates line and substitutes text as it goes

I'm about to end this man's whole career.
#include
void main() {
const char *vars[] = { "FizzBuzz ","%d ","%d ","Fizz ","%d ","Buzz ","Fizz ","%d ","%d ","Fizz ","Buzz ","%d ","Fizz ","%d ","%d " };
for (int i = 1; i < 101; ++i)
printf(vars[i % 15], i);
}

See the first reply ITT

void fizzbuzz(int n) {
StringBuilder sb = new StringBuilder();
for(int i=1; i

Damn, I guess it was too obvious.

const char* arr[] = {"fizzbuzz", "%d", "%d", "fizz", "%d", "buzz", "fizz", %d", "%d", "fizz", "buzz", "%d", "fizz", "%d", "%d"};
for (int i = 0; i < 90; i += 15) {
for (int j = 0; i < 15; j++) {
printf(arr[j], i);
}
}
for (i = 0; i < 10; i++) {
printf(arr[i], i);
}

There, no modulo.