LINQ Cookbook, Recipe 3: Find all the prime numbers in a given range (Jonathan Aneja)

Published 28 June 07 01:21 PM

Ingredients:

-          Visual Studio 2008 (Beta2 or Higher)

 

Categories: LINQ-To-Objects 

 

Instructions:

-          Open Visual Studio 2008, and Click ‘File/New Project’. Find and double-click the ‘Console Application’ Icon

-         Add the following code: 

     

Module Module1

 

    Sub Main()

        Dim primes = GetPrimesInRange(30, 50)

 

        For Each n In primes

            Console.WriteLine(n)

        Next

        Console.ReadLine()

    End Sub

 

    Function GetPrimesInRange(ByVal low, ByVal high) As IEnumerable(Of Integer)

        Dim range = Enumerable.Range(1, high)

        Return From num In range _

               From num2 In range _

               Where num Mod num2 = 0 _

               Group By num Into Count() _

               Where Count = 2 And num > low _

               Select num

    End Function

 

End Module

-          The grouping is used to select only those numbers that have exactly two factors (i.e. they're prime)

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Bill_McC said on June 28, 2007 10:00 PM:

Doesn't that only work if the low value is 0 or 1 ?

And isn't this a really innefficient way of doing this as there is no early exit for a given value once the count  > 2.

BTW: I haven't got Beta 2 (what up with that ?) but

the code:

      Group By num Into Count() _

      Where Count = 2 _

looks strange to me. Why does Count have () after it, yet when you refer to it on the next line it is as a single value not an array ?

# Aaron said on June 29, 2007 8:30 AM:

I have to agree with the first comment. I have a general feeling about LINQ that it basically looks impossible to debug and is not intuitive. I have started to play with it in beta 1 and I have tried ScottGu's LINQ to SQL examples and every time I work with them I feel like they are useless in an enterprise application. For simple applications, they seem useful for querying simple collections. But in every example that I see an anonymous method and LINQ in I just fell yucky when I imagine trying to debug someone else's I-Used-A-Cool-Feature code.

Cheers,

Aaron

# VBTeam said on June 29, 2007 4:29 PM:

Thanks Bill, I've updated the sample so it works for any given range now.  This isn't really intended to be a textbook example of calculating primes, so I'll happily concede that it's grossly inefficient :)

As far as the Count thing goes...there's parentheses on the first one because we're calling the Count() function, but we don't need it on the second one because that's a property.  Since we didn't explicitly name our Group, the compiler inferred it's name to be Count.  If you want to name the group you could do this instead:

               Group By num Into Factors = Count() _

               Where Factors = 2 And num > low _

Thanks!

Jonathan

# Public Sector Developer Weblog said on July 3, 2007 8:25 AM:

The VB team just started a LINQ Cookbook series. These are great because they show you how you can easily

# ulu said on July 5, 2007 1:40 PM:

I agree that this example takes some time to understand. I believe LINQ is great when you just need a simple query. If you need something debuggable, you could do the same with extension methods from the System.Query.Sequence class and some explicitly defined lambdas. 3-4 times more code though.

It is great, however, that LINQ can do such things.

# zack said on January 8, 2008 5:26 AM:

引用:LINQ菜谱,菜单3:在给定的范围内找到所有的素数(JonathanAneja) [原文作者]:JonathanAneja

[原文链接]:LINQCookbook,Reci...

Leave a Comment

(required) 
(optional)
(required) 

This Blog

Syndication

Page view tracker