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 6^(th) prime is 13.

What is the 10001^(st) 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,

1^(2) + 2^(2) + … + 10^(2) = 385

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

(1 + 2 + … + 10)^(2) = 55^(2) = 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
    Next

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

palindrome

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
        Next
    Next
    Response.Write(iLargest.ToString())
End Sub

On TV: Fringe

fringe

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

hockey

hockey

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.

hockey

hockey

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

projecteuler.net 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 asp.net:

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;
	}
	Response.Write(sum.ToString());
}

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;
			}
		}
	}
	Response.Write(target.ToString());
}

The Julie Amero Debacle (or Why I’ll never go to Norwich, CT)

Imagine this.  You are a substitute teacher and are surfing the web on the classroom computer only to find a porn popup come up on the monitor.  You try and go to the teachers’ lounge to get help but when you leave the classroom, students see the porn on the computer.  When public officials find out about this, you are charged with a felony of impairing the morals of a child.  After testimony by some idiot of a cyber crime detective named Mark Lounsbury (complete douchebag), a jury convicts you of a crime that is punishable by up to 40 years!

Sounds incredulous, right?  This couldn’t happen in our wonderfully technological society filled with intelligent people who make decisions based on rational thought, right?  WRONG.  It did happen.  It happened in some backwards ass town called Norwich, Connecticut.  The incident happened in 2004.  And the teacher, Julie Amero, was convicted in January 2007.  That’s just last year!  LAST FUCKING YEAR!!!  How does this kind of stuff happen?

Luckily, some intelligent people heard about this case and came to Miss Amero’s rescue.  There was even an ad put in the Hartford Courant signed by 28 computer science professors that all agree that Julie Amero couldn’t control the porn popups.  This case dragged on until just a few days ago when Miss Amero finally agreed to a plea bargain where she plead guilty to disorderly conduct and paid a $100 fine.  Unfortunately she has paid a much larger price in that she has lost her teaching credentials and has had her reputation completely tarnished in the process.

How do we prevent this from happening again?  Well, first we need to stop bumbling idiots like Mark Lounsbury from being able to testify and be “expert witnesses” in something they only have mediocre knowledge about.  Second, the prosecutors in this case should have criminal charges brought upon them.  The experts that came to Miss Amero’s defense have also formed a group to bring cases like this to the spotlight in order to educate the public to prevent “bad law”.  Finally, we need to stop being so goddamn uptight and listen to reason.  Anytime someone mentions “the kids”, people tend to lose their minds in some mob mentality.  Strangely, it reminds me of Horton Hears a Who.  Check that movie out if you have the time one day.

blank page at end of wordML document

So I’m doing this XML to wordML conversion using XSLT and there is this annoying blank page at the end of my document.  My code looks like this:

<w:body>
   <wx:sect>
      <w:p>
         <w:pPr>
            <w:sectPr>
               <w:type w:val="continuous"/>
            </w:sectPr>
         </w:pPr>
         <w:r>
            <w:t>section 1</w:t>
         </w:r>
      </w:p>
   </wx:sect>
   <wx:sect>
      <w:p>
         <w:pPr>
            <w:sectPr>
               <w:type w:val="continuous"/>
            </w:sectPr>
         </w:pPr>
         <w:r>
            <w:t>section 2</w:t>
         </w:r>
      </w:p>
   </wx:sect>
</w:body>

However, all you have to do to avoid the blank page is move the section formatting information from inside the last paragraph of the last section to a sibling node next to the last paragraph node.

<w:body>
   <wx:sect>
      <w:p>
         <w:pPr>
            <w:sectPr>
               <w:type w:val="continuous"/>
            </w:sectPr>
         </w:pPr>
         <w:r>
            <w:t>section 1</w:t>
         </w:r>
      </w:p>
   </wx:sect>
   <wx:sect>
      <w:p>
         <w:pPr>
         </w:pPr>
         <w:r>
            <w:t>section 2</w:t>
         </w:r>
      </w:p>
      <w:sectPr>
         <w:type w:val="continuous"/>
      </w:sectPr>
   </wx:sect>
</w:body>

Tada! Blank page gone and your document still has excellent structure.

daily IM tidbit

(4:07:35 PM) me: in the end, she was a wild stallion
(4:07:39 PM) me: and he had to just let her go
(4:07:45 PM) me: into the wilderness
(4:08:43 PM) friend: lol
(4:08:48 PM) friend: to eat hey
(4:08:53 PM) friend: or whatever wild ponies eat
(4:09:01 PM) me: i think they eat smaller ponies
(4:09:05 PM) me: >.<
(4:09:24 PM) me: it’s a wild pony-eat-pony world out tharrr