This question is a mix of math and programming, but I\'m guessing the solution lay on the programming side of things. Also, apologies in advance for not formatting the math