Saturday, January 09, 2010

TekPub/CodeThinked Challenge

 

Justin Etheredge posted this challenge on CodeThinked so I thought I should give it a try though I’ve just had my TekPub subscription (Looking forward to use it, heavily!! :D)

So here’s my solution:

var myPrimes =
                Enumerable.Range(1, SomePositiveValue)
                    .Where(x => x != 1 &&
                        !Enumerable.Range(2, (int)Math.Ceiling(Math.Sqrt(x)))
                            .Any(y =>   x % y == 0 &&
                                        x != y));

You might want to remove the “x != 1” depending on you consider 1 as a prime or not :)

No comments:

Disclaimer

All the opinions expressed on this blog are my own and don't necessarily represent my employer's positions, strategies or opinions.