Motzkin numbers: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
(9 intermediate revisions by 6 users not shown)
Line 118:
41 192,137,918,101,841,817
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">motzkin: function [n][
result: array.of:n+1 0
result\[0]: 1
result\[1]: 1
 
loop 2..n 'i ->
result\[i]: ((result\[i-1] * (inc 2*i)) + result\[i-2] * (sub 3*i 3)) / i+2
return result
]
 
printLine: function [items][
pads: [4 20 7]
loop.with:'i items 'item [
prints pad to :string item pads\[i]
]
print ""
]
 
motzkins: motzkin 41
 
printLine ["n" "M[n]" "prime"]
print repeat "=" 31
 
loop.with:'i motzkins 'm ->
printLine @[i m prime? m]</syntaxhighlight>
 
{{out}}
 
<pre> 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 2188 false
11 5798 false
12 15511 true
13 41835 false
14 113634 false
15 310572 false
16 853467 false
17 2356779 false
18 6536382 false
19 18199284 false
20 50852019 false
21 142547559 false
22 400763223 false
23 1129760415 false
24 3192727797 false
25 9043402501 false
26 25669818476 false
27 73007772802 false
28 208023278209 false
29 593742784829 false
30 1697385471211 false
31 4859761676391 false
32 13933569346707 false
33 40002464776083 false
34 114988706524270 false
35 330931069469828 false
36 953467954114363 true
37 2750016719520991 false
38 7939655757745265 false
39 22944749046030949 false
40 66368199913921497 false
41 192137918101841817 false</pre>
 
=={{header|AWK}}==
Line 192 ⟶ 267:
41 192137918101841817 0
</pre>
 
 
=={{header|BASIC256}}==
Line 638 ⟶ 712:
66368199913921500
192137918101841820</pre>
 
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang=easylang>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
m[] = [ 1 1 ]
print 1 ; print 1
len m[] 39
arrbase m[] 0
for n = 2 to 38
m[n] = m[n - 1]
for i = 0 to n - 2
m[n] += m[i] * m[n - 2 - i]
.
if isprim m[n] = 1
print m[n] & " is a prime"
else
print m[n]
.
.
</syntaxhighlight>
 
=={{header|EMal}}==
Line 643 ⟶ 747:
fun isPrime = logic by int n
if n <= 1 do return false end
for int i = 2; i <= [int] !sqrt(n); ++i
if n % i == 0 do return false end
end
return true
end
fun format = text by var n, int max do return " " * (max - length([text] !n)) + n end
fun motzkin = List by int n
List m = int[].with(n)
Line 959 ⟶ 1,063:
66368199913921497
192137918101841817
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
 
local fn IsMotzkin
NSUInteger M(42) : M(0) = 1 : M(1) = 1
NSInteger i, n
printf @" 0.%20ld\n 1.%20ld", 1, 1
for n = 2 to 41
M(n) = M(n-1)
for i = 0 to n-2
M(n) += M(i) * M(n-2-i)
next
printf @"%2ld.%20ld\b", n, M(n)
if fn IsPrime( M(n) ) then print " <-- is a prime" else print
next
end fn
 
fn IsMotzkin
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
0. 1
1. 1
2. 2 <-- is a prime
3. 4
4. 9
5. 21
6. 51
7. 127 <-- is a prime
8. 323
9. 835
10. 2188
11. 5798
12. 15511 <-- is a prime
13. 41835
14. 113634
15. 310572
16. 853467
17. 2356779
18. 6536382
19. 18199284
20. 50852019
21. 142547559
22. 400763223
23. 1129760415
24. 3192727797
25. 9043402501
26. 25669818476
27. 73007772802
28. 208023278209
29. 593742784829
30. 1697385471211
31. 4859761676391
32. 13933569346707
33. 40002464776083
34. 114988706524270
35. 330931069469828
36. 953467954114363 <-- is a prime
37. 2750016719520991
38. 7939655757745265
39. 22944749046030949
40. 66368199913921497
41. 192137918101841817
</pre>
 
