Fairly simple program to calculate the needed factories to build the desired amount of materials from given materials. It uses iteration to satisfy all intermediate material needs and prints the working factoriy configuration and the total of material consumption.
(You might solve this better using linear equasions, but I could not wrap my head around fast enough to formulate such an equasion system, so just throw more cpu power onto it for now.)