Seems like the point of the assignment is more of an exercise in translating math to software than to write some monolithic conditional. Trying to solve the problem without using math works, but not in an elegant way.
Test it extensively if you're going to go with this, and expect that the...