Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Thanks guys, I ended up solving this with a semi-brute force method. I was hoping for a mathematical solution, but my method should be quicker than what I had before.