Created
September 5, 2011 01:01
-
-
Save banksJeremy/1193816 to your computer and use it in GitHub Desktop.
playing with linq as I first try C#
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
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