◐ Shell
clean mode source ↗

Fix intermediate integer overflow in math.perm and math.comb by ekMartian · Pull Request #2879 · lcompilers/lpython

Description
The previous implementations calculated the full factorial(n) before division. This caused an intermediate integer overflow for valid 32-bit/64-bit inputs. For example, perm(30, 1) correctly evaluates to 30, but calculating 30! first exceeds the 64-bit integer limit and returns garbage.

Changes

  • Replaced factorial-based formulas with iterative loops to keep intermediate values small.
  • Added symmetry optimization for comb (k = n - k if k > n/2).
  • Used local variables (e.g., k_final) to prevent parameter immutability compiler errors.

Verification
Verified locally that perm(30, 1) and comb(30, 1) now correctly return 30 instead of overflowing to negative values.