Calculation of terms and sum