Motzkin numbers

From Rosetta Code
Revision as of 20:02, 13 August 2021 by PureFox (talk | contribs) (Created new draft task, Motzkin numbers, and added a Wren solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Motzkin numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definition

The nth Motzkin number (denoted by M[n]) is the number of different ways of drawing non-intersecting chords between n points on a circle (not necessarily touching every point by a chord).

By convention M[0] = 1.


Task

Compute and show on this page the first 42 Motzkin numbers or, if your language does not support 64 bit integers, as many such numbers as you can. Indicate which of these numbers are prime.


See also



Wren

Library: Wren-long
Library: Wren-fmt

<lang ecmascript>import "/long" for ULong import "/fmt" for Fmt

var motzkin = Fn.new { |n|

   var m = List.filled(n+1, 0)
   m[0] = ULong.one
   m[1] = ULong.one
   for (i in 2..n) {
       m[i] = (m[i-1] * (2*i + 1) + m[i-2] * (3*i -3))/(i + 2)
   }
   return m

}

System.print(" n M[n] Prime?") System.print("-----------------------------------") var m = motzkin.call(41) for (i in 0..41) {

   Fmt.print("$2d  $,23i  $s", i, m[i], m[i].isPrime)

}</lang>

Output:
 n          M[n]             Prime?
-----------------------------------
 0                        1  false
 1                        1  false
 2                        2  true
 3                        4  false
 4                        9  false
 5                       21  false
 6                       51  false
 7                      127  true
 8                      323  false
 9                      835  false
10                    2,188  false
11                    5,798  false
12                   15,511  true
13                   41,835  false
14                  113,634  false
15                  310,572  false
16                  853,467  false
17                2,356,779  false
18                6,536,382  false
19               18,199,284  false
20               50,852,019  false
21              142,547,559  false
22              400,763,223  false
23            1,129,760,415  false
24            3,192,727,797  false
25            9,043,402,501  false
26           25,669,818,476  false
27           73,007,772,802  false
28          208,023,278,209  false
29          593,742,784,829  false
30        1,697,385,471,211  false
31        4,859,761,676,391  false
32       13,933,569,346,707  false
33       40,002,464,776,083  false
34      114,988,706,524,270  false
35      330,931,069,469,828  false
36      953,467,954,114,363  true
37    2,750,016,719,520,991  false
38    7,939,655,757,745,265  false
39   22,944,749,046,030,949  false
40   66,368,199,913,921,497  false
41  192,137,918,101,841,817  false