Skip to content

Justin Etheredge’s LINQ Challenge

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.

Advertisements

From → Uncategorized

9 Comments
  1. Rene permalink

    i want the suscription =D

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

  3. 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? ; )

  4. Conker permalink

    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));

  5. @Conker:

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

  6. Conker permalink

    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.

  7. 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.

  8. 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.

Trackbacks & Pingbacks

  1. The TekPub LINQ Challenge Part 2 – Faster Algorithms | CodeThinked

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: