Often times when assembling circuits, there may not be an exact resistor to match your schematic. Multiple resistors in series or parallel can be used instead to approximate or sometimes exactly match the desired resistance.
Resistors in series are additive, making for a very simple calculation:
For resistors in parallel, it's a bit more involved. Due to current being split proportionally across resistors in parallel and voltage remaining constant, Ohm's Law can help reach the following formula:
A list of around 70 different resistor values was made based on resistors I had for use. Based on the max number of resistors chosen, all possible combinations of resistors (including ones with fewer resistors than the max) are generated. Series and parallel resistance values are then computed for each pair and their respective percent errors compared to the desired resistance value are calculated. Finally, the combination with the lowest percent error is displayed.
Despite memoization being used for several different operations, this strategy is still fairly slow, especially for a max resistor count of 3 or greater. However, it works for most cases as two resistors is usually plenty to find a suitable combination.
The source for this project can be found here.