Speeding up the program by avoiding unnecessary evaluations of the exponential function