reducing the number of lines of the code

the entire calculation is done using a for loop.