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    integer function p0006() result(answer)
23        integer :: sum
24        integer :: sum_of_squares
25        integer :: i
26        answer = 0
27        sum = 1
28        sum_of_squares = 1
29
30        do i = 2, 100
31            sum = sum + i
32            sum_of_squares = sum_of_squares + i * i
33        end do
34
35        answer = sum * sum - sum_of_squares
36    end function p0006
37end module Problem0006

Tags: arithmetic-progression, sequence-summation