how can make my Phi function code more efficient? - C#