Improved efficiency of prime calculation