Original problem · C++ solution
The recurrence multiplies all positive integers through N, starting with the base value one.
The recurrence multiplies all positive integers through N, starting with the base value one. An empty product correctly handles N=0.
- Time: O(n).
- Extra space: O(1).
The types of operands control intermediate arithmetic. Read large values into long long before forming sums, products, or differences so the arithmetic itself uses 64 bits.
Integer division truncates toward zero; floor and ceiling formulas need special care for negative values.
State the invariant without looking at the code. Give one input that breaks the most tempting incorrect approach, then add it to the tests.