## Fizzbuzz

### March 23, 2021

I’ve seen several recent posts on the beginning-programmer forums about how to solve the fizzbuzz problem, so let’s talk about that today:

Enumerate the numbers from 1 to N by writing “Fizz” if the number is divisible by 3, “Buzz” if the number is divisible by 5, “FizzBuzz” if the number is divisible by both, and the number itself if the number is divisible by neither. For instance, counting from 1 to 25 works like this: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11. Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz.

FizzBuzz also appears as the first problem in Project Euler, where they characterize the problem like this:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

Your task is to solve the two versions of the FizzBuzz problem, for all numbers up to *N*; you can use a simple method, but it is more fun to be a little bit clever. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

There’s no reason to be clever, otherwise, you end up with something like: https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition ;-)

Here’s a solution in Python.

Output:

Here’s my attempt at a clever solution.

A solution in Racket:

Another solution in Racket:

(The first post was for 1 – 100, as requested by Project Euler; this post is for 1 to N.)

Example:

For the Sum from 1 to N in C#:

(3

(N/3)(N/3 + 1) + 5(N/5)(N/5 + 1) – 15(N/15)(N/15 + 1))/2my answer in c++

`#include`

using namespace std;

int main()

{

int setc = 0;

int N = 100;

for (int i = 1; i < N; i++)

{

setc = 0;

if (i % 3 == 0)

{

setc = 1;

cout << "Fizz";

}

if (i % 5 == 0)

cout << "Buzz";

`}`