Section 9.3: Difference Between Sum of Squares and Square of Sums#
Adapted from: “Introduction to Programming Using Fortran 95/2003/2008” by Ed Jorgensen (March 2018 / Version 3.0.51).
Program to Calculate the Difference Between the Sum of Squares and the Square of Sums#
program SOSdifference
! declare variables
implicit none
integer :: i, n, SumOfSqrs = 0, SqrOfSums = 0, difference
! display initial header
!write (*,*) "Example Program"
print *
write (*,'(a)') "This program calculates the"
write (*,'(a)') "difference between sum of squares "
write (*,'(a)') "and square of sums"
! prompt for and read the n value
!write (*,*) "Enter N value: "
!read (*,*) n
! Set n = 100 for Jupyter Notebook
n = 100
! loop from 1 to n
do i = 1, n
! compute sum of squares
SumOfSqrs = SumOfSqrs + i ** 2
! compute square of sums
SqrOfSums = SqrOfSums + i
end do
! square the sums
SqrOfSums = SqrOfSums ** 2
! compute the difference between sum of squares and square of sums
difference = SqrOfSums - SumOfSqrs
! display results
print *
write (*,'(a)') "Results..."
print *
write (*, '(a, i5, a, i10)') "The square of the sums from 1 to ", n, " is -> ", SqrOfSums
print *
write (*, '(a, i5, a, i10)') "The sum of the squares from 1 to ", n, " is -> ", SumOfSqrs
print *
write (*,'(a, i10)') "The difference between the square of the sums and sum of the squares is -> ", difference
print *
end program SOSdifference
The following explanation is given by “Code Copilot” within ChatGPT 4.
The Fortran program SOSdifference
is designed to calculate and display the difference between two mathematical quantities: the square of the sum of the first n
natural numbers and the sum of the squares of the first n
natural numbers. Here is a step-by-step explanation of how the program works:
Variable Declaration: The program begins by declaring the necessary variables:
i
andn
are integers used for looping and setting the upper limit, respectively.SumOfSqrs
andSqrOfSums
are initialized to zero. They are used to store the sum of squares and the square of sums, respectively.difference
is declared to store the computed difference between the two calculated values.
User Interface Messages: The program uses
write
statements to print messages to the user explaining what the program does. These messages are:“This program calculates the difference between sum of squares and square of sums.”
Input Handling: Originally, the program likely had code to prompt the user to enter the value of
n
(number of terms). In this specific version, the user input is commented out, andn
is set to 100 directly in the code. This adjustment might be for simplification, especially in environments like Jupyter Notebooks where interactive inputs can be cumbersome.Computation Loop:
The program uses a
do
loop from 1 ton
. Inside this loop:It calculates the sum of squares (
SumOfSqrs
) by adding the square of each numberi
to the existing sum.It also calculates the sum of the numbers (
SqrOfSums
) by adding each numberi
to the cumulative sum.
Final Calculation:
After exiting the loop, it squares the total sum (
SqrOfSums
) to get the square of the sum of the numbers from 1 ton
.It calculates the
difference
by subtracting the sum of squares from the square of sums.
Result Display: Finally, the program prints the results:
It displays the square of the sums.
It displays the sum of the squares.
It displays the computed difference between these two values.
This program highlights a classic mathematical problem often used to demonstrate algebraic identities or computational capabilities in programming.
Program Compilation and Execution#
The above program is compiled and run using Fortran Package Manager (fpm):
import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Fortran_Code/Section_9_3_SOS_Difference"
os.chdir(code_dir)
build_status = os.system("fpm build 2>/dev/null")
exec_status = os.system("fpm run 2>/dev/null")
This program calculates the
difference between sum of squares
and square of sums
Results...
The square of the sums from 1 to 100 is -> 25502500
The sum of the squares from 1 to 100 is -> 338350
The difference between the square of the sums and sum of the squares is -> 25164150