How to resolve the algorithm Motzkin numbers step by step in the Dart programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Motzkin numbers step by step in the Dart 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 Dart programming language
Source code in the dart programming language
import 'dart:math';
void main() {
var M = List<int>.filled(42, 1);
M[0] = 1;
M[1] = 1;
print('1');
print('1');
for (int n = 2; n < 42; ++n) {
M[n] = M[n - 1];
for (int i = 0; i <= n - 2; ++i) {
M[n] += M[i] * M[n - 2 - i];
}
if (isPrime(M[n])) {
print('${M[n]} is a prime');
} else {
print('${M[n]}');
}
}
}
bool isPrime(int n) {
if (n <= 1) return false;
if (n == 2) return true;
for (int i = 2; i <= sqrt(n); ++i) {
if (n % i == 0) return false;
}
return true;
}
You may also check:How to resolve the algorithm Fractran step by step in the Nim programming language
You may also check:How to resolve the algorithm String concatenation step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Random number generator (device) step by step in the ProDOS programming language
You may also check:How to resolve the algorithm Forward difference step by step in the Ring programming language
You may also check:How to resolve the algorithm Casting out nines step by step in the Seed7 programming language