The model for this tool
One decision variable per food: servings bought per day. The cost to minimize is the serving price times servings, summed over foods. Each nutrition target is one straight-line requirement (calories ≥ 2,000; protein ≥ 50), and each food’s serving cap is an upper bound that keeps the plan edible.
minimize 0.25·eggs + 0.35·rice + 0.55·beans + 0.20·milk
subject to 72·eggs + 205·rice + 110·beans + 149·milk ≥ 2000 (calories)
6.3·eggs + 4.3·rice + 7·beans + 7.7·milk ≥ 50 (protein)
0 ≤ each food ≤ 12 servingsWith the default numbers the optimum is 12 milk + 1.04 rice = $2.76/day: milk carries the best calories- and protein-per-dollar, so the solver fills it to the cap and tops up calories with rice.
The idea in one sentence
Linear programming answers the question "what is the best plan when every rule and every goal is a straight-line relationship?" — it finds the allocation that pushes a goal (cost, interest, priority score) as far as it can go without breaking any rule.
The word "programming" is 1940s operations-research slang for "planning," not computer programming. The method was invented to allocate scarce materials (L. Kantorovich, USSR, 1939; G. Dantzig, USA, 1947) and has been a standard tool in logistics, finance, and economics ever since — the 1975 Nobel Prize in Economics went to its founders.
Why the best plan sits on a corner
Every rule in this tool is a line (or flat boundary), so the set of legal plans is a flat-sided shape — a polygon in two dimensions, a polyhedron in more. This tool's goal is also linear, which means its "equal-value" lines are straight and parallel.
Picture sliding a ruler across the shape in the improving direction: the last point of the shape it touches is always a corner (vertex) — or a whole edge if two corners tie. That is the fundamental theorem of linear programming, and it is why a computer never has to check the infinitely many interior plans: only the finite list of corners, and in practice only a short walk among them.
How the grocery optimizer actually solves it
The solver in this page is the simplex method (Dantzig, 1947). It starts at one corner of the legal shape and repeatedly walks along an edge to a strictly better neighboring corner — choosing the first improving direction by index (Bland’s rule) — until no neighbor is better. That final corner is provably optimal.
A subtlety the solver also handles: a rule set can contradict itself ("save $600 and spend $620"), in which case no plan exists at all — the tool tells you instead of guessing. The walk always terminates (the implementation uses Bland's rule, which forbids cycling), and it is deterministic: the same inputs always produce the same plan.
A piece of history: the $39.93 diet
George Stigler posed this exact problem in 1945 — 77 foods, minimum nutrients, least cost — and hand-picked a $39.93/year diet he believed near-optimal. In 1947, the simplex method (invented the year before by George Dantzig) recomputed the true optimum: $39.69. Correcting an economist by 24 cents a year was modest; proving a method could find the best answer to any such problem was the beginning of modern optimization. The 1975 Nobel Prize in Economics went to the founders of this field.
Shadow prices: what a calorie costs
Solving the diet problem also prices its constraints. The calorie row’s dual value is the cost of one more calorie at the optimum — with the defaults, about $0.0017, i.e. 17¢ per 100 calories. The protein row prices at zero because the plan overshoots protein for free. These "shadow prices" are the nutrient prices implied by your own grocery economics — the same idea central banks and grid operators use to price constraints every day.