Open access to data

During T175 there was a section on “eGovernment” – block 4 part 3. Section 2 discussed modernising government and suggested that two essential aspects of electronic government should be:

Making most, or even all, of the government’s services available online.

Bringing online services together, so that the user does not have to go to different departmental websites for different services.

The text also remarked there are a multitude of formats and making tools to mine this data is complicated.

Hand Rosling has a talk on TED about a dedicated tool to mine such publicly funded data – Gapminder. The software is free to download and the site even offers handouts, lecture plans and interactive presentations. It’s a fascinating talk, highlighting the possibilities of presenting data in new and more accessible ways.

Edit: Here’s Tim Berners Lee discussing open data availability a year after requesting it.

Why are video games the 30 something’s hidden vice?

I’m 35. There’s one PS3 in our house and I bought it. It’s attached to a nice big Plasma TV with a high refresh rate. If that sounds like I’m bragging, I’m not.

See the kids come in from school a good hour and a half before I do. So they can adhere to the rules (complete homework and get their kit ready for the next day) yet still be engrossed in video games by the time I get home. But it doesn’t end there, see I have a Lovefilm subscription so that means my wife likes to watch a film or two on the PS3 once we’ve eaten.

I like on line multi-player, particularly FPS but I can’t really get on before about ten each evening. The weird thing is I’m not alone. I just logged off after an hour playing with three other men my age(ish) – completely unplanned.

There’s something deeply flawed with three thirty-something married men whispering into headsets at midnight on a week night trying to play PS3. Its almost as if its a taboo.

Injuries, contracts and holidays

Physiotherapist thinks that it’s going to be a while before I’m back running, although I haven’t had a specific diagnosis (the Army seems to feel physiotherapy is a panacea). He thinks there’s a 75% chance of recovery without further referral. So no impact PT and watch what I eat.

It’s all quiet on the education front – my next course (or module as the new terminology  dictates) isn’t until the end of September. The Open University also sent me out my contract for TU100′s Café forum – never read so much legalise.

On leave just now, which is nice. Kids are busy with youth club activities this week so I’m pretty much just relaxing at home. Re-read World War Z (someone I know is an extra in the upcoming movie) and re-played Final Fantasy VII.

I’m also drudging through Assassin’s Creed II. Don’t get me wrong it’s a great title but I’ve already done it. I rented it and finished it. Then I saw it on offer and bought a copy to play the add-on levels. FYI, the Platinum versionwon’t read the original’s save game.

Talk about first world problems, lol.

Sum of all the primes below two million

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

Seemed pretty straight forward, loop through all numbers up to 2,000,000 – if they’re prime add them to a tally.

Module Module1

  Sub Main()
    Dim beganAt As Date = Now
    Dim n As Integer = 2000000
    Dim total As Long = 0

    For counter As Integer = 2 To n
      If isPrime(counter) = True Then
        total = total + counter
      End If
    Next

    Dim endAt As Global.System.TimeSpan = Now.Subtract(beganAt)
    Dim took As Integer = endAt.Milliseconds

    Console.WriteLine(total.ToString + " in " + took.ToString + " ms.")
    Console.ReadKey()
  End Sub

  Private Function isPrime(ByVal thisNumber As Integer) As Boolean
    ' Prime numbers other than two are odd...
    If thisNumber = 2 Then
      Return True
    ElseIf thisNumber Mod 2 = 0 Then
      Return False
    End If

    'Check it isn't divisible by up to its square root
    '(consider n=(root n)(root n) as factors)
    For counter As Integer = 3 To (Math.Sqrt(thisNumber)) Step 2
      If thisNumber Mod counter = 0 Then
        Return False
      End If
    Next
    Return True
  End Function

End Module

Just needed to be careful with data types – VB.Net’s Integer isn’t large enough so I changed to a Long. Gives 142,913,828,922 in 953 milliseconds.

 

 

Pythagorean triplet

Project Euler again, this time its problem 9.

A Pythagorean triplet is a set of three natural numbers, a<b<c, for which:

a^{2}+b^{2}=c^{2}

For example:

3^{2}+4^{2}=9+16=25=5^{2}.

There exists exactly one Pythagorean triplet for which abc = 1000.
Find the product abc.

My first draft is simply brute force checking:

Module Module1

  Sub Main()
    Dim beganAt As Date = Now

    Dim answer As Integer = pythagorean(1000)

    Dim endAt As Global.System.TimeSpan = Now.Subtract(beganAt)
    Dim took As Integer = endAt.Milliseconds

    Console.WriteLine(answer.ToString + " in " + took.ToString + "ms.")
    Console.ReadKey()
  End Sub

  Private Function pythagorean(ByVal thisNumber As Integer) As Integer
    For a As Integer = 1 To thisNumber
      For b As Integer = 1 To thisNumber
        For c As Integer = 1 To thisNumber
          If a + b + c = 1000 Then
            If (a * a) + (b * b) = (c * c) Then
              Return (a * b * c)
            End If
          End If
        Next
      Next
    Next
    Return -1
  End Function

