Justin Etheredge’s LINQ Challenge

by Nate on January 8, 2010

Justin Etheredge just wrapped up a series of videos on LINQ for TekPub, and when announcing it, he offered a challenge with the following rules:

  1. You have to blog about a single LINQ query which starts with Enumerable.Range(1,n) and produces a list of prime numbers from the range.
  2. You can’t cheat. This is determined by me, and includes hardcoding values in the results. You’ll know if you cheated.
  3. Uses no custom LINQ methods.
  4. Will return all of the prime numbers of the sequence. It doesn’t have to be super optimal, but it has to be correct.
  5. Be one of the first 5 people to blog a correct answer and then tweet this "I just solved the @tekpub LINQ challenge: <link to post>" will get any single TekPub screencast. The time of your solution will be based on your tweet! So be prompt!
  6. You must link to both TekPub’s website and this post in your blog post.

I can’t resist a good brainteaser, so here’s my answer. I’m sure it’s not the most efficient way of solving the problem, but it works:

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

But, I’ve already got a subscription to TekPub, so I don’t need the prize! Instead, I’m going to give it away to the first person who comments on this post. If you haven’t seen TekPub yet, you should check it out! It’s a great resource for programmers of all levels of skill.

Share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • FriendFeed
  • HackerNews
  • Posterous
  • Reddit
  • Twitter

Related posts:

  1. Great IoC Presentation by Justin Etheredge

{ 8 comments… read them below or add one }

Rene January 8, 2010 at 8:38 pm

i want the suscription =D

Nate January 8, 2010 at 8:40 pm

@Rene: It’s yours. Send me an email (nate@enkari.com) with your info and I’ll pass it along to Justin. :)

Yex January 8, 2010 at 8:41 pm

Thanks for the post. I’d love a subscription to TekPub. I figured the other guy didn’t leave an email address, so maybe I still had a shot, eh? ; )

Conker January 12, 2010 at 6:48 am

More compact and faster variant :

int max = …
var primes = Enumerable.Range(2, max-1).Where(x => !Enumerable.Range(2, (int)Math.Sqrt(x)-1).Any(y =>x % y == 0));

Nate January 12, 2010 at 8:47 am

@Conker:

The challenge said the beginning of the statement had to start with Enumerable.Range(1, max) … otherwise I’d agree. :)

Conker January 12, 2010 at 10:37 am

Hello Nate, not trying to take a credit, its a cleaned up version of your query :)
To be compatible with chanllenge, the first check x != 1 can be left as it is, it doesnt affect speed as much as second check x!=y, which is completely unnecessary.
Challenge aside, i think this \cleaned\ version is as very elegant , simple and truly shows the power of LINQ.

M@ January 13, 2010 at 3:30 pm

this is going to seem like a moronic question, but why create a programming language around sql? I know NOTHING about LINQ so you can feel free to tell me to RTFM :)

I’m working in a company that is about to start in on a lot of BI stuff, so I’m curious to learn more about LINQ.

Nate January 18, 2010 at 8:43 am

M@: LINQ (at least LINQ to Objects, which is what we’re talking about here) doesn’t really have anything to do with databases. The syntax is reminiscent of SQL, but it’s just intended to operate on collections of objects in memory.

Leave a Comment

Previous post: Guarantees, SLAs, and Hollow Promises

Next post: Ninject Forever