I can't look closer at your code, I was trying to see where your maths might have gone off the rails.
Here is an expression, not your problem:
100 * 100 + C * C - 100 * 100
Isn't that just C * C?
Here
is a good question. Does that happen? Add printing. See if. See what gets returned when.
Is the printing you do plausible or does it just reflect the flaws?
a7