Complete these tasks to reinforce what you learned in this module.
Write an infinite Fibonacci iterator starting 0, 1, 1, 2, 3 — then print the first ten values on one line, separated by single spaces.
while (true) with yield return a;, then shuffle a and b forward. Main already has the Take(10) and the join.using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static IEnumerable<long> Fibonacci()
{
// Never-ending: 0, 1, 1, 2, 3, 5, ...
yield break;
}
static void Main()
{
Console.WriteLine(string.Join(" ", Fibonacci().Take(10)));
}
}
From an infinite sequence of natural numbers, find the first multiple of 7 whose square is greater than 1000, and print that square.
Naturals().Where(n => n % 7 == 0).Select(n => n * n).First(n => n > 1000).using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static IEnumerable<int> Naturals()
{
for (int i = 1; ; i++)
{
yield return i;
}
}
static void Main()
{
// Squares of multiples of 7 — print the first one over 1000
}
}
Find the first natural number divisible by 13, then print how many values the infinite sequence produced in total. Have the iterator count each value it hands out.
yield return. First stops asking as soon as its condition holds.using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static int _produced;
static IEnumerable<int> Naturals()
{
for (int i = 1; ; i++)
{
// Count this value, then hand it back
yield return i;
}
}
static void Main()
{
var first = Naturals().First(n => n % 13 == 0);
Console.WriteLine(first);
Console.WriteLine(_produced);
}
}