"use strict"; function round2(x) { return Math.round(x * 100) / 100; } module.exports = { slug: "right-triangle-pythagoras", title: "Right triangle side by the Pythagorean theorem", deck_category: "geometry", description: "Computes the missing side of a right triangle (hypotenuse or a leg) given the other two sides.", inputs: [ { name: "mode", type: "string", required: true, description: "Which side to solve for.", enum: ["find_hypotenuse", "find_leg"] }, { name: "known_a", type: "number", unit: "length units", required: true, description: "For find_hypotenuse: one leg. For find_leg: the known leg.", min: 0 }, { name: "known_b", type: "number", unit: "length units", required: true, description: "For find_hypotenuse: the other leg. For find_leg: the hypotenuse.", min: 0 } ], outputs: [ { name: "result", unit: "length units", description: "The computed hypotenuse (find_hypotenuse) or leg (find_leg)." } ], steps: [ "The Pythagorean theorem: for a right triangle with legs a and b and hypotenuse c, a^2 + b^2 = c^2.", "If mode is find_hypotenuse: c = sqrt(known_a^2 + known_b^2), where known_a and known_b are the two legs.", "If mode is find_leg: known_b is the hypotenuse and known_a is a leg; the missing leg = sqrt(known_b^2 - known_a^2). known_b must be greater than known_a.", "Round to 2 decimals." ], constants: [], sources: [ { kind: "edition_page", value: "Euclid. Elements, Book I, Proposition 47.", title: "Elements, Book I, Proposition 47 (the Pythagorean theorem)", publisher: "Standard geometry reference", retrieved_utc: "2026-09-30T19:47:00Z", note: "Classical statement and proof of the Pythagorean theorem." } ], disclaimer: null, questionText(inputs) { if (inputs.mode === "find_hypotenuse") { return `What is the hypotenuse of a right triangle with legs ${inputs.known_a} and ${inputs.known_b}?`; } return `What is the missing leg of a right triangle with hypotenuse ${inputs.known_b} and one leg ${inputs.known_a}?`; }, run(inputs) { const { mode, known_a, known_b } = inputs || {}; if (mode !== "find_hypotenuse" && mode !== "find_leg") throw new Error("mode must be find_hypotenuse or find_leg"); if (typeof known_a !== "number" || known_a <= 0 || typeof known_b !== "number" || known_b <= 0) { throw new Error("known_a and known_b must be positive numbers"); } let result; if (mode === "find_hypotenuse") { result = Math.sqrt(known_a * known_a + known_b * known_b); } else { if (known_b <= known_a) throw new Error("known_b (the hypotenuse) must be greater than known_a (the leg)"); result = Math.sqrt(known_b * known_b - known_a * known_a); } result = round2(result); return { answer: `${result.toFixed(2)}`, values: { result } }; }, examples: [ { name: "worked example", inputs: { mode: "find_hypotenuse", known_a: 3, known_b: 4 }, expected: { answer: "5.00" }, check: "3-4-5 is the classic Pythagorean triple, independently known: 3^2+4^2=9+16=25=5^2." }, { name: "test vector: find a leg", inputs: { mode: "find_leg", known_a: 5, known_b: 13 }, expected: { answer: "12.00" }, check: "5-12-13 is the classic Pythagorean triple, independently known: 13^2-5^2=169-25=144=12^2." } ] };