Skip to content

Commit 9b9c93b

Browse files
TotalVerbKristofferC
authored andcommitted
Generate instead of hard coding factorial tables (#22096)
* Generate instead of hard coding factorial tables * Preallocate factorial tables * Add tests with old literal tables
1 parent 086d36b commit 9b9c93b

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

base/combinatorics.jl

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,17 @@
22

33
# Factorials
44

5-
const _fact_table64 =
6-
Int64[1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,
7-
87178291200,1307674368000,20922789888000,355687428096000,6402373705728000,
8-
121645100408832000,2432902008176640000]
9-
10-
const _fact_table128 =
11-
UInt128[0x00000000000000000000000000000001, 0x00000000000000000000000000000002,
12-
0x00000000000000000000000000000006, 0x00000000000000000000000000000018,
13-
0x00000000000000000000000000000078, 0x000000000000000000000000000002d0,
14-
0x000000000000000000000000000013b0, 0x00000000000000000000000000009d80,
15-
0x00000000000000000000000000058980, 0x00000000000000000000000000375f00,
16-
0x00000000000000000000000002611500, 0x0000000000000000000000001c8cfc00,
17-
0x0000000000000000000000017328cc00, 0x0000000000000000000000144c3b2800,
18-
0x00000000000000000000013077775800, 0x00000000000000000000130777758000,
19-
0x00000000000000000001437eeecd8000, 0x00000000000000000016beecca730000,
20-
0x000000000000000001b02b9306890000, 0x000000000000000021c3677c82b40000,
21-
0x0000000000000002c5077d36b8c40000, 0x000000000000003ceea4c2b3e0d80000,
22-
0x000000000000057970cd7e2933680000, 0x00000000000083629343d3dcd1c00000,
23-
0x00000000000cd4a0619fb0907bc00000, 0x00000000014d9849ea37eeac91800000,
24-
0x00000000232f0fcbb3e62c3358800000, 0x00000003d925ba47ad2cd59dae000000,
25-
0x0000006f99461a1e9e1432dcb6000000, 0x00000d13f6370f96865df5dd54000000,
26-
0x0001956ad0aae33a4560c5cd2c000000, 0x0032ad5a155c6748ac18b9a580000000,
27-
0x0688589cc0e9505e2f2fee5580000000, 0xde1bc4d19efcac82445da75b00000000]
5+
const _fact_table64 = Vector{Int64}(20)
6+
_fact_table64[1] = 1
7+
for n in 2:20
8+
_fact_table64[n] = _fact_table64[n-1] * n
9+
end
10+
11+
const _fact_table128 = Vector{UInt128}(34)
12+
_fact_table128[1] = 1
13+
for n in 2:34
14+
_fact_table128[n] = _fact_table128[n-1] * n
15+
end
2816

2917
function factorial_lookup(n::Integer, table, lim)
3018
n < 0 && throw(DomainError())

test/combinatorics.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,34 @@ end
6767
@test factorial(Int32(12)) === Int32(479001600)
6868
@test_throws OverflowError factorial(Int32(13))
6969
end
70+
71+
_fact_table64 =
72+
Int64[1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,
73+
87178291200,1307674368000,20922789888000,355687428096000,6402373705728000,
74+
121645100408832000,2432902008176640000]
75+
76+
_fact_table128 =
77+
UInt128[0x00000000000000000000000000000001, 0x00000000000000000000000000000002,
78+
0x00000000000000000000000000000006, 0x00000000000000000000000000000018,
79+
0x00000000000000000000000000000078, 0x000000000000000000000000000002d0,
80+
0x000000000000000000000000000013b0, 0x00000000000000000000000000009d80,
81+
0x00000000000000000000000000058980, 0x00000000000000000000000000375f00,
82+
0x00000000000000000000000002611500, 0x0000000000000000000000001c8cfc00,
83+
0x0000000000000000000000017328cc00, 0x0000000000000000000000144c3b2800,
84+
0x00000000000000000000013077775800, 0x00000000000000000000130777758000,
85+
0x00000000000000000001437eeecd8000, 0x00000000000000000016beecca730000,
86+
0x000000000000000001b02b9306890000, 0x000000000000000021c3677c82b40000,
87+
0x0000000000000002c5077d36b8c40000, 0x000000000000003ceea4c2b3e0d80000,
88+
0x000000000000057970cd7e2933680000, 0x00000000000083629343d3dcd1c00000,
89+
0x00000000000cd4a0619fb0907bc00000, 0x00000000014d9849ea37eeac91800000,
90+
0x00000000232f0fcbb3e62c3358800000, 0x00000003d925ba47ad2cd59dae000000,
91+
0x0000006f99461a1e9e1432dcb6000000, 0x00000d13f6370f96865df5dd54000000,
92+
0x0001956ad0aae33a4560c5cd2c000000, 0x0032ad5a155c6748ac18b9a580000000,
93+
0x0688589cc0e9505e2f2fee5580000000, 0xde1bc4d19efcac82445da75b00000000]
94+
95+
for expected in Any[_fact_table64, _fact_table128]
96+
for (n, factn) in enumerate(expected)
97+
@test factorial(oftype(factn, n)) === factn
98+
end
99+
end
70100
end

0 commit comments

Comments
 (0)