Padovan n-step number sequences: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: marked p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 52:
{{trans|Nim}}
<
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(‘ ’))</
{{out}}
Line 75:
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}
<
# 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</
{{out}}
<pre>
Line 119:
=={{header|ALGOL W}}==
<
% 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.</
{{out}}
<pre>
Line 170:
=={{header|AppleScript}}==
<
use framework "Foundation"
use scripting additions
Line 523:
end tell
end if
end zipWith</
{{Out}}
<pre>Padovan N-step Series:
Line 536:
=={{header|C}}==
{{trans|Wren}}
<
void padovanN(int n, size_t t, int *p) {
Line 564:
}
return 0;
}</
{{out}}
Line 579:
=={{header|F_Sharp|F#}}==
<
// 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>
{{out}}
<pre>
Line 597:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
math.ranges prettyprint sequences ;
Line 607:
"Padovan n-step sequences" print
2 8 [a..b] [ 15 swap padn ] map simple-table.</
{{out}}
<pre>
Line 622:
=={{header|Go}}==
{{trans|Wren}}
<
import "fmt"
Line 650:
fmt.Printf("%d: %3d\n", n, padovanN(n, t))
}
}</
{{out}}
Line 665:
=={{header|Haskell}}==
<
import Data.List (transpose, uncons, unfoldr)
Line 721:
justifyRight :: Int -> a -> [a] -> [a]
justifyRight n c = drop . length <*> (replicate n c <>)</
{{Out}}
<pre>Padovan N-step series:
Line 734:
=={{header|JavaScript}}==
<
"use strict";
Line 893:
// MAIN ---
return main();
})();</
{{Out}}
<pre>Padovan N-step series:
Line 906:
=={{header|Julia}}==
{{trans|Python}}
<
"""
First nterms terms of the first 2..max_nstep -step Padovan sequences.
Line 937:
print_Padovan_seq(nstep_Padovan())
</
:::: {| style="text-align: left;" border="4" cellpadding="2" cellspacing="2"
|+ Padovan <math>n</math>-step sequences
Line 960:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Padovan[2,tmax_]:=Module[{start,a,m},
start={1,1,1};
Line 979:
Padovan[6,15]
Padovan[7,15]
Padovan[8,15]</
{{out}}
<pre>{1,1,1,2,2,3,4,5,7,9,12,16,21,28,37}
Line 990:
=={{header|Nim}}==
<
proc rn(n, k: Positive): seq[int] =
Line 999:
for n in 2..8:
echo n, ": ", rn(n, 15).mapIt(($it).align(3)).join(" ")</
{{out}}
Line 1,011:
=={{header|Perl}}==
<
use warnings;
use feature <state say>;
Line 1,030:
print ' ' . $pad_n->next() for 1..25;
print "\n"
}</
{{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}}
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,075:
===Python: Procedural===
Generates a wikitable formatted output
<
"""
First t terms of the first 2..max_n-step Padovan sequences.
Line 1,101:
if __name__ == '__main__':
p = pad_like()
pr(p)</
{{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.
<
from itertools import chain, islice, repeat
Line 1,247:
if __name__ == '__main__':
main()
</syntaxhighlight>
{{Out}}
<pre>Padovan n-step series:
Line 1,260:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
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";
}</
{{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.
<
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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,324:
=={{header|Rust}}==
<
fn padovan(n: u64, x: u64) -> u64 {
if n < 2 {
Line 1,344:
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,358:
=={{header|Sidef}}==
{{trans|Perl}}
<
Enumerator({|callback|
var n = 2
Line 1,371:
for n in (2..8) {
say "n = #{n} | #{padovan(n).first(25).join(' ')}"
}</
{{out}}
<pre>
Line 1,385:
=={{header|Wren}}==
{{libheader|Wren-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))</
{{out}}
|