End Module

It takes 375 milliseconds but gives the correct answer.

 

The 10001st prime number

Project Euler time again, I’ve come out of sequence – here’s 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?

I’m going to start at the beginning and check if each is a prime, until I find the 10001th.

Module Module1
  Sub Main()
    Dim beganAt As Date = Now
    Dim n = 10001
    Dim prime As Integer = 0
    Dim counter As Integer = 0

    ' Check each number until you've got 10001 prime numbers.
    Do Until prime = n + 1
      counter = counter + 1
      If isPrime(counter) Then
        prime = prime + 1
      End If
    Loop

    Dim endAt As Global.System.TimeSpan = Now.Subtract(beganAt)
    Dim took As Integer = endAt.Milliseconds

    Console.WriteLine(counter.ToString + " in " + took.ToString + "ms.")
    Console.ReadKey()
  End Sub

  Private Function isPrime(ByVal thisNumber As Integer) As Boolean
    ' Prime numbers other than two are odd...
    If thisNumber = 2 Then
      Return True
    ElseIf thisNumber Mod 2 = 0 Then
      Return False
    End If

    'Check it isn't divisible by up to its square root
    '(consider n=(root n)(root n) as factors)
    For counter As Integer = 3 To (Math.Sqrt(thisNumber)) Step 2
      If thisNumber Mod counter = 0 Then
        Return False
      End If
    Next
    Return True
  End Function

End Module

I used a function for finding primes, it keeps coming up. It takes an integer and returns true or false by discounting even numbers except 2 and checking for divisibility up to the integer’s square root. If you consider n=\sqrt{n}\times\sqrt{n} then if you have not found a number that divides into n evenly once reaching \sqrt{n}, its factors can only be one and itself. This significantly reduces processing time and appears to be how my HP40gs works out its ISPRIME() function.

It gives the answer 104743 in 125 milliseconds.

Running injuries

My Dailymile report is looking a little sorry for itself this week as I’ve picked up an injury. Tendinitis – doctor recommends two weeks off running, anti-inflammatories, ice and rest. He suggested investigating my running shoes too, so I went in to my local running shop.

Starting with a set of neutral trainers, you run on a treadmill, which has a little video camera behind it. You can analyse your gait and adjust accordingly. Five pairs of trainers later and it appears that I need a moderate stability shoe as I over-pronate. Its also clear I need to focus on my running style because I seem to turn out my right foot a lot.

I thought I was going to get caught on the price but a pair of Asics GT2160 were quite reasonable and if it makes the difference its worth it. They offered a 30 day guarantee too, so I can go back if they make no difference.

Largest prime factor of a composite number

The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?

Prime factors are prime numbers that can be multiplied together to make a given number. One way to find them is to start by dividing the number by the first prime (2) and continuing to do so until it cannot be divided, then moving on to the next.

Module Module1
  Sub Main()
    Dim beganAt As Date = Now
    Dim n As Long = 600851475143
    Dim factor As Integer = 2
    Dim highestFactor As Integer = 1
    While n > 1
      If n Mod factor = 0 Then
        highestFactor = factor
        n = n / factor
        While n Mod factor = 0
          n = n / factor
        End While
      End If
      factor = factor + 1
    End While
    Dim endAt As Global.System.TimeSpan = Now.Subtract(beganAt)
    Dim took As Integer = endAt.Milliseconds
    Console.WriteLine(highestFactor.ToString + " in " + took.ToString + "ms.")
    Console.ReadKey()
  End Sub
End Module

Sum of all even Fibonacci terms below 4 million

The second Euler problem concerns the Fibonacci sequence, which for anyone doing MS221 is the basis of module A1.

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, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Following the logic from the previous problem, then I can check each number of the Fibonacci sequence to see if it’s even and add it to the total, n_{3} = n_{2} + n_{1} where n_{0}=1. We need to repeat this while n < 4000000 and add n to our total if it’s even.

Sub Main()
    Dim beganAt As Date = Now
    Dim n, n1, n2, total As Integer
    n1 = 0
    n2 = 1
    Do While n2 < 4000000
        n = n1 + n2
        n1 = n2
        n2 = n
        If n < 4000000 Then
            If n Mod 2 = 0 Then
                total = total + n
            End If
        End If
    Loop
    Dim endAt As Global.System.TimeSpan = Now.Subtract(beganAt)
    Dim took As Integer = endAt.Milliseconds
    Console.WriteLine(total.ToString + " in " + took.ToString + "ms.")
    Console.ReadKey()
End Sub

Gives an answer of 4613732, which is correct. I know there is a more efficient approach but I’m tired and pissed off. Today was not a good day.