Skip to content

Instantly share code, notes, and snippets.

@oliverbooth
Created October 5, 2020 16:30
Show Gist options
  • Save oliverbooth/0e83a8e6ecc3c06720941035cc42ff86 to your computer and use it in GitHub Desktop.
Save oliverbooth/0e83a8e6ecc3c06720941035cc42ff86 to your computer and use it in GitHub Desktop.
public static void Main() {
FizzBuzz(100, new[]{("Fizz",3),("Buzz",5)});
}
public static void FizzBuzz(int c, (string,int)[] d) {
bool f(int n, int d)=>n%d==0;
for (int i=1;i<=c;i++) {
bool p=false;
for (int m=0;m<d.Length;m++) {
if(f(i,d[m].Item2)) {
Console.Write(d[m].Item1);
p=true;
}
}
if(!p) Console.Write(i);
Console.WriteLine();
}
}
@oliverbooth
Copy link
Author

Readable version:

public static void FizzBuzz(int count, (string Term, int Value)[] dividends)
{            
    bool IsMultiple(int quotient, int dividend) => quotient % dividend == 0;

    for (var number = 1; number <= count; number++) {
        bool printWord = false;

        foreach (var item in dividends)
        {
            if (IsMultiple(number, item.Value))
            {
                Console.Write(item.Term);
                printWord = true;
            }
        }

        if(!printWord)
        {
            Console.Write(number);
        }
        Console.WriteLine();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment