Fortran Implementation of Problem 6

View source code here on GitHub!

integer Problem0006/p0006()
 1! Project Euler Problem 6
 2!
 3! This turned out to be really easy
 4!
 5! Problem:
 6!
 7! The sum of the squares of the first ten natural numbers is,
 8! 1**2 + 2**2 + ... + 10**2 = 385
 9!
10! The square of the sum of the first ten natural numbers is,
11! (1 + 2 + ... + 10)**2 = 55**2 = 3025
12!
13! Hence the difference between the sum of the squares of the first ten natural
14! numbers and the square of the sum is 3025 − 385 = 2640.
15!
16! Find the difference between the sum of the squares of the first one hundred
17! natural numbers and the square of the sum.
18
19module Problem0006
20    implicit none
21contains
22    pure integer function p0006() result(answer)
23        integer :: sum, sum_of_squares, i
24        answer = 0
25        sum = 1
26        sum_of_squares = 1
27
28        do i = 2, 100
29            sum = sum + i
30            sum_of_squares = sum_of_squares + i * i
31        end do
32
33        answer = sum * sum - sum_of_squares
34    end function p0006
35end module Problem0006

Tags: arithmetic-progression, sequence-summation