Line 1,174 ⟶ 1,358:
 
So basically we calculate (X<sub>n-1</sub>(1+2n)+3X<sub>n-2</sub>(n-1)) and divide that by 2+n and append this new value to the list. For n we use the list length. For X<sub>n-1</sub> we use the last element of the list. And, for X<sub>n-2</sub> we use the second to last element of the list. For the task we repeat this list operation 40 times, starting with the list 1 1 and check to see which elements of the resulting list are prime. Because these values get large, we need to use arbitrary precision integers for our list values.
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.math.BigInteger;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
 
public final class MotzkinNumbers {
 
public static void main(String[] aArgs) {
final int count = 42;
List<BigInteger> motzkins = motzkinNumbers(count);
NumberFormat ukNumberFormat = NumberFormat.getInstance(Locale.UK);
System.out.println(" n Motzkin[n]");
System.out.println("-----------------------------");
for ( int n = 0; n < count; n++ ) {
BigInteger motzkin = motzkins.get(n);
boolean prime = motzkin.isProbablePrime(PROBABILITY);
System.out.print(String.format("%2d %23s", n, ukNumberFormat.format(motzkin)));
System.out.println( prime ? " prime" : "" );
}
}
private static List<BigInteger> motzkinNumbers(int aSize) {
List<BigInteger> result = new ArrayList<BigInteger>(aSize);
result.add(BigInteger.ONE);
result.add(BigInteger.ONE);
for ( int i = 2; i < aSize; i++ ) {
BigInteger nextMotzkin = result.get(i - 1).multiply(BigInteger.valueOf(2 * i + 1))
.add(result.get(i - 2).multiply(BigInteger.valueOf(3 * i - 3)))
.divide(BigInteger.valueOf(i + 2));
result.add(nextMotzkin);
}
 
return result;
}
private static final int PROBABILITY = 20;
 
}
</syntaxhighlight>
{{ out }}
<pre>
n Motzkin[n]
-----------------------------
0 1
1 1
2 2 prime
3 4
4 9
5 21
6 51
7 127 prime
8 323
9 835
10 2,188
11 5,798
12 15,511 prime
13 41,835
14 113,634
15 310,572
16 853,467
17 2,356,779
18 6,536,382
19 18,199,284
20 50,852,019
21 142,547,559
22 400,763,223
23 1,129,760,415
24 3,192,727,797
25 9,043,402,501
26 25,669,818,476
27 73,007,772,802
28 208,023,278,209
29 593,742,784,829
30 1,697,385,471,211
31 4,859,761,676,391
32 13,933,569,346,707
33 40,002,464,776,083
34 114,988,706,524,270
35 330,931,069,469,828
36 953,467,954,114,363 prime
37 2,750,016,719,520,991
38 7,939,655,757,745,265
39 22,944,749,046,030,949
40 66,368,199,913,921,497
41 192,137,918,101,841,817
</pre>
 
=={{header|jq}}==
Line 1,358 ⟶ 1,636:
40 66368199913921497 False
41 192137918101841817 False</pre>
 
=={{header|Maxima}}==
There are many formulas for these numbers
<syntaxhighlight lang="maxima">
motzkin[0]:1$
motzkin[1]:1$
motzkin[n]:=((2*n+1)*motzkin[n-1]+(3*n-3)*motzkin[n-2])/(n+2)$
 
/* Test cases */
makelist(motzkin[i],i,0,41);
 
sublist(%,primep);
</syntaxhighlight>
{{out}}
<pre>
[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,1697385471211,4859761676391,13933569346707,40002464776083,114988706524270,330931069469828,953467954114363,2750016719520991,7939655757745265,22944749046030949,66368199913921497,192137918101841817]
 
[2,127,15511,953467954114363]
</pre>
 
=={{header|Nim}}==
Line 2,246 ⟶ 2,543:
{{libheader|Wren-long}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./long" for ULong
import "./fmt" for Fmt
 
var motzkin = Fn.new { |n|
9,476

edits