Added precomputed values and optimized the code.