Here is the second (hopefully of many) implementations of the Sieve of Eratosthenes. For this one I wanted to step outside of my comfort zone and do a language which I really don't care that much for.
Function FindPrimes(ByVal max As Integer) As IList(Of Integer)
Dim vals = New List(Of Integer)(max / (Math.Log(max) - 1.08366))
Dim maxSquareRoot = Math.Sqrt(max)
Dim eliminated As New BitArray(max + 1)
vals.Add(2)
For i = 3 To max Step 2
If (Not eliminated(i)) Then
If (i < maxSquareRoot) Then
For j = i * i To max Step 2 * i
eliminated(j) = True
Next
End If
vals.Add(i)
End If
Next
Return vals
End Function
I won't bother explaining the algorithm or my optimizations again. What I want to talk about is the language.
This one was easy for me; after all, it's the same .NET base libraries that I'm comfortable with. At first I wrote the code with a bunch of "As Integer" garbage everywhere. Once I realized I didn't need it, I went back and scrapped a bunch of the unnecessary code to create what you see above. It looks okay to me, but I still don't like some of the noise that VB requires.
- End Foo - This has to be my number on gripe about VB syntax. I REALLY hate the wordiness that is required to end a block of code. To my eye it destroys the balance of whitespace in the code and makes it harder to see code depth at a glance.
- Proper Case Keywords - This is a minor thing, but I really dislike the proper cased keywords. I'm used to seeing words start with a capital letter only if it's a class or public member. For me it's still a whitespace thing and these capital letters everywhere encroach on it big time.
It's not all hate from me though. I actually enjoyed the loop syntax (minus the "Next" statement at the end). The only reason I needed the Step statement was because I was skipping numbers. The syntax just felt natural.
In the end, it's not my favorite language, but it's not my least favorite. I would squarely place this in the "meh" category. I would write code in VB if I needed to work in a legacy environment, but for new .NET code I would still turn to C#.
As always, if you see something stupid that I did then please let me know and I'll update as necessary.