Motzkin numbers: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Quackery}}: linked to Primality by trial division)
m (syntax highlighting fixup automation)
Line 20:
Uses ALGOL 68G's LONG LONG INT which allows for programmer specifiable precision integers. The default precision is sufficient for this task.
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find some Motzkin numbers #
PR read "primes.incl.a68" PR
# returns a table of the Motzkin numbers 0 .. n #
Line 74:
)
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 124:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK --bignum -f MOTZKIN_NUMBERS.AWK
BEGIN {
Line 149:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 199:
 
=={{header|BASIC256}}==
<langsyntaxhighlight BASIC256lang="basic256">dim M(42)
M[0] = 1 : M[1] = 1
print rjust("1",18) : print rjust("1",18)
Line 219:
next i
return True
end function</langsyntaxhighlight>
 
 
=={{header|Burlesque}}==
<langsyntaxhighlight lang="burlesque">41rz{{2./}c!rz2?*{nr}c!\/2?/{JJ2.*J#Rnr#r\/+.nr.-}m[?*++it+.}m[Jp^#R{~]}5E!{fCL[1==}f[#r</langsyntaxhighlight>
{{out}}
<pre>1
Line 271:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
Line 305:
m1 = m;
}
}</langsyntaxhighlight>
 
{{out}}
Line 357:
=={{header|C#|CSharp}}==
Arbitrary precision. Now showing results up to the 80th. Note comment about square root limit, one might think the limit should be a <code>long</code> instead of an <code>int</code>, since the square root of a 35 digit number could be up to 18 digits (too big for an <code>int</code>).
<langsyntaxhighlight lang="csharp">using System;
using BI = System.Numerics.BigInteger;
Line 385:
a = t);
}
}</langsyntaxhighlight>
{{out}}
<pre style="height:121ex;"> 1
Line 469:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cstdint>
#include <iomanip>
#include <iostream>
Line 518:
<< is_prime(n) << '\n';
}
}</langsyntaxhighlight>
 
{{out}}
Line 569:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Motzkin numbers. Nigel Galloway: September 10th., 2021
let M=let rec fN g=seq{yield List.item 1 g; yield! fN(0L::(g|>List.windowed 3|>List.map(List.sum))@[0L;0L])} in fN [0L;1L;0L;0L]
M|>Seq.take 42|>Seq.iter(printfn "%d")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 621:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: combinators formatting io kernel math math.primes
tools.memory.private ;
 
Line 641:
dup motzkin [ commas ] keep prime? "prime" "" ?
"%2d %24s %s\n" printf
] each-integer</langsyntaxhighlight>
{{out}}
<pre>
Line 691:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">
Array m[42];
m[1]:=1;
Line 706:
; {the smallest prime factor for composites, so this actually gives}
; {slightly more information than requested}
</syntaxhighlight>
</lang>
 
{{out}}
Line 756:
=={{header|FreeBASIC}}==
Use any of the primality testing examples as an include.
<langsyntaxhighlight lang="freebasic">
#include "isprime.bas"
 
Line 770:
if isprime(M(n)) then print "is a prime" else print
next n
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 821:
{{libheader|Go-rcu}}
Assumes a 64 bit Go compiler.
<langsyntaxhighlight lang="go">package main
 
import (
Line 845:
fmt.Printf("%2d %23s %t\n", i, rcu.Commatize(e), rcu.IsPrime(e))
}
}</langsyntaxhighlight>
 
{{out}}
Line 896:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad.Memo (Memo, memo, startEvalMemo)
import Math.NumberTheory.Primes.Testing (isPrime)
import System.Environment (getArgs)
Line 926:
main = do
[n] <- map read <$> getArgs
printMotzkins n</langsyntaxhighlight>
 
{{out}}
Line 978:
Implementation:
 
<langsyntaxhighlight Jlang="j">nextMotzkin=: , (({:*1+2*#) + _2&{*3*#-1:)%2+#</langsyntaxhighlight>
 
Task example:
<langsyntaxhighlight Jlang="j"> (":,.' ',.('prime'#~ 1&p:@{.)"1) ,.nextMotzkin^:40(1 1x)
1
1
Line 1,023:
22944749046030949
66368199913921497
192137918101841817</langsyntaxhighlight>
 
This might not be a particularly interesting to describe algorithm (which might be why the task description currently fails to provide any details of the algorithm). Still... it's probably worth an attempt:
Line 1,039:
For a suitable implementation of `is_prime`, see e.g. # [[Erd%C5%91s-primes#jq]].
 
<langsyntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def motzkin:
Line 1,054:
(41 | motzkin
| range(0;length) as $i
|"\($i|lpad(2)) \(.[$i]|lpad(20)) \(.[$i]|if is_prime then "prime" else "" end)")</langsyntaxhighlight>
{{out}}
<pre>
Line 1,105:
=={{header|Julia}}==
{{trans|Wren}}
<langsyntaxhighlight lang="julia">using Primes
 
function motzkin(N)
Line 1,120:
println(lpad(i - 1, 2), lpad(m, 20), lpad(isprime(m), 8))
end
</langsyntaxhighlight>{{out}}
<pre>
n M[n] Prime?
Line 1,169:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Table[With[{o = Hypergeometric2F1[(1 - n)/2, -n/2, 2, 4]}, {n, o, PrimeQ[o]}], {n, 0, 41}] // Grid</langsyntaxhighlight>
{{out}}
Alignment lost in copying.
Line 1,217:
=={{header|Nim}}==
{{trans|Wren}}
<langsyntaxhighlight Nimlang="nim">import strformat, strutils
 
func isPrime(n: Positive): bool =
Line 1,242:
var m = motzkin(41)
for i, val in m:
echo &"{i:2} {insertSep($val):>23} {val.isPrime}"</langsyntaxhighlight>
 
{{out}}
Line 1,291:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">
M=vector(41)
M[1]=1
Line 1,298:
M=concat([1], M)
for(n=1, 42, print(M[n]," ",isprime(M[n])))
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Motzkin_numbers
Line 1,324:
printf "%3d%25s %s\n", $count++, s/\B(?=(\d\d\d)+$)/,/gr,
is_prime($_) ? 'prime' : '';
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,373:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,398:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d %23s %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">mi</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pi</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,448:
===native===
Normally I'd start with this simpler and perfectly valid 64-bit code, but at the moment am somewhat biased towards things that run online, without limiting things as this does.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">motzkin</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 1,466:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d %,23d %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">pi</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
Aside: <small>Note that, under 32-bit and p2js, M[36] and M[37] happen only by chance to be correct: an intermediate value exceeds precision (in this case for M[36] it is a multiple of 2 and hence ok, for M[37] it is rounded to the nearest multiple of 4, and the final divide by (i+1) results in an integer simply because there isn't enough precision to hold any fractional part).
Output as above on 64-bit, less four entries under 32-bit and pwa/p2js, since unlike M[36..37], M[38..41] don't quite get away with the precision loss, plus M[39] and above exceed precision and hence is_prime() limits on 32-bit anyway.</small>
Line 1,474:
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<langsyntaxhighlight Quackerylang="quackery"> ' [ 1 1 ]
41 times
[ dup -2 split nip do
Line 1,482:
behead drop
witheach
[ i^ echo sp dup echo isprime if [ say " prime" ] cr ]</langsyntaxhighlight>
 
{{out}}
Line 1,531:
=={{header|Raku}}==
===Using binomial coefficients and Catalan numbers===
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
constant \C = 1, |[\×] (2, 6 … ∞) Z/ 2 .. *;
Line 1,540:
 
say " 𝐧 𝐌[𝐧] Prime?";
𝐌[^42].kv.map: { printf "%2d %24s %s\n", $^k, $^v.&comma, $v.is-prime };</langsyntaxhighlight>
{{out}}
<pre> 𝐧 𝐌[𝐧] Prime?
Line 1,588:
===Using recurrence relationship===
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
my \𝐌 = 1, 1, { state $i = 2; ++$i; ($^b × (2 × $i - 1) + $^a × (3 × $i - 6)) ÷ ($i + 1) } … *;
 
say " 𝐧 𝐌[𝐧] Prime?";
𝐌[^42].kv.map: { printf "%2d %24s %s\n", $^k, $^v.&comma, $v.is-prime };</langsyntaxhighlight>
 
Same output
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program to display the first N Motzkin numbers, and if that number is prime. */
numeric digits 92 /*max number of decimal digits for task*/
parse arg n . /*obtain optional argument from the CL.*/
Line 1,640:
do k=p?.n by 6 while k*k<=x; if x//k ==0 then return 0
if x//(k+2)==0 then return 0
end /*k*/; return 1 /*Passed all divisions? It's a prime.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,691:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
 
motzkin = Enumerator.new do |y|
Line 1,707:
puts "#{'%2d' % i}: #{m}#{m.prime? ? ' prime' : ''}"
end
</syntaxhighlight>
</lang>
{{out}}
<pre >
Line 1,756:
=={{header|Rust}}==
{{trans|Wren}}
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
// num-format = "0.4"
Line 1,784:
);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,836:
=={{header|Sidef}}==
Built-in:
<langsyntaxhighlight lang="ruby">say 50.of { .motzkin }</langsyntaxhighlight>
 
Motzkin's triangle (the Motzkin numbers are on the hypotenuse of the triangle):
<langsyntaxhighlight lang="ruby">func motzkin_triangle(num, callback) {
var row = []
{ |n|
Line 1,849:
motzkin_triangle(10, {|row|
row.map { "%4s" % _ }.join(' ').say
})</langsyntaxhighlight>
 
{{out}}
Line 1,867:
Task:
 
<langsyntaxhighlight lang="ruby">func motzkin_numbers(N) {
 
var (a, b, n) = (0, 1, 1)
Line 1,881:
motzkin_numbers(42).each_kv {|k,v|
say "#{'%2d' % k}: #{v}#{v.is_prime ? ' prime' : ''}"
}</langsyntaxhighlight>
 
{{out}}
Line 1,933:
{{trans|Rust}}
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension BinaryInteger {
Line 1,971:
for (i, n) in m.enumerated() {
print("\(i): \(n) \(n.isPrime ? "prime" : "")")
}</langsyntaxhighlight>
 
{{out}}
Line 2,021:
{{libheader|Wren-long}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/long" for ULong
import "/fmt" for Fmt
 
Line 2,039:
for (i in 0..41) {
Fmt.print("$2d $,23i $s", i, m[i], m[i].isPrime)
}</langsyntaxhighlight>
 
{{out}}
Line 2,090:
 
=={{header|Yabasic}}==
<langsyntaxhighlight lang="yabasic">dim M(41)
M(0) = 1 : M(1) = 1
print str$(M(0), "%19g")
Line 2,113:
wend
return True
end sub</langsyntaxhighlight>
10,327

edits