do not use math.Pow to compute ** 99, and only compute p if required
compute r only if required