Combination Math

Heyo!
I’ve done some coding in the past, but I’m new to PHP. This is more of a logic/function/math question more than a syntax question.
I’m trying to create a calculator for pipe fittings. We have access to 8°, 11.25°, 22.5°, 45°, and 90° pipe fittings. The idea is that a user could enter the angle their pipes need to fit at, then the calculator could tell them what fittings they’d need to use to get closest to that angle.

Instead of using all of those fittings, I’m using 8 degree and 11.25 degree fittings, and I can just convert them into larger fittings (e.g. four 11.25 degree fittings will be converted to 45 degrees). So, I can find the closest known value, then solve the equation 8x + 11.25y = angle, where x and y are whole number integers and x + y < 360°. I’m not sure what arithmetic steps I’ll need to take to solve this equation, since my memory of math class is fuzzy.

The other idea I had was to have an associative array, where the angle was the key and the number of fittings was the value (e.g. 16 degrees => “2,0”). The attached image is an excel spreadsheet that I used to find all of the values < 360°. I have absolutely no clue how I’d sort this table so that it could be easily interpreted by the code. The only good thing about this table (so far) is that there’s only one duplicate angle, 360°. The rest of the angles are unique.

So, should I try and use an equation, or should I try and sort this table somehow to make it easy to understand? Any help would be greatly appreciated!

Sponsor our Newsletter | Privacy Policy | Terms of Service