Home >

Tekpub’s Mastering Linq Challenge

9. January 2010

Update 6: When you think you’re done, think twice. The code should be valid even when the range contains some negative value

var primes =
    Enumerable.Range(-10, 34).Aggregate<int , IEnumerable<int>>(new int[] {},
                         (list, x) =>
                         x >1 && list.All(y => x%y != 0)
                             ? list.Union(new[] {x})
                             : list);

End update

Update 5: Long story short, Linq is not the type of thing you’d use when finding primes.

Update 4: Removing “if” in expense of reduced speed

Enumerable.Range(1, 34).Aggregate<int , IEnumerable><int>>(new int[] {},
                         (list, x) =>
                         x != 1 && list.All(y => x%y != 0)
                             ? list.Union(new[] {x})
                             : list);

End update

Update 3: Making the code a bit faster by dividing only primes found so far

var primes =
    Enumerable.Range(1, 34).Aggregate(new List<int>(), (list, x) =>
{
    if(x!=1&&list.All(y=>x%y!=0))
    {
        list.Add(x);
    }
    return list;
});

End update

Update 2: Using all, one can have one step cleaner code.

var primes =
        Enumerable.Range(1, 34).Where(x =>
                         x == 2 || (x != 1 &&
                                    Enumerable.Range(2, (int) (Math.Sqrt(x)))
                                                           .All(y=>x%y!=0)));

End update

Update: Using any, one can make a bit faster implementation of it

var primes =
                Enumerable.Range(1, 34).Where(x =>
                                              x == 2 || (x != 1 &&
                                                         !Enumerable.Range(2, (int) (Math.Sqrt(x)))
                                                             .Any(y=>x%y==0)));

End update

Here is my solution to TekPub’s Mastering Linq Challenge appeared on Justin Etheredge’s blog

            var primes =
                Enumerable.Range(1, 34).Where(x =>
                                              x == 2 || (x != 1 &&
                                                         Enumerable.Range(2, (int) (Math.Sqrt(x)))
                                                             .Where(y => (x%y) == 0).Count() == 0));
Not very efficient, but simple :)

Comments

1/9/2010 10:49:52 AM #
Great work!
1/9/2010 12:03:11 PM #
Pingback from topsy.com

Twitter Trackbacks for
        
        Tekpub’s Mastering Linq Challange - Tuna Toksoz's weblog
        [tunatoksoz.com]
        on Topsy.com
1/10/2010 5:38:45 PM #
Winners of the TekPub LINQ Challenge

Winners of the TekPub LINQ Challenge
8/16/2010 2:46:28 PM #
that's very good info
i think i will put some my own blog Laughing
cheers
http://zoodiscounts.net
http://tvdump.net
8/27/2010 11:14:00 AM #
that's very good info
i think i will put some my own blog
8/27/2010 11:14:43 AM #
yeah i think that too

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading