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
9/6/2010 7:13:41 AM #
You will find surely a fantastic deal of details like that to take into consideration. Which is a fantastic point to provide up. I present the thoughts above as general inspiration but clearly you will find questions such as the one you bring up where the most critical factor will be working in truthful good faith. I don?t know if greatest practices have emerged around points like that, but I'm certain that your job is obviously identified being a fair game.
9/26/2010 7:14:52 PM #
Thanks for the great scripts. It's really helpful for as a newbie programmer.
9/28/2010 8:05:18 AM #
Honestly, I don't know what this code all about. Can you enlighten me?
9/29/2010 8:50:22 AM #
These helped me out quite alot, excellent! Many thanks..
10/12/2010 11:02:00 AM #
great post thanks for posting
10/22/2010 12:13:34 PM #
Your blog is very useful. Thank you so much for providing plenty of useful content. I have bookmark your blog site and will be without doubt coming back. Once again, I appreciate all your work and also providing a lot vital tricks for your readers.
10/27/2010 12:28:09 PM #
Its good things, keep share
10/30/2010 6:41:35 AM #
Well done, I can't seem to understand why would you removed the "if" if it's a part of this sequence?
11/2/2010 7:52:04 AM #
I must say that overall I am really impressed with this blog.It is easy to see that you are passionate about your writing. If only I had your writing ability I look forward to more updates and will be returning
11/2/2010 7:54:48 AM #
This post was very well written, and it also contains many useful facts. I appreciated your professional way of writing the post. Thanks, you have made it easy for me to understand.
11/2/2010 7:57:53 AM #
After reading this I thought it was very informative. I appreciate you taking the time to put this blog piece together.
11/2/2010 8:00:43 AM #
I harmonize with your conclusions and will thirstily look forward to your approaching updates.
11/20/2010 1:49:44 AM #
Hello People! Your blog is awesome! I would like to visit it everyday. It's very informative and amazing
11/29/2010 10:49:28 AM #
I wonder how you got so good.  This is really a fascinating blog, lots of stuff that I can get into.  One thing I just want to say is that your Blog is so perfect!
12/5/2010 6:28:05 PM #
Facinating stuff. I loved it.
12/6/2010 8:03:29 AM #
nice post great blog thanks
12/6/2010 8:37:07 AM #
Ha!  I love climbing up the local Bay Area hills and passing people with headphones.  I'll say "Hi" as I pass them and it scares the crap out of them because they didn't hear me rolling up behind them.*-*
12/6/2010 10:29:20 AM #
dispenses use a terrific web site decent Offers cheers for the exhausting work to assist folks*-*
12/6/2010 11:10:03 AM #
Your article very good, I am very interested in your story! *_*
12/7/2010 12:08:19 AM #
I printed this article for a college Essay I am doing right now, thanks!
12/7/2010 3:08:19 AM #
Your article very good, I am very interested in your story! *_*
12/7/2010 9:03:21 AM #
Wow interested video, I like the potential of DGML, gonna have to research this further!!!
12/9/2010 4:12:17 AM #
The web log really is wonderful.  I like reading through it but the truth is, the writing appears to be kinda odd when using the safari website broswer
12/9/2010 4:59:32 AM #
Your article very good, I am very interested in your story! oooh this is exciting! I am going to submit... Smile
12/9/2010 11:14:10 AM #
Yes social media may have successes in recruitment, but i cant believe it will be the sole tool used to advertise a placement.
12/9/2010 5:59:14 PM #
Shop http://www.medexsupply.com/WelchAllyn-x_mid-53.htm for all of your Welch Allyn products.
12/10/2010 9:56:47 AM #
Hey, do you have a writer for this post or do you write it by yourself? It kinda interesting and I like the way you write.
12/22/2010 1:45:25 AM #
thanks for the code help, this stuffs above my head
12/22/2010 9:15:38 AM #
Hey great stuff, thank you for sharing this useful information and i will let know my friends as well.
12/22/2010 9:10:15 PM #
12/27/2010 10:45:28 AM #
This is the good step u have taken. Thanks for writing this, it’s clear that you have spent a good amount of time on your sites development
12/28/2010 8:39:42 AM #
Wow nice information you have shared here.  Actually Google made searching of information easy on any topic. Well keep it up and post more interesting blogs
12/28/2010 8:43:32 AM #
Why didn’t I find this post earlier? Keep up the good work!
12/30/2010 1:43:09 AM #

Easily, the article is in reality the sweetest on this deserving topic.
I fit in with your conclusions and will eagerly look forward to your future updates.
Just saying thanks will not just be adequate, for the great lucidity in your writing.
I will directly grab your rss feed to stay abreast of any updates
12/31/2010 11:01:50 AM #
Great work dude, u gave nice post to us. Thanks for spending the time to discuss this, I feel strongly about it and love learning more on this topic.
1/2/2011 4:31:54 AM #
Great post, thanks for taking the time to write it. I like the direction you are taking your blog. I'll be bookmarking your blog so I Can keep up in the future. Looking forward to more posts soon.
1/3/2011 4:04:47 PM #
Its'a nice blog for a newbie regarding mastering linq challenge !
1/6/2011 7:04:00 AM #
thanks for the valuable article. Good info. I'll make sure to comeback soon.
1/7/2011 1:16:56 PM #
I just couldn’t leave your website before saying that I really enjoyed the quality information you offer to your visitors... Will be back often to check up on new stuff you post!
1/11/2011 7:43:36 PM #
Thank you for your very useful information. I was no longer a strict follower.
1/13/2011 8:26:08 AM #
Excellent stuff from you, man. I’ve read your things before and you are just too awesome. I adore what you have got right here. You make it entertaining and you still manage to keep it smart.
This is truly a great blog thanks for sharing…
1/14/2011 9:23:33 AM #
Nice blog, This is very detailed and informative article.
1/15/2011 2:48:49 PM #
Thanks for the info. Good article.
2/28/2011 7:17:10 AM #
It's a Very informative article , all the information are given very clearly.I really appreciated with it, I will bookmark this site.Thanks for sharing.
2/28/2011 7:20:20 AM #
i just wanna thank you for sharing your information and your site or blog this is simple but nice article I've ever seen like it i learn something today...
3/19/2011 3:35:48 AM #
If your picking a druid what is better? horde or alliences?
3/22/2011 6:09:24 PM #
Great researched information you've got here! Ill mention your site in my blog Smile
3/22/2011 6:15:47 PM #
This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. Youve got a design here thats not too flashy, but makes a statement as big as what youre saying. Great job, indeed.
3/22/2011 7:23:11 PM #
What a great opportunity to finally read something worth reading.  thanks a lot and keep posting articles like this for us to read.
3/22/2011 7:56:12 PM #
This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the good work.
3/22/2011 8:27:29 PM #
I have been searching all over the internet for something to give me the correct advice.  This is not exactly what I was looking for but it helped a lot.  Thanks
3/23/2011 3:06:16 AM #
I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful
3/23/2011 3:59:16 AM #
I don't know why...
3/23/2011 3:14:59 PM #
thanks !!  very helpful post!
Comments are closed