# [Challenge] The Classic FizzBuzz Challenge

Java solution.

``````public class Main{

public static void main(String []args){
for(int i=1; i<=100; i++){
String s="";
if(i%3==0){
s+="Fizz";
}
if(i%5==0){
s+="Buzz";
}
System.out.println((s.length()>1)?s:i);
}

}
}
``````
1 Like

This is my code in python 2:

``````for i in range(1,101):

if (i % 3 == 0) and (i % 5 == 0):
print "\t\t\tFizzbuzz"

elif i % 5 == 0:
print "\t\tBuzz"

elif i%3 == 0:
print "\tFizz"

else:
print i
``````

and it shows fizz, buzz, and fizzbuzz in separate columns

1 Like

Remembering back from my assembler days that division is a CPU hog, I decided to cache the remainders first:

``````for i in range(1,101):
d3 = i % 3
d5 = i % 5
if d3 and d5: num = i
elif d5: num = "Fizz"
elif d3: num = "Buzz"
else: num = "FizzBuzz"
print(num)
``````

This way for every (n, …, n+14) segment it performs only 30 divisions compared to 39 in “mod15-mod3-mod5” approach. Also tried to save on amount of boolean tests by going from the most frequently satisfied case to the rarest.

I’ve used the num variable rather than print immediately so I could comment out the last line and run this code 100,000 times in a time-measuring loop. Turns out to run about 10% faster than the version that divides within every test, and comparably fast to far more elegant (and still faster) datfatcat’s solution. The “track responses” solution by micromaster15787 is certainly the quickest for this challenge, but I imagined such code might need to handle other computation for test that doesn’t have a repeating pattern (eg. involving primes).

3 Likes

I see my code in JavaScript is similar as other replays…

``````function FizzBuzz(){
for(i=1; i<101; i++){
switch(0){
case i%15:
console.log("FizzBuzz");
break;
case i%3:
console.log("Fizz");
break;
case i%5:
console.log("Buzz");
break;
default:
console.log(i);
}
}
}
``````
3 Likes
``````for i in range(1,101): #Numbers from 1-100
if i%3==0 and i%5==0: #If they are divisible by 3 and 5...
print "FizzBuzz"
elif i%3==0: #Else if they are divisible by 3...
print "Fizz"
elif i%5==0: #Else if they are divisible by 5...
print "Buzz"
else: #Else if none of these apply print the number
print(i)

``````

To me this seems the most logical way but I am a reasonably inexperienced programmer so any advice is appreciated.

2 Likes

My solution to the FizzBuzz Challenge…

``````class FizzBuzz{
public static void main(String[]args){
for(int i=1; i<=100; i++){
if(i%3==0 && i%5!=0){
System.out.println("Fizz");
}
else if(i%3!=0 && i%5==0){
System.out.println("Buzz");
}
else if(i%3==0 && i%5==0){
System.out.println("FizzBuzz");
}
else{
System.out.println(i);
}
}
}
}
``````
3 Likes

Here’s my solution in Ruby

``````def fizzbuzz
for i in 1..100
if i%3 == 0 && i%5 == 0
puts "FizzBuzz"
elsif i%3 == 0
puts "Fizz"
elsif i%5 == 0
puts "Buzz"
else
puts i
end
end
end
``````
2 Likes

Very similar to mine in python, except my range was (1,101) and i didn’t do the columns. Also I’m not sure how you all are displaying your code, perhaps as an image?

1 Like

sorry, I couldn’t find your code.
you are true about range (1, 101), i should have done that too. my bad!

Also I’m not sure how you all are displaying your code, perhaps as an image?

If I’m not mistaken, you are asking how I run it? I saved it in a file and call it with command `python filename.py` in terminal
or you are asking how I show it here? I copy paste it here and put there ``` around it.
( ` or backtick is the key above tab key )

If i’m wrong, my bad, English is my third language!
Thanks, mori.

1 Like

