Last active
March 28, 2025 20:41
-
-
Save einarwh/f02636bf4519c0588f6c932ab602a390 to your computer and use it in GitHub Desktop.
FizzBuzz using IEnumerable
This file contains hidden or 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> | |
{ | |
public IEnumerator<string> GetEnumerator() | |
{ | |
int n = 0; | |
while (true) | |
{ | |
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"; | |
n += 15; | |
} | |
} | |
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