Skip to content

Instantly share code, notes, and snippets.

@banksJeremy
Created September 5, 2011 01:01
Show Gist options
  • Save banksJeremy/1193816 to your computer and use it in GitHub Desktop.
Save banksJeremy/1193816 to your computer and use it in GitHub Desktop.
playing with linq as I first try C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace mtf
{
class Squares : IEnumerable
{
private System.Collections.Generic.List<BigInteger> values;
public Squares (BigInteger limit)
{
values = BigIntegers().Select (x => x * x).TakeWhile (x => (x <= limit)).ToList ();
}
public System.Collections.IEnumerator GetEnumerator ()
{
return values.GetEnumerator ();
}
private IEnumerable<BigInteger> BigIntegers ()
{
BigInteger value = 0;
while (true) {
yield return value++;
}
}
}
class MainClass
{
public static void Main (string[] args)
{
var limit = 345;
Console.WriteLine ("Printing all squares up to " + limit.ToString () + ":");
foreach (var n in new Squares (limit)) {
Console.WriteLine (n);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment