File tree Expand file tree Collapse file tree 2 files changed +26
-13
lines changed Expand file tree Collapse file tree 2 files changed +26
-13
lines changed Original file line number Diff line number Diff line change @@ -15,24 +15,24 @@ export bellnum,
15
15
16
16
Compute the ``n``th Bell number.
17
17
"""
18
- function bellnum (bn :: Integer )
19
- if bn < 0
20
- throw (DomainError (bn, " n must be nonnegative " ))
21
- else
22
- n = BigInt (bn)
18
+ function bellnum (n :: Integer )
19
+ if n < 0
20
+ throw (DomainError (n ))
21
+ elseif n < 2
22
+ return 1
23
23
end
24
- list = Vector {BigInt} (undef, div (n * (n + 1 ), 2 ) )
24
+ list = Vector {BigInt} (undef, n )
25
25
list[1 ] = 1
26
26
for i = 2 : n
27
- beg = div (i* (i- 1 ),2 )
28
- list[beg+ 1 ] = list[beg]
29
- for j = 2 : i
30
- list[beg+ j] = list[beg+ j- 1 ]+ list[beg+ j- i]
27
+ for j = 1 : i - 2
28
+ list[i - j - 1 ] += list[i - j]
31
29
end
30
+ list[i] = list[1 ] + list[i - 1 ]
32
31
end
33
- return list[end ]
32
+ return list[n ]
34
33
end
35
34
35
+
36
36
"""
37
37
catalannum(n)
38
38
Original file line number Diff line number Diff line change 56
56
@test stirlings2 (6 , 6 ) == 1
57
57
58
58
# bell
59
- @test bellnum (7 ) == 877
59
+ @test bellnum .(0 : 10 ) == [
60
+ 1
61
+ 1
62
+ 2
63
+ 5
64
+ 15
65
+ 52
66
+ 203
67
+ 877
68
+ 4140
69
+ 21147
70
+ 115975
71
+ ]
72
+
60
73
@test bellnum (42 ) == parse (BigInt," 35742549198872617291353508656626642567" )
61
- @test_throws DomainError bellnum (- 1 )
74
+ @test_throws DomainError ( - 1 ) bellnum (- 1 )
You can’t perform that action at this time.
0 commit comments