01-summation-notation's Σ accumulates by adding. ∏ (capital pi) is the exact same accumulation pattern, just multiplying instead — and it shows up wherever probabilities of independent events combine (the likelihood of a whole dataset is a product of per-example probabilities) or wherever a count is built up multiplicatively (a factorial, covered next, is itself a ∏).
Implement product(f, lo, hi), computing ∏_{i=lo}^{hi} f(i) — call f once per integer i from lo to hi inclusive, and multiply the results together. The signature and docstring are already in the editor.
lo and hi are integers; hi may be less than lo, in which case the product is empty (the standard mathematical convention: an empty product is 1, not 0 — multiplying by nothing should leave a value unchanged, exactly like adding nothing leaves a sum unchanged at 0).f takes a single integer and returns a number (int or float).Structurally identical to 01-summation-notation's loop — only the accumulator's starting value (1 instead of 0) and the combining operation (*= instead of +=) differ.
Starting the accumulator at 1 is exactly what makes the empty-range case correctly return 1 with no special-cased branch, the same way starting at 0 did for summation's empty case.
No results yet
Run your code to check it against the examples, or submit to run every test.