Java Implementation of Problem 6

View source code here on GitHub!

public class p0006 implements IEuler
Object answer()
Returns:

The answer to Project Euler problem 6

 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*/
20package euler;
21
22public class p0006 implements IEuler {
23    @Override
24    public Object answer() {
25        int sum_of_squares = 0, sum = 0;
26        for (int i = 1; i < 101; i++) {
27            sum += i;
28            sum_of_squares += i * i;
29        }
30
31        int square_of_sum = sum * sum;
32        return square_of_sum - sum_of_squares;
33    }
34}

Tags: arithmetic-progression, sequence-summation