How should I reference this? Should I reference this?
As your implementation is based on the Quora solution, you obviously have to credit the author. Depending on how big a part of the entire thing this solution is, you might even consider offering the mathematician coauthorship for this.

If it suffices to acknowledge: As the Quora post is public, I would suggest to cite the post (and potentially do the thanks via footnote/acknowledgement section in addition). This ensures in particular that an interested reader can go and read the original post, which might be of interest in addition to your implementation.

Regarding the published paper: First, given that finding this solution was not trivial, I would suggest to cite it in your paper already only for the sake of others searching for these things. Moreover, you should give context for the solution you implemented, and the paper might belong to that context.