Created
March 28, 2025 20:49
-
-
Save einarwh/fbee2ce2b15c27ab69292281844c0b95 to your computer and use it in GitHub Desktop.
FizzBuzz in C# without conditionals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Fizzbuzz; | |
using System.Collections; | |
using System.Collections.Generic; | |
class Program | |
{ | |
public class FizzBuzzEnumerable : IEnumerable<string> | |
{ | |
private readonly int n; | |
public FizzBuzzEnumerable() : this(0) {} | |
private FizzBuzzEnumerable(int offset) | |
{ | |
n = offset; | |
} | |
public IEnumerator<string> GetEnumerator() | |
{ | |
yield return (n + 1).ToString(); | |
yield return (n + 2).ToString(); | |
yield return "Fizz"; | |
yield return (n + 4).ToString(); | |
yield return "Buzz"; | |
yield return "Fizz"; | |
yield return (n + 7).ToString(); | |
yield return (n + 8).ToString(); | |
yield return "Fizz"; | |
yield return "Buzz"; | |
yield return (n + 11).ToString(); | |
yield return "Fizz"; | |
yield return (n + 13).ToString(); | |
yield return (n + 14).ToString(); | |
yield return "Fizz Buzz"; | |
foreach (var s in new FizzBuzzEnumerable(n + 15)) | |
{ | |
yield return s; | |
} | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
foreach (var s in new FizzBuzzEnumerable().Take(int.Parse(args[0]))) | |
{ | |
Console.WriteLine(s); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment