How to use this calculator
List the foods you actually buy, with price per serving (check the shelf price ÷ servings per pack) and the calories and protein in one serving.
Set your daily targets — the FDA label reference is 2,000 calories and 50 g protein.
Cap servings per food so the plan stays realistic (default 12).
Read the cheapest plan, its daily and weekly cost, and how each food contributes.
Open "How the math works" for the full linear-programming explanation, including the marginal prices of your nutrients.
The standard formula
grocery-budget-optimizerminimize c·x subject to A·x ≥ b, x ≥ 0
A linear program in standard form: choose the plan x that minimizes a linear cost c·x subject to linear requirement constraints A·x ≥ b. Here x is servings of each food, c the per-serving prices, and the rows of A·x ≥ b the nutrition targets and caps.
How the calculation works
grocery-budget-optimizerminimize Σ priceᵢ·sᵢ
subject to Σ caloriesᵢ·sᵢ ≥ calorie target
Σ proteinᵢ·sᵢ ≥ protein target
0 ≤ sᵢ ≤ max servingsThis tool's exact model, solved by the two-phase simplex method with Bland's anti-cycling rule. If no combination of servings within the caps can reach both targets, the solver reports that honestly instead of guessing.
Site-wide conventions and all calculator formulas — rounding policy, period conventions, and how every formula is verified.
Worked example
Input
Four foods: eggs ($0.25/serving, 72 cal, 6.3 g protein), cooked rice ($0.35, 205 cal, 4.3 g), black beans ($0.55, 110 cal, 7 g), whole milk ($0.20, 149 cal, 7.7 g). Targets: 2,000 calories, 50 g protein, cap 12 servings each.
Result
Cheapest plan: 12 servings of milk (the best nutrition-per-dollar, so the optimizer fills to the cap) plus 1.04 servings of rice — $2.76/day, $19.32/week, hitting 2,001 calories and 96.9 g protein. The marginal calorie costs $0.0017 (17¢ per 100 calories); extra protein is free because the plan already overshoots it.
The diet problem, then and now
| Solve | Year | Method | Annual cost (77 foods) |
|---|---|---|---|
| Stigler’s hand estimate | 1945 | Trial, error, and insight — no optimality proof | $39.93 |
| First simplex solve | 1947 | Linear programming (Dantzig’s new algorithm) | $39.69 |
| This calculator | Today | Simplex in your browser, any foods you enter | your numbers |
The 1945/1947 figures are Stigler’s nine-food subsistence diet study, retold in the 1975 Nobel economics lectures; the method that beat the estimate became this page’s solver.
Assumptions and important notes
- Prices and nutrition are per serving exactly as you type them; defaults are typical grocery-store values, not live prices.
- Nutrition targets treat calories and protein as the only requirements — a real diet needs more (fiber, micronutrients, variety); use this as a budgeting bound, not a meal plan.
- Servings are divisible (0.5 cup, 1.3 eggs-as-large-egg-equivalents); displayed servings are rounded to two decimals and repaired upward so targets still read as met.
- The 2,000-calorie and 50 g protein defaults mirror the FDA Nutrition Facts reference values, which are labeling conventions rather than personal dietary advice.
Common mistakes
- Treating the result as a prescribed diet — it is the mathematical cost floor for your inputs, useful for budgeting and price comparison.
- Entering package prices instead of per-serving prices (a $3.00 dozen of eggs is $0.25 per egg).
- Forgetting the serving cap: with no cap the cheapest plan may be 15 pounds of rice.
Frequently asked questions
What is the "diet problem" this calculator is based on?
In 1945, economist George Stigler asked which combination of foods feeds a person adequately for the least money — and hand-computed a $39.93/year answer he suspected was not quite optimal. In 1947 the newly invented simplex method proved him right: the true optimum was $39.69. That solve was linear programming's first public triumph, and this tool runs the same mathematics on your foods.
Why does the answer pile up on one or two foods?
Minimizing cost finds the foods with the best nutrition-per-dollar and buys them until a cap or a different nutrient's requirement forces a switch. That is exactly what "cheapest legal diet" means mathematically — the serving caps exist so the result stays edible and nutritionally sane.
What are the shadow prices in the notes?
Each nutrition target has a marginal price: how much one more calorie (or gram of protein) would add to your daily cost at the optimum. If protein overshoots for free, its price is $0. Economists call these dual values — they are the market prices of nutrients implied by your own grocery list.
Why do I only see the polygon chart with two foods?
With exactly two foods the whole problem lives in a flat plane — every affordable pair of servings is a point in a shaded region, and cost lines slide across it until the last touch, which is the optimum. With more foods the same geometry happens in dimensions we cannot draw, so the page shows bars and the table instead.
Are the default nutrition numbers medical advice?
No. Calories and protein per serving are typical grocery-store values for illustration — edit them to match your labels. Targets default to the FDA label reference (2,000 calories, 50 g protein), which is a labeling convention, not a personal recommendation. See a professional for dietary guidance.
Does this upload my grocery list anywhere?
No. The optimization runs entirely in your browser; nothing you type leaves your device.
Sources
The claims on this page are backed by these official sources:
- Nobel Prize 1975 — Kantorovich & Koopmans — the founding story of linear programming and optimal resource allocation (the diet problem’s method).
- FDA — Changes to the Nutrition Facts Label — the 2,000-calorie daily reference used for the default target.