Fizz Buzz Time

So, this time you are only allowed to use 2 if blocks with optional else.
Every variable you declare should be final / immutable.
The only exception is the outer loop.

If you wish extra difficulty: no arrays allowed.

Just a reminder:
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

Syntactic sugar to avoid the restrictions is NOT allowed. Examples of what is not allowed:
import fizzbuzz
test ? if_true : if_false
del varname

Easy.
Print ("1")
Print ("2")
Print ("Fizz")
Etc

Wonder if that would take more or less CPU cycles than an algorithm that goes through a loop with nested if statements.

why would you ever need more than 2 ifs ?

less obviously, are you retarded ?

Nah it will not. Unless you execute it on an ancient CPU with an extremely limited cache.

Yes. Explain pls.

Final variables only. You are not allowed to change the value of the existing variable. It is getting challenging with only 2 ifs.

how could it possibly take more cpu cycles ? it's just print, no conditionals, no incrementation of counter variables and most likely (depends on if the compiler/interpreter unrolls the loops) no loop (cmp/test + jmp)

[source]
#include

int main(int argc, char **argv)
{
for (int i = 1; i

So if you wanted to write the most efficient fizzbuzz on an interview you should just do ?

so i just use a pointer (whichs value doesn't change) and change the memory it points to ?
but i guess thats not what you meant
so by final variables you mean only constants + no arrays ?
i assume strings are considered char arrays ?

Talking to and waiting for the slow RAM on your [pseudo-] von neumann CPU.

I'm new around here.


#include

int main(int argc, char **argv)
{
for (int i = 1; i

>Three if statements in a challenge that limits you to two if statements
Really?

cpu efficient? yes. but the binary would be much bigger e.g.
on an interview? nope. but fizzbuzz won't be used for interviews anyway.

{
like this
}

fml

A[I]←1+I←(0⍷A)/⍳⍴A←('FizzBuzz' 'Fizz’ 'Buzz' 0)[2⊥¨×(⊂3 5)|¨1+⍳100]

Consider it to be a pointer on a variable that changes. Forgot to mention it for the c/cpp world, was too long ago...
Though strings technically are, you still need to output something.
Don't get too captious, I bet you understood the idea of the task, and cheating is no go ;) Did not want to write poems in the first post, readability and shit.

Too many IF-s. Busted.

import time
__ = time.time()
while True:
time.sleep(1)
_ = int(time.time() - __)
print('Fizz'[0:int(_%3==0)*4]+'Buzz'[0:int(_%5==0)*4] or _)
if _ >= 100:
break

if you can't change any value or do any pointer arithmetic and arrays are forbidden, the only option would be but i'm probably missing something.

Dude you exploded my brain.

Now we have a winner in the easy difficulty using arrays (lists). Nice use of if btw.
And go make import this

You are missing something.

Still waiting for a solution without arrays/lists and other crazy shit stuff. It exists.

now i'm really curious how it's possible with immutable variables. i mean it's possible without using any variables at all just writing/reading from the stack directly wie bp/sp but thats kinda language specific so i'd say it counts as "cheating"

Welcome.
And, no, my bio-grep-eye shows you have used 3 IFs.

I will post the solution if I don't fall asleep earlier. Otherwise make a disposable email box and I send it.

lul


li{
list-style-type: none
}
li:nth-child(3n+1), li:nth-child(5n+1){
font-size: 0;
}
li:nth-child(3n+1):before{
content: 'fizz';
font-size: initial;
}
li:nth-child(5n+1):after{
content: 'buzz';
font-size: initial;
}

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100

import time
__ = time.time()
while (time.time() - __

What the fuck is that image?

I figure if you're gonna go janky, go for broke:

function f0(x) { document.write("FizzBuzz") }
function f1(x) { document.write(x) }
function f2(x) { document.write(x) }
function f3(x) { document.write("Fizz") }
function f4(x) { document.write(x) }
function f5(x) { document.write("Buzz") }
function f6(x) { document.write("Fizz") }
function f7(x) { document.write(x) }
function f8(x) { document.write(x) }
function f9(x) { document.write("Fizz") }
function f10(x) { document.write("Buzz") }
function f11(x) { document.write(x) }
function f12(x) { document.write("Fizz") }
function f13(x) { document.write(x) }
function f14(x) { document.write(x) }
for(var i = 1; i

import time

sleep(3153600000)

print('fizzbuzz :^)')

sleep(1)


Job offer when

FizzBuzz, what the fuck else would it be?

What is up with that padding? That a faggot thing?

I like this for thinking outside the box.

default html padding is 8px

Shit son, what's got your panties all tied up in a bunch?

The image.

I was really overthinking it

def fizzbuzz(start, end):
print(((start%3==0 and 'Fizz') or '')+((start%5==0 and 'Buzz') or '') or start)
if start < end:
fizzbuzz(start + 1, end)

fizzbuzz(1, 100)

oh, that's a shadow default osx's window screenshot tool makes

can't be bothered to disable it

Mostly reading retarded questions.

Sorry, not at all. Cheating. Here you are using extra conditions hidden behind logical functions.
x % 3 == 0 and 'fizz' or 'buzz'
if (x % 3 == 0) fizz; else buzz;

But I'm glad you find it interesting to explore new features. Check checkio.org

I am not sure whether I hate you or fascinated of the enterprise creativity.

Look ma, no if blocks!
;Boolean short-circuiting
(for ([n (in-range 100)])
(or (and (remainder n 3) (~a "Fizz"))
(and (remainder n 5) (~a "Buzz"))
(void n))

Hidden conditions, same as Also fizzbuzz(1, 9000) just realized it's recursive, maniac.

Come one, I want to see here a nice algorithm driven solution finally!

>or
>and
>and

That's one if-statement too many.

>inb4 you don't know that 'and' and 'or' are semantic sugar

Sure is pleb in here.

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

>I am not sure whether I hate you or fascinated of the enterprise creativity.
Strictly speaking, my solution is dictionary-based, but this gets into why it's so tricky to impose restrictions like "only two 'if' blocks" (what about languages that use pattern-matching?) and "no arrays" (what about compiler/interpreter internal arrays?). Because if you try restricting symbol table usage, then these modern interpreted languages are straight out, and we'd be writing x86 assembly with restrictions on which/how many instructions we can use.

Do conditions in for loops count as ifs?

#include

int main(int argc, char *argv[]) {
for (int i = 1; i

What do you believe this prints at numbers 3, 5 and 15

No, no, I seen solutions with the same idea before. But not with functions and eval(). It's actually enough to form a 15-element iterable of any type. That's why I put the "no arrays for extra difficulty" part. Since it is not possible to explain or even find each detail for each possible implementation in each existing programming language, I kept the conditions minimalisting hoping everyone will understand the idea of not using iterables solving the task itself. Otherwise anyone would be disqualified by using array of characters to store the source code.

In a way, you do. This way to solve it I never seen before. And you are surprisingly close to a clean one, without manipulating the actual output or other language/compiler/environment specific stuff.

look ma, no macros!
[CODE]
(doseq [t (range 1 101)]
(println
(match [(mod t 3) (mod t 5)]
[0 0] "FizzBuzz"
[0 _] "Fizz"
[_ 0] "Buzz"
:else t)))
[/CODE]
using clojure with core.match here

This solution violates all the restrictions, but it hoists them to the terminal implementation. The '\b' character relies on having a mutable character array (called the terminal buffer), and the terminal's implementation of '\b' requires a condition (to make sure it doesn't backspace past the beginning of the line).
It's clever and funny, but it's a more exotic version of "import fizzbuzz."

As for me, it would fall under
>Syntactic sugar
and other cheating solutions.

Reminder: there actually IS a clean solution, implementable in (nearly?) any popular programming language.

Sorry for multiposting.

Unfortunately I don't speak Clojure, care to explain the code?
Also, you seem to use arrays. Easy difficulty level solved for you.

Then would be considered wrong, right?

Fuck wikipedia links, if you wanted that you'd google. Pattern matching is a technique used in functional programming to trick a function into doing a different thing based on the parameters it receives, like in a "switch" instruction, but not really.

Not using arrays here - (doseq [t (range 1 101)] ... ) means do something for every t in a sequence obtained by taking a range 1...100. This data structure is immutable, so if it's not returned it'll be garbage collected. If you are willing to consider it like "using an array or a list" anyways, then this instruction may be substituted by (dotimes [t 101] ...) - yields the same result without creating any data structure.

Right, wrong. But a very beret guy way of thinking I should admit.

The "match" is where all the conditions are buried. It's the standard solution, but with a four-clause match instead of if/else if/else if/else. If you speak Haskell, it's like,

data Result = StrRes String | IntRes Int
deriving Show

solve :: Int -> Int -> Int -> Result
solve 0 0 _ = StrRes "FizzBuzz"
solve 0 _ _ = StrRes "Fizz"
solve _ 0 _ = StrRes "Buzz"
solve _ _ x = IntRes x

massage :: Int -> Result
massage x = solve (mod x 3) (mod x 5) x

main :: IO ()
main = print (map massage [1..100])

Okay, thanks for explaining, stay calm. Maybe today I find a nice reason to finally try out some functional languages.

Oh well, this is a kind of problem that is not solvable without conditional constructs. Strictly speaking, (match ...) in clojure is not syntactic sugar for IF, and neither is the macro of the lisp guy before me P.S. for pythonfags, here's your python of the christ:
for i in range(1,101):
print("FizzBuzz"[i*i%3*4:8--i**4%5] or i)
sorry if i seemed angry, I was not

So effectively there are 4 mutually exclusive branches, which is impossible using 2 ifs (you always get 3 branches).

>Strictly speaking, (match ...) in clojure is not syntactic sugar for IF
Sort of, but I think what OP was going for is that "if" really means "conditional branch." In general for functional languages, match is a very heavyweight conditional branch (heavyweight because they often include safe downcasting). Certainly building this as a pattern-match problem with more than three clauses violates the spirit of OP's restrictions.
Indeed, in a nonstrict functional language like Haskell, you can do away with if entirely:

if' :: Bool -> a -> a -> a
if' True r _ = r
if' _ _ r = r


But note that does rely on nonstrict evaluation.

My favorite solution in Python
(SPOILERS if you still want to try yourself) pastebin.com/hFfSNa7a

I want to apologize if it became "guess my code" kind of party. Hope still had some fun & g'nite.

That's wrong. If your code is short, the pc is maybe able to work almost only on the cache without using the ram that much. Which is a lot faster.

yeah, worded it wrong, the original question was about cpu cycles, not actual efficiency

>implementable in (nearly?) any popular programming language.
then what you did contains a for (which uses an if to check the end of the range) and two other ifs, making 3.

def fb(x):
return ( 'Fizz'*len('x'[x%3:] )
+ 'Buzz'*len('x'[x%5:] ) ) or str(x)

print(*map(fb, range(1, 101)))

Here's an array one in JS:

for (var i = 1; i < 101; i++) print((["Fizz", "" ,""][(i % 3)] + ["Buzz", "", "", "", ""][(i % 5)]) || i);

I'm pretty sure map has two if statements in it

Without a loop it would not make any sense therefore >The only exception is the outer loop.
not to call the goddamn function a hundred of times.

If you're already using arrays, why not to implement a single one
["FizzBuzz", 1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14] and reference it by array[i % 15] replacing integers with i? Nah, nevermimd, just asking.

>hiding conditions in the inner loop

fail

less fun ;)

#include

const char* fizzbuzz[] = {
"%d\n",
"%d\n",
"Fizz\n",
"%d\n",
"Buzz\n",
"Fizz\n",
"%d\n",
"%d\n",
"Fizz\n",
"Buzz\n",
"%d\n",
"Fizz\n",
"%d\n",
"%d\n",
"FizzBuzz\n"
};

int main(const int argc, const char **argv) {
for (int i = 1; i

public class fb {
public static void main (String args[]) {
for(int i=0;i

_.range(1,100).map(n => n%3===0?n%5===0?"FizzBuzz":"Fizz":n%5===0?"Buzz":n)

here u go

#include
int main(void)
{
for(int i=1; i

> India
Trolling?
P.S. check the output, compare with FizzBuzz task definition.

I think we have a real wiener here.

Let me know if any of you guys have a better solution.

private static void gFizzbuzz() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 1; i

Abusing the output again. \r overwriting the output plus conditionals hidden using operators. Soz.

A better solution would be a solution with the correct output

>stringBuilder
explicitly violates
> Every variable you declare should be final / immutable.
Under the hood Sting operations return new object every time effectively changing the reference variable is. StringBuilder is just a nice wrapper around it with some optimizations.
>Let me know
Why not this? Cannot see anything wrong with it.

I know a solution! It's called brain and hand. It can be implementable in almost any language as an AI, as it is possible to simulate a miniscule portion of a brain at mathematical terms, and the brain does not do conditions! Just pure simple conversions however you imagine it. The hand does most of the outer work for you.

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

Tada.

I feel sort of obligated to point out that any solution dependent on nonliteral strings is array-based. The requirement of immutability is a false constraint, because of course any mutation can be replaced by a looping copy. And since you're doing string concatenation as a supposedly primitive operation, you're implicitly allowing looping copies.

Given that, the \b and \r solutions could be reimplemented in ways that follow the rules as much as your version does. I wrote this version in Haskell just to be clear that these are all immutable operations:

solve :: Int -> String
solve i =
let res = show i in
let res2 = if mod i 3 == 0 then "Fizz " else res in
let res3 = if mod i 5 == 0 then take (length res2 - 3) res2 ++ "Buzz" else res2 in
res3

main :: IO ()
main = mapM_ putStrLn (map solve [1..100])


The "problem" with this is that the take function contains a condition:

take :: Int -> [a] -> [a]
take 0 xs = []
take n (x:xs) = x : take (n - 1) xs


But at the same time, list concatenation contains nearly the same condition:

(++) :: [a] -> [a] -> [a]
(++) [] ys = ys
(++) (x:xs) ys = x : xs ++ ys


This is all to say, you violated your own rules, but the ways in which you did so are quite obscure.

Woops, didn't read properly last time. This one has proper output.


private static void gFizzbuzz() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 1; i

Just agree there could stand 3 print operators instead: to print var1, to print var3 and to print \n and no string operations would be involved.

Okay, but the implementation of "print" will do the same thing. I was comparing this to \b and \r solutions, which only have the problem that they implicitly embed loops in the way that terminals are implemented. I brought this up because of the use of nonliteral strings, which is what makes them feel more list-like.

I mean it's all up to interpretation, obviously, but if you think about what's going on: a good compiler could print a string literal without runtime branching by unrolling the loop, while any operation on a nonliteral string has to be a runtime operation, so it must always branch. I think this is why anything that's based on any kind of string manipulation, including printing from pointers, feels like it goes against the spirit of the question.

I'm unemployed, actually. I left LANL after my postdoc ended two-ish years ago to try and figure out what to do with the rest of my life. But if you want to talk about that more, you can use this temporary email address: [email protected]

for i in range(1, 101):
start = 0 + (i % 3 != 0) * 4
end = 8 - (i % 5 != 0) * 4
if (i % 3 == 0) or (i % 5 == 0):
print('FizzBuzz'[start:end])
else:
print(i)

>2016
>using if or conditionals on a fizzbuzz ever
how is unemployment gıing for you?

#include
#include

int fizz(int i){
int (*f[])(int) = {fizz, exit};
static char* s[] = {"%d\n", "fizz\n", "buzz\n", "fizzbuzz\n"};
printf(s[!(i%3) + !(i%5)*2], i);
f[i/100]((i+1)%101);
}

int main(){
fizz(1);
}

cleaner
for i in range(1, 101):
start = 0 + (i % 3 > 0) * 4
end = 8 - (i % 5 > 0) * 4
if (start - end) == 0:
print(i)
else:
print('FizzBuzz'[start:end])

Yes, but it's clearly implemented incorrectly

Just declare the function outside the main program

>import FizzBuzz
>mainFunction()
>print("OP is still a fucking faggot")

there 2 lines you autistic faggots

and here version without arrays. almost. if you count that string literal as array then fuck you

#include
#include

int fizz(int i){
printf("%d\n\0fizz\n\0fizzbuzz\n"+!(i%3)*4+!(i%5)*14-!(i%15)*8, i);
(fizz+((int(*)(int))exit-fizz)*(i/100))((i+1)%101);
}

int main(void){
fizz(1);
}