No, I meant y’all are displaying your code in a grey box here. Seems it is triple backticks - lemme try:

``````#FizzBuzz Challenge April 21, 2017

for i in range(1,101):
r = i % 3
s = i % 5
if (r == 0 and s == 0):
print("FizzBuzz")
elif (r == 0 and s > 0):
print("Fizz")
elif (r > 0 and s == 0):
print("Buzz")
elif (r > 0 and s > 0):
print i
``````
1 Like

My solution in Swift:

``````
for i in 1...100 {
if i % 3 == 0 && i % 5 == 0{
print("FizzBuzz")
}
else if i % 3 == 0 {
print("Fizz")
}else if i % 5 == 0{
print("Buzz")
}else{
print(i)
}
}

``````
2 Likes

`Array.from({length:100})`
Nice use of Array.from’s iterator param.
Also didn’t realise having an object containing a length property constituted ‘array-like’

1 Like

Here is my python code:

Print the number.

First check if it is divisible by 3 and 5, print “fizzbuzz” if it does

Otherwise check if divisible by 3, print “fizz”

Otherwise check if divisible by 5, print “buzz”

Seems simple enough. However, when I run it, the “fizz”, “buzz” or “fizzbuzz” appear on the line below the number whereas I’d like it to appear next to the number. Not sure how to do this without making the code far more complex

``````for i in range(1, 101):
print i
if (i%3 == 0) and (i%5 == 0):
print "fizzbuzz"

elif (i%3 == 0):
print "fizz"

elif (i%5 == 0):
print "buzz"``````
1 Like

C++. Similar to others. Call to fizzbuzz function. Nothing fancy.

``````#include <iostream>

void fizzbuzz(){
for (int i=1; i <= 100; i++){
if (i%3 == 0 && i%5 == 0)
std::cout << "fizzbuzz" << "\n";
else if (i%3 == 0)
std::cout << "fizz" << "\n";
else if (i%5 == 0)
std::cout << "buzz" << "\n";
else
std::cout << i << "\n";
}
}
int main () {
fizzbuzz();
return 0;
}
``````
2 Likes

Better than mine, except for some sloppy indents and mismatched quotes.
Better because simpler, and I understand loop exitting better now, so thanks. I thought yours would print multiple outputs for one input, but it doesn’t. It exists the loop after returning any of the outputs.

1 Like

A generalised version of the game in Python 3. The rules are passed in as a list of (number, word) pairs, for example [(3, ‘fizz’), (5, ‘buzz’)] for the basic FizzBuzz game.

``````def play_game(limit, rules):
"""
play a specific variant of the FizzBuzz game up to the given limit
the rules parameter is a list of (number, word) tuples
e.g. [(3, 'fizz'), (5, 'buzz')]
"""
for x in range(1, limit+1):
# select the words where the number is a factor of x
words = [w for (n,w) in rules if x%n==0]
# join the words together if there any otherwise answer with the number
answer = "".join(words) if len(words)>0 else str(x)

# play FizzBuzz
rules = [(3, 'fizz'), (5, 'buzz')]
play_game(100, rules)

#play FizzBuzzWoof
rules = [(3, 'fizz'), (5, 'buzz'), (7, 'woof')]
play_game(100, rules)
``````
2 Likes

If you want to print something without a newline, you can add a comma in Python 2:

`print i,`

but you will also need to add an else clause to the if statement so that the next answer is on a new line.

``````else:
print
``````
1 Like

Thanks. I actually misread the challenge because it asks for “fizz” to be printed instead of the number; I was aiming to have it printed next to the number. It made for an interesting variation anyway

2 Likes

VERY elegant solution!!!

2 Likes

Boring, but it works

``````for x in range(1,101):
if x % 3 == 0:
if x % 5 == 0:
print('Fizzbuzz')
else:
print("Fizz")
elif x % 5 == 0:
print('Buzz')
else:
print(x);
``````
2 Likes