Padovan n-step number sequences: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: marked p2js compatible)
m (syntax highlighting fixup automation)
Line 52:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F rn(n, k) -> [Int]
assert(k >= 2)
V result = I n == 2 {[1, 1, 1]} E rn(n - 1, n + 1)
Line 60:
 
L(n) 2..8
print(n‘: ’rn(n, 15).map(it -> ‘#3’.format(it)).join(‘ ’))</langsyntaxhighlight>
 
{{out}}
Line 75:
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}
<langsyntaxhighlight lang="algol68">BEGIN # show some valuies of the Padovan n-step number sequences #
# returns an array with the elements set to the elements of #
# the Padovan sequences from 2 to max s & elements 1 to max e #
Line 105:
print( ( newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 119:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % show some valuies of the Padovan n-step number sequences %
% sets R(i,j) to the jth element of the ith padovan sequence %
% maxS is the number of sequences to generate and maxE is the %
Line 156:
end for_n
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 170:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
Line 523:
end tell
end if
end zipWith</langsyntaxhighlight>
{{Out}}
<pre>Padovan N-step Series:
Line 536:
=={{header|C}}==
{{trans|Wren}}
<langsyntaxhighlight lang="c">#include <stdio.h>
 
void padovanN(int n, size_t t, int *p) {
Line 564:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 579:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Padovan n-step number sequences. Nigel Galloway: July 28th., 2021
let rec pad=function 2->Seq.unfold(fun(n:int[])->Some(n.[0],Array.append n.[1..2] [|Array.sum n.[0..1]|]))[|1;1;1|]
|g->Seq.unfold(fun(n:int[])->Some(n.[0],Array.append n.[1..g] [|Array.sum n.[0..g-1]|]))(Array.ofSeq(pad(g-1)|>Seq.take(g+1)))
[2..8]|>List.iter(fun n->pad n|>Seq.take 15|>Seq.iter(printf "%d "); printfn "")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 597:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: compiler.tree.propagation.call-effect io kernel math
math.ranges prettyprint sequences ;
 
Line 607:
 
"Padovan n-step sequences" print
2 8 [a..b] [ 15 swap padn ] map simple-table.</langsyntaxhighlight>
{{out}}
<pre>
Line 622:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 650:
fmt.Printf("%d: %3d\n", n, padovanN(n, t))
}
}</langsyntaxhighlight>
 
{{out}}
Line 665:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (second)
import Data.List (transpose, uncons, unfoldr)
 
Line 721:
 
justifyRight :: Int -> a -> [a] -> [a]
justifyRight n c = drop . length <*> (replicate n c <>)</langsyntaxhighlight>
{{Out}}
<pre>Padovan N-step series:
Line 734:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 893:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Padovan N-step series:
Line 906:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">
"""
First nterms terms of the first 2..max_nstep -step Padovan sequences.
Line 937:
 
print_Padovan_seq(nstep_Padovan())
</langsyntaxhighlight>{{out}}
:::: {| style="text-align: left;" border="4" cellpadding="2" cellspacing="2"
|+ Padovan <math>n</math>-step sequences
Line 960:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Padovan]
Padovan[2,tmax_]:=Module[{start,a,m},
start={1,1,1};
Line 979:
Padovan[6,15]
Padovan[7,15]
Padovan[8,15]</langsyntaxhighlight>
{{out}}
<pre>{1,1,1,2,2,3,4,5,7,9,12,16,21,28,37}
Line 990:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, sequtils, strutils
 
proc rn(n, k: Positive): seq[int] =
Line 999:
 
for n in 2..8:
echo n, ": ", rn(n, 15).mapIt(($it).align(3)).join(" ")</langsyntaxhighlight>
 
{{out}}
Line 1,011:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <state say>;
Line 1,030:
print ' ' . $pad_n->next() for 1..25;
print "\n"
}</langsyntaxhighlight>
{{out}}
<pre>N = 2 | 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37 49 65 86 114 151 200 265 351 465 616
Line 1,042:
=={{header|Phix}}==
{{trans|Go}}
<!--<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;">padovann</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
Line 1,059:
<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: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">&</span><span style="color: #000000;">padovann</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,075:
===Python: Procedural===
Generates a wikitable formatted output
<langsyntaxhighlight lang="python">def pad_like(max_n=8, t=15):
"""
First t terms of the first 2..max_n-step Padovan sequences.
Line 1,101:
if __name__ == '__main__':
p = pad_like()
pr(p)</langsyntaxhighlight>
 
{{out}}
Line 1,138:
Patterns of functional composition are constrained more by mathematical necessity than by arbitrary convention, but this still leaves room for alternative idioms of functional coding in Python. It is to be hoped that others will contribute divergent examples, enriching the opportunities for contrastive insight which Rosetta code aims to provide.
 
<langsyntaxhighlight lang="python">'''Padovan n-step number sequences'''
 
from itertools import chain, islice, repeat
Line 1,247:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
{{Out}}
<pre>Padovan n-step series:
Line 1,260:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>say 'Padovan N-step sequences; first 25 terms:';
 
for 2..8 -> \N {
my @n-step = 1, 1, 1, { state $n = 2; @n-step[ ($n - N .. $n++ - 1).grep: * >= 0 ].sum } … *;
put "N = {N} |" ~ @n-step[^25]».fmt: "%5d";
}</langsyntaxhighlight>
{{out}}
<pre>Padovan N-step sequences; first 25 terms:
Line 1,278:
=={{header|REXX}}==
Some additional code was added for this REXX version to minimize the width for any particular column.
<langsyntaxhighlight lang="rexx">/*REXX program computes and shows the Padovan sequences for M steps for N numbers. */
parse arg n m . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 15 /*Not specified? Then use the default.*/
Line 1,308:
/*──────────────────────────────────────────────────────────────────────────────────────*/
pd: procedure expose @. #; parse arg x; if @.x\==0 then return @.x /*@.x defined?*/
do k=1 for #; _= x-1-k; @.x= @.x + @._; end; return @.x</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,324:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
fn padovan(n: u64, x: u64) -> u64 {
if n < 2 {
Line 1,344:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,358:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func padovan(N) {
Enumerator({|callback|
var n = 2
Line 1,371:
for n in (2..8) {
say "n = #{n} | #{padovan(n).first(25).join(' ')}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,385:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var padovanN // recursive
Line 1,401:
var t = 15
System.print("First %(t) terms of the Padovan n-step number sequences:")
for (n in 2..8) Fmt.print("$d: $3d" , n, padovanN.call(n, t))</langsyntaxhighlight>
 
{{out}}
10,333

edits