24 September 2012

Project Euler - Problem 6

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.

Answer:
This is a simple problem. Again, I'm using VB.NET to solve this problem. But this time, it is not the Windows App, it's the Console Application. The reason is that we do not need any User Interface here.

So, let's just get right to the code.

Code:

Module Module1

    Sub Main()
        Dim x, y As Double

        For i As Integer = 1 To 100
            x = x + i
        Next

        For j As Integer = 1 To 100
            y = y + (j ^ 2)
        Next

        Console.WriteLine("Square of the sum of the first one hundred natural numbers : " & x ^ 2)
        Console.WriteLine("Sum of the squares of the first one hundred natural numbers : " & y)
        Console.WriteLine("Hence, the difference of both numbers is " & (x ^ 2) & " - " & y & " = " & (x ^ 2) - y)
    End Sub

End Module


Here's what it looks like :













From the picture above, we can see that the answer is 25164150

Good luck trying !

Cyber  Frost








No comments:

Post a Comment

Search