Getting a Prime

Project Euler has quickly become my hobby for when I have small moments of free time.  Here is problem #7:

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10001st prime number?

and this is my brute force solution:

Public Sub GetPrime()
    Dim iPrimeMax As Integer = 10001
    Dim iPrimeCounter As Integer = 0
    Dim iCurrentNum As Integer = 2
    While (iPrimeCounter < iPrimeMax)
        Dim bPrime As Boolean = True
        Dim iCounter As Integer = 1
        While (bPrime = True And iCounter < iCurrentNum)
            If (iCurrentNum Mod iCounter = 0 And iCounter <> 1) Then
                bPrime = False
            End If
            iCounter += 1
        End While
        If (bPrime = True) Then
            iPrimeCounter += 1
        End If
        iCurrentNum += 1
    End While
    Response.Write((iCurrentNum - 1).ToString())
End Sub

Project Euler #6

The problem:

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is

3025 – 385 = 2640

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

My solution:

Public Sub Difference()
    Dim iStart, iEnd, iSumOfSquares, iSquareOfSum As Integer
    iStart = 1
    iEnd = 100
    iSumOfSquares = 0
    iSquareOfSum = 0

    For iCounter As Integer = iStart To iEnd
        iSumOfSquares = iSumOfSquares + iCounter ^ 2
        iSquareOfSum = iSquareOfSum + iCounter

    Response.Write(iSquareOfSum ^ 2 - iSumOfSquares)
End Sub


Project Euler problem #4:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.

Find the largest palindrome made from the product of two 3-digit numbers.

My solution was a quick brute force attack done while I was eating lunch at work.

Public Sub Palindrome()
    Dim iMax As Integer = 999
    Dim iX, iY, iProduct, iStart, iEnd, iLargest As Integer
    iLargest = 0
    Dim bFound As Boolean = False
    For iX = 100 To iMax
        For iY = 100 To iMax
            iProduct = iX * iY
            Dim sProduct As String = iProduct.ToString()
            iStart = 0
            iEnd = sProduct.Length - 1
            Dim bStop As Boolean = False
            While (iStart + 1 <= iEnd And bStop = False)
                If (sProduct(iStart) <> sProduct(iEnd)) Then
                    bStop = True
                End If
                iStart = iStart + 1
                iEnd = iEnd - 1
            End While
            If (bStop = False) Then
                If (iProduct > iLargest) Then
                    iLargest = iProduct
                End If
            End If
End Sub

On TV: Fringe


So when I heard that J.J. Abrams was coming out with a new show that was supposed to be like the X-Files, I of course was thrilled and couldn’t wait.  I am in the habit of obsessing over the shows that I like and have often “inhaled” whole series extremely fast (eg. all 7 seasons of Buffy in about 2 months).

I was first introduced to J.J. Abrams’ work with the show Alias.  I was an avid fan until I felt that the Season 3 finale completely destroyed everything the show had built up and trivialized months of my life.  I subsequently stopped watching the show.  After Alias was LOST.  I decided to give Abrams a second chance to redeem himself and he managed to get back in my good graces with a show that developed a complex web of relationships among its cast members along with an incredibly detailed universe that managed to feed its ever growing fan base.

And now comes Fringe, a show that is like X-Files mixed with Alias.  The show centers around 3 main characters.  Anna Torv plays Olivia Dunham, an agent of the DHS who is assigned to investigate cases that exhibit extraordinary conditions similar to “x-files”.  John Noble plays Dr. Walter Bishop, an eccentric scientist who specialized in “fringe” science before being locked up in a mental institution for decades.  Joshua Jackson plays Peter Bishop, Walter’s estranged son who is called upon to be his father’s guardian while Walter helps the DHS.  I feel like the best interactions in the show are between the father and son Bishops.  They have a certain on-screen synergy which complements each other perfectly.  Sometimes the dialogue can be a little weak, but honestly if you want dialogue go watch a Joss Whedon show.  The role of Olivia Dunham seems weakly thought out at best.  I think they need to re-examine her role in the show and decide who the real focus of the show is.

Fringe does have a main story arc currently (in Season 1) which has captured my interest thus far.  While it may not be as epic as the X-Files main story arc, maybe this one will actually have some answers for us.  I think the secret behind the X-Files cult popularity was more than just the paranormal activity that the writers used to drive the plot.  It was the dynamic between Mulder and Scully.  They had an onscreen chemistry whose ebb and tide you could feel throughout the episodes through the television (or computer screen in my case).  Until Fringe can bring Agent Dunham’s character into a more cohesive role in the show, I think it’s pretty much doomed to low ratings and a “fringe” audience comprised of people like me who are just cult fans of sci-fi shows.  Give it a shot though and let me know what you think.

My first hockey game


Tonight I went to my first hockey game with a friend’s dad and had a great time!  The action was intense and the game was easy to pick up for a hockey noob like me.  It was the Washington Capitals vs the New York Islanders.  The Caps have lost the last four games to the Islanders.

The Caps started off strong and scored twice in the first period, but the Islanders came back and tied it up during the 2nd.  In the end, the Caps played a better game and won decisively 5-2.  My friend’s dad had front row seats so we were right up next to everything which made the experience that much more fantastic.

After the Caps scored the first time, I got to take part in the glorious act of banging on the glass like a madman along with everyone else in the front row.  The fans’ enthusiasm amazed me as the game progressed.  You could feel the anticipation of the audience every time the Caps had the puck near the Islanders’ goal.  And every time a penalty was called on a Cap, the crowd seemed like they were going to jump onto the ice and beat up the offending referee.  All in all, I had an awesome time and think that I’ll probably be going to more hockey games in the future.  I will most likely not be sitting in the front row in the future, but hopefully it is still just as exciting hehe.

Project Euler is awesome is one of my new favorite addictions.  i’ll be periodically posting my solutions to the problems on this site to keep track of my algorithms.  this is problem #2:

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
and this is my brute force solution done in

protected void Page_Load(object sender, EventArgs e)
    long prevFib = 0;
    long currFib = 1;
    long sum = 0;
    while (currFib < 4000000)
        if (currFib % 2 == 0)
            sum += currFib;
        currFib += prevFib;
        prevFib = currFib - prevFib;

this is problem #3:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?
and this is my solution which started off brute force but reduces the target number every time it finds a new prime factor causing a drastic reduction in processing time:

protected void Page_Load(object sender, EventArgs e)
    long target = 600851475143;
    for (long counter = 2; counter < target; counter++)
        // is counter a factor?
        if (target % counter == 0)
            // find out if counter is prime
            bool print = true;
            for (long i = 2; i < counter; i++)
                // if counter isn't prime,
                // cut out of for-loop and don't print
                if (counter % i == 0)
                    i = counter;
                    print = false;
            if (print)
                Response.Write(counter.ToString() + "<br />");
                // counter is a prime factor.  let's divide target
                // by counter to get a smaller target to work with.
                target = target / counter;