C# Implementation of Problem 6

View source code here on GitHub!

class p0006
: Euler.IEuler
object Answer ()
 1/*
 2Project Euler Problem 6
 3
 4This turned out to be really easy
 5
 6Problem:
 7
 8The sum of the squares of the first ten natural numbers is,
 91**2 + 2**2 + ... + 10**2 = 385
10
11The square of the sum of the first ten natural numbers is,
12(1 + 2 + ... + 10)**2 = 55**2 = 3025
13
14Hence the difference between the sum of the squares of the first ten natural
15numbers and the square of the sum is 3025 − 385 = 2640.
16
17Find the difference between the sum of the squares of the first one hundred
18natural numbers and the square of the sum.
19*/
20using System;
21
22namespace Euler
23{
24    public class p0006 : IEuler
25    {
26        public object Answer()
27        {
28            int sum_of_squares = 0,
29                  sum = 0;
30            for (int i = 1; i < 101; i++)
31            {
32                sum += i;
33                sum_of_squares += i * i;
34            }
35
36            int square_of_sum = sum * sum;
37            return square_of_sum - sum_of_squares;
38        }
39    }
40}

Tags: arithmetic-progression, sequence-summation