← All lints

fact_tables_by_hand

L0587Idiomswarn

Factorial table built by hand

fact[i] = fact[i - 1] * i % P in a loop: Comb<Mod<P>>(n) with comb.fact(i), comb.inv_fact(i), comb.choose(a, b)

Flagged
let (n, k): (1..=1000000, 0..=1000000) = read()
var fact: List<Int> = [1 for _ in 0..=n]
for i in 1..=n:
    fact[i] = fact[i - 1] * i % 998244353
print(fact[min(k, n)])
Write instead
type Z = Mod<998244353>

let (n, k): (1..=1000000, 0..=1000000) = read()
let comb = Comb<Z>(n)
print(comb.fact(min(k, n)))

Why

A loop that fills fact[i] = fact[i - 1] * i % P builds the factorial table every counting solution needs, and usually its inverse table and a choose next to it, each with its own % P to forget. Comb<Mod<P>>(n) (std.mod) holds both tables, built in O(n)O(n): comb.fact(i) is i!i!, comb.inv_fact(i) is 1/i!1 / i!, and comb.choose(a, b) is (ab)\binom{a}{b} in O(1)O(1), 0 when b is outside 0..=a. The tables grow on demand, so Comb<Z>() with no size also works. A recurrence over the previous entry that is not a factorial (powers, (i + 1) products) is not reported. There is no automatic fix, because each reader of the table becomes a different Comb call.

The practice it serves

A hand-written pattern the language or the standard library already says in one step.

Not flagged

A near miss: close to the pattern, but not it.

Near miss
// A recurrence over the previous entry that is not `i!` is not a factorial table.
let n: 1..=1000000 = read()
var pw: List<Int> = [1 for _ in 0..=n]
for i in 1..=n:
    pw[i] = pw[i - 1] * 2 % 998244353
var ways: List<Int> = [1 for _ in 0..=n]
for i in 1..=n:
    ways[i] = ways[i - 1] * (i + 1) % 998244353
print(pw[n], ways[n])

Turn it off

For one block, wrap it in with lint(fact_tables_by_hand: allow):. For a project, add fact_tables_by_hand = "allow" under [lints] in cp.toml.

Generated from spec/lints/fact_tables_by_hand, the same record the linter is built from.