code to calculate the k-factor.