-
Notifications
You must be signed in to change notification settings - Fork 0
/
motzkin_numbers.sf
47 lines (36 loc) · 1.53 KB
/
motzkin_numbers.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/ruby
# Generate the first `n` Motzkin numbers (the numbers on the hypotenuse of the Motzkin triangle).
# Motzkin's triangle:
# 1
# 1 1
# 1 2 2
# 1 3 5 4
# 1 4 9 12 9
# 1 5 14 25 30 21
# 1 6 20 44 69 76 51
# 1 7 27 70 133 189 196 127
# 1 8 35 104 230 392 518 512 323
# 1 9 44 147 369 726 1140 1422 1353 835
# 1 10 54 200 560 1242 2235 3288 3915 3610 2188
# 1 11 65 264 814 2002 4037 6765 9438 10813 9713 5798
# There is no Motzkin number divisible by 8.
# Eu Sen-Peng & Liu Shu-Chung & Yeh, Yeong-nan proved in 2008 that no Motzkin number
# is a multiple of 8, in their paper "Catalan and Motzkin numbers modulo 4 and 8".
# https://www.math.sinica.edu.tw/www/file_upload/mayeh/2008CatalanandMotzkinnumbersmodulo4and8.pdf
# See also:
# https://oeis.org/A001006
# https://en.wikipedia.org/wiki/Motzkin_number
# https://oeis.org/wiki/User:Peter_Luschny/MotzkinNumbers
# Traslation of the Sage program by Peter Luschny from https://oeis.org/A001006
func motzkin_numbers(N) {
var (a, b, n) = (0, 1, 1)
N.of {
var M = b//n #/
n += 1
(a, b) = (b, (3*(n-1)*n*a + (2*n - 1)*n*b) // ((n+1)*(n-1))) #/
M
}
}
say motzkin_numbers(30)
__END__
1, 1, 2, 4, 9, 21, 51, 127, 323, 835, 2188, 5798, 15511, 41835, 113634, 310572, 853467, 2356779, 6536382, 18199284, 50852019, 142547559, 400763223, 1129760415, 3192727797, 9043402501, 25669818476, 73007772802, 208023278209, 593742784829