|
2 | 2 |
|
3 | 3 | # Factorials |
4 | 4 |
|
5 | | -const _fact_table64 = Vector{Int64}(undef, 20) |
6 | | -_fact_table64[1] = 1 |
7 | | -for n in 2:20 |
8 | | - _fact_table64[n] = _fact_table64[n-1] * n |
| 5 | +const _fact_table64 = let _fact_table64 = Vector{Int64}(undef, 20) |
| 6 | + _fact_table64[1] = 1 |
| 7 | + for n in 2:20 |
| 8 | + _fact_table64[n] = _fact_table64[n-1] * n |
| 9 | + end |
| 10 | + Tuple(_fact_table64) |
9 | 11 | end |
10 | 12 |
|
11 | | -const _fact_table128 = Vector{UInt128}(undef, 34) |
12 | | -_fact_table128[1] = 1 |
13 | | -for n in 2:34 |
14 | | - _fact_table128[n] = _fact_table128[n-1] * n |
| 13 | +const _fact_table128 = let _fact_table128 = Vector{UInt128}(undef, 34) |
| 14 | + _fact_table128[1] = 1 |
| 15 | + for n in 2:34 |
| 16 | + _fact_table128[n] = _fact_table128[n-1] * n |
| 17 | + end |
| 18 | + Tuple(_fact_table128) |
15 | 19 | end |
16 | 20 |
|
17 | | -function factorial_lookup(n::Integer, table, lim) |
18 | | - n < 0 && throw(DomainError(n, "`n` must not be negative.")) |
19 | | - n > lim && throw(OverflowError(string(n, " is too large to look up in the table; consider using `factorial(big(", n, "))` instead"))) |
20 | | - n == 0 && return one(n) |
21 | | - @inbounds f = table[n] |
| 21 | +function factorial_lookup( |
| 22 | + n::Union{Checked.SignedInt,Checked.UnsignedInt}, |
| 23 | + table::Union{NTuple{20,Int64},NTuple{34,UInt128}}, lim::Int) |
| 24 | + idx = Int(n) |
| 25 | + idx < 0 && throw(DomainError(n, "`n` must not be negative.")) |
| 26 | + idx > lim && throw(OverflowError(lazy"$n is too large to look up in the table; consider using `factorial(big($n))` instead")) |
| 27 | + idx == 0 && return one(n) |
| 28 | + f = getfield(table, idx) |
22 | 29 | return oftype(n, f) |
23 | 30 | end |
24 | 31 |
|
|
0 commit comments