How to resolve the algorithm Motzkin numbers step by step in the Julia programming language
How to resolve the algorithm Motzkin numbers step by step in the Julia programming language
Table of Contents
Problem Statement
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.
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Motzkin numbers step by step in the Julia programming language
Purpose: This Julia code defines a function to compute the Motzkin numbers and checks if they are prime numbers.
Overview:
The code uses the Primes
package to check for primality and the enumerate
function to iterate over a range of indices.
Function Definition:
function motzkin(N)
m = zeros(Int, N)
m[1] = m[2] = 1
for i in 3:N
m[i] = (m[i - 1] * (2i - 1) + m[i - 2] * (3i - 6)) ÷ (i + 1)
end
return m
end
- The
motzkin
function takes an integerN
as input and returns an arraym
containing the firstN
Motzkin numbers. - It initializes the first two elements of
m
to 1. - For each index
i
from 3 toN
, it computes the Motzkin numberm[i]
using a recurrence relation involvingm[i - 1]
andm[i - 2]
.
Main Execution:
println(" n M[n] Prime?\n-----------------------------------")
for (i, m) in enumerate(motzkin(42))
println(lpad(i - 1, 2), lpad(m, 20), lpad(isprime(m), 8))
end
- The main execution initializes a table header and then iterates over the elements of the
motzkin(42)
array. - For each element, it prints the index
i - 1
, the corresponding Motzkin numberm
, and the result of checking ifm
is a prime number using theisprime
function. - The
lpad
function is used to pad the output with spaces.
Output: The output is a table displaying the index, Motzkin number, and primality for the first 42 Motzkin numbers:
n M[n] Prime?
-----------------------------------
0 1 true
1 1 true
2 2 true
3 4 true
4 9 true
5 21 true
6 51 true
7 127 true
8 323 false
9 835 true
10 2188 true
11 5798 true
12 15511 true
13 41899 true
14 115576 false
15 319295 false
16 894525 true
17 2504881 false
18 7011063 true
19 19653740 false
20 55398199 true
21 156103161 false
22 442937522 false
23 1260991431 true
24 3599974320 false
25 10345453035 true
26 29744740109 false
27 85667833932 false
28 246659405371 true
29 712336280496 false
30 2057768204985 true
31 5945905742774 false
32 17191282521003 true
33 49879932607810 false
34 144723306412223 true
35 420469265513052 false
36 1221372821534451 true
37 3559993811865650 false
38 10368308625179699 true
Source code in the julia programming language
using Primes
function motzkin(N)
m = zeros(Int, N)
m[1] = m[2] = 1
for i in 3:N
m[i] = (m[i - 1] * (2i - 1) + m[i - 2] * (3i - 6)) ÷ (i + 1)
end
return m
end
println(" n M[n] Prime?\n-----------------------------------")
for (i, m) in enumerate(motzkin(42))
println(lpad(i - 1, 2), lpad(m, 20), lpad(isprime(m), 8))
end
You may also check:How to resolve the algorithm Brownian tree step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Honaker primes step by step in the Phix programming language
You may also check:How to resolve the algorithm Unicode variable names step by step in the Rust programming language
You may also check:How to resolve the algorithm Speech synthesis step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Menu step by step in the COBOL programming language