Archive for March, 2010

Hudson is a Crawler

This past Friday I stayed home to take Hudson to the doctor and he rewarded me with this show:

I don't think it's possible to overstate just how proud I am of him. Every single day I can see him growing and developing and it's an amazing sight. We're 7 months into this and I still have no clue what the heck I am doing. The only thing I do know is that we're moving forward.

Sieve of Eratosthenes in PL/SQL

Yesterday I posted an Oracle SQL query that would produce primes, but it wasn't an implementation of the Sieve of Eratosthenes. Here for your viewing pleasure discomfort is a version in Oracle PL/SQL.

create or replace package josh.sieve as
   type prime_numbers is table of number;
   function calculate ( the_end number ) return prime_numbers pipelined;
end;
/

create or replace package body josh.sieve as
   function calculate ( the_end number ) return prime_numbers pipelined is
      type bitbucket is table of boolean index by binary_integer;
      primes bitbucket;
      max_sqrt number :=floor(sqrt(the_end));
      i number:=3;
      j number:=1;
    begin
        pipe row(2);

        for i IN 1 .. the_end loop
          primes(i) := true;
        end loop;

        while i <= the_end loop
          if primes(i) then
            if(i<=max_sqrt) then
              j:=i*i;
              while j<=the_end loop
                primes(j):=false;
                j:=j+2*i;
              end loop;
            end if;

            pipe row(i);
          end if;

          i:=i+2;
        end loop;
    end;
end;
/

This is the ugliest one so far. It really burns my eyes!

What I liked
I was kind of pleased to see the pipe row(foo); functionality. This is very similar to yield in c# where I can just lazily return numbers as they are found instead of building up the whole list table in advance.

What I hated
There's a bunch here. First off, I tried to write this with a for loop, but there is no way to define the step value. I had to resort to using a while loop with an external counter variable. It also sucks that I have to define a custom type which is just a table of numbers. Finally, I didn't see a clean way to initialize a table with a size and default value.

Why it burns my eyes
I'm not digging that assignments have to have a colon-equal "foo := 42" operator. To the same tune as basic, I hate that blocks end with an end keyword. This is an especially big WTF when you consider that lines are terminated with semi-colons. The end result is a bunch of colon-ish punctuation littered with end keywords.

Regardless, I was pleased with the performance. Running a query like select count(1) from table(josh.sieve.calculate(1000000)); runs in about 600 milliseconds on our db machine. Not too shabby...

Prime Numbers as a SQL Query

This isn't a Sieve of Eratosthenes algorithm, but it's too good not to share it.

select r
from (
  select rownum+1 r from dual connect by rownum < 10000
)
where
  r=2
  or 0 not in(
    select mod(r,rownum+1) from dual
    connect by rownum<sqrt(r)
  )

This is an Oracle query that will generate all of the primes up to 10,000. The query is not even close to fast, but I think it's kind of funny. It's just your typical everyday brute force method of calculating prime numbers.

Sieve of Eratosthenes in F#

Here is the third(hopefully of many) implementations of the Sieve of Eratosthenes. F# has been making a lot of noise lately, so let's see what's up.

let findPrimes max=
    seq {
        yield 2
        let maxSquareRoot:int = int (sqrt (float max))
        let primes = Array.create (max+1) true
        for n in 3 .. 2 .. max do
            if primes.[n] then
                if n<=maxSquareRoot  then
                    for i in n*n..2*n..max do primes.[i] <- false
                yield n
    }

I struggled a bit with F#. Functional languages have a different look and feel for sure. The hardest part of this was that I had several ways to implement this. I'm still not sure I chose the most optimal way since this is the slowest one so far. My first stab used lists like so:

let getPrimes max =
    let maxSquareRoot:int = int (sqrt (float max))
    let primes = Array.create (max+1) true    

    2::[3 .. 2 .. max]
    |>List.filter(fun n->
        if primes.[n] && n<=maxSquareRoot  then
            for i in n*n..2*n..max do primes.[i] <- false
        primes.[n]
    )

This version was slower than what I ultimately wrote up top using sequences, but I really like the pipe syntax. I think both versions are very compact and easy to follow. I'm a little bummed that both versions are slower than my C# version. If anyone can help me write a better(faster) version, I'd love the help. I feel like I'm not seeing the full potential of the language and I'm missing something huge.

Sieve of Eratosthenes in VB

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.