Padovan n-step number sequences: Difference between revisions
m
→{{header|Wren}}: Minor tidy
SqrtNegInf (talk | contribs) (Added Perl) |
m (→{{header|Wren}}: Minor tidy) |
||
(38 intermediate revisions by 15 users not shown) | |||
Line 48:
# Use this to print and show here at least the first <code>t=15</code> values of the first <code>2..8</code> <math>n</math>-step sequences.<br> (The [https://oeis.org OEIS] column in the table above should be omitted).
<br><br>
=={{header|11l}}==
{{trans|Nim}}
<syntaxhighlight lang="11l">F rn(n, k) -> [Int]
assert(k >= 2)
V result = I n == 2 {[1, 1, 1]} E rn(n - 1, n + 1)
L result.len != k
result.append(sum(result[(len)-n-1 .< (len)-1]))
R result
L(n) 2..8
print(n‘: ’rn(n, 15).map(it -> ‘#3’.format(it)).join(‘ ’))</syntaxhighlight>
{{out}}
<pre>
2: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
3: 1 1 1 2 3 4 6 9 13 19 28 41 60 88 129
4: 1 1 1 2 3 5 7 11 17 26 40 61 94 144 221
5: 1 1 1 2 3 5 8 12 19 30 47 74 116 182 286
6: 1 1 1 2 3 5 8 13 20 32 51 81 129 205 326
7: 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349
8: 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362
</pre>
=={{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 81 ⟶ 105:
print( ( newline ) )
OD
END</
{{out}}
<pre>
Line 95 ⟶ 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 132 ⟶ 156:
end for_n
end
end.</
{{out}}
<pre>
Line 146 ⟶ 170:
=={{header|AppleScript}}==
<
use framework "Foundation"
use scripting additions
Line 499 ⟶ 523:
end tell
end if
end zipWith</
{{Out}}
<pre>Padovan N-step Series:
Line 509 ⟶ 533:
7 -> 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349
8 -> 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362</pre>
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">global t
t = 15
global p
dim p(t)
print "First"; t; " terms of the Padovan n-step number sequences:"
for n = 2 to 8
print n; ":";
call padovanN(n, p)
for i = 0 to t-1
print rjust(p[i],4);
next i
print
next n
end
subroutine padovanN(n, p)
if n < 2 or t < 3 then
for i = 0 to t-1
p[i] = 1
next i
return
end if
call padovanN(n-1, p)
for i = n + 1 to t-1
p[i] = 0
for j = i - 2 to i-n-1 step -1
p[i] += p[j]
next j
next i
return
end subroutine</syntaxhighlight>
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 CLS
110 t = 15
120 DIM p(t)
130 SUB padovann(n,p())
140 IF n < 2 OR t < 3 THEN
150 FOR i = 0 TO t-1
160 p(i) = 1
170 NEXT i
180 EXIT SUB
190 endif
200 padovann(n-1,p())
210 FOR i = n+1 TO t-1
220 p(i) = 0
230 FOR j = i-2 TO i-n-1 STEP -1
240 p(i) = p(i)+p(j)
250 NEXT j
260 NEXT i
270 EXIT SUB
280 END SUB
290 PRINT "First";t;" terms of the Padovan n-step number sequences:"
300 FOR n = 2 TO 8
310 PRINT n;":";
320 padovann(n,p())
330 FOR i = 0 TO t-1
340 PRINT USING "### ";p(i);
350 NEXT i
360 PRINT
370 NEXT n
380 END</syntaxhighlight>
==={{header|FreeBASIC}}===
{{trans|C}}
<syntaxhighlight lang="vb">' Rosetta Code problem: https://rosettacode.org/wiki/Padovan_n-step_number_sequences
' by Jjuanhdez, 05/2023
Const t = 15
Dim Shared As Integer p(t)
Sub padovanN(n As Integer, p() As Integer)
Dim As Integer i, j
If n < 2 Or t < 3 Then
For i = 0 To t-1
p(i) = 1
Next i
Exit Sub
End If
padovanN(n-1, p())
For i = n + 1 To t-1
p(i) = 0
For j = i - 2 To i-n-1 Step -1
p(i) += p(j)
Next j
Next i
End Sub
Print "First"; t; " terms of the Padovan n-step number sequences:"
Dim As Integer n, i
For n = 2 To 8
Print n; ": ";
padovanN(n, p())
For i = 0 To t-1
Print Using "### "; p(i);
Next i
Print
Next n
Sleep</syntaxhighlight>
{{out}}
<pre>First 15 terms of the Padovan n-step number sequences:
2: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
3: 1 1 1 2 3 4 6 9 13 19 28 41 60 88 129
4: 1 1 1 2 3 5 7 11 17 26 40 61 94 144 221
5: 1 1 1 2 3 5 8 12 19 30 47 74 116 182 286
6: 1 1 1 2 3 5 8 13 20 32 51 81 129 205 326
7: 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349
8: 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362</pre>
==={{header|Just BASIC}}===
{{trans|FreeBASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="vb">global t, p
t = 15
dim p(t)
print "First"; t; " terms of the Padovan n-step number sequences:"
for n = 2 to 8
print n; ":";
call padovanN n, p
for i = 0 to t-1
print using("####", p(i));
next i
print
next n
end
sub padovanN n, p
if n < 2 or t < 3 then
for i = 0 to t-1
p(i) = 1
next i
exit sub
end if
call padovanN n-1, p
for i = n + 1 to t-1
p(i) = 0
for j = i - 2 to i-n-1 step -1
p(i) = p(i) + p(j)
next j
next i
end sub</syntaxhighlight>
==={{header|QBasic}}===
{{trans|FreeBASIC}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">DECLARE SUB padovanN (n!, p!())
CONST t = 15
DIM SHARED p(t)
PRINT "First"; t; " terms of the Padovan n-step number sequences:"
FOR n = 2 TO 8
PRINT n; ":";
CALL padovanN(n, p())
FOR i = 0 TO t - 1
PRINT USING "### "; p(i);
NEXT i
PRINT
NEXT n
SUB padovanN (n, p())
IF n < 2 OR t < 3 THEN
FOR i = 0 TO t - 1
p(i) = 1
NEXT i
EXIT SUB
END IF
CALL padovanN(n - 1, p())
FOR i = n + 1 TO t - 1
p(i) = 0
FOR j = i - 2 TO i - n - 1 STEP -1
p(i) = p(i) + p(j)
NEXT j
NEXT i
END SUB</syntaxhighlight>
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">Global.i t = 15, Dim p(t)
Procedure.i padovanN(n, Array p(1))
If n < 2 Or t < 3
For i = 0 To t - 1
p(i) = 1
Next i
ProcedureReturn
EndIf
padovanN(n - 1, p())
For i.i = n + 1 To t - 1
p(i) = 0
For j.i = i - 2 To i - n - 1 Step -1
p(i) = p(i) + p(j)
Next j
Next i
EndProcedure
If OpenConsole()
PrintN("First" + Str(t) + " terms of the Padovan n-step number sequences:")
For n.i = 2 To 8
Print(Str(n) + ":")
padovanN(n, p())
For i.i = 0 To t - 1
Print(RSet(Str(p(i)),4))
Next i
PrintN("")
Next n
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
CloseConsole()
EndIf</syntaxhighlight>
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">t = 15
dim p(t)
print "First", t, " terms of the Padovan n-step number sequences:"
for n = 2 to 8
print n, ":";
padovanN(n, p())
for i = 0 to t-1
print p(i) using ("###");
next i
print
next n
end
sub padovanN(n, p())
local i, j
if n < 2 or t < 3 then
for i = 0 to t-1
p(i) = 1
next i
return
fi
padovanN(n-1, p())
for i = n + 1 to t-1
p(i) = 0
for j = i - 2 to i-n-1 step -1
p(i) = p(i) + p(j)
next j
next i
return
end sub</syntaxhighlight>
=={{header|C}}==
{{trans|Wren}}
<
void padovanN(int n, size_t t, int *p) {
Line 540 ⟶ 843:
}
return 0;
}</
{{out}}
Line 552 ⟶ 855:
7: 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349
8: 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362
</pre>
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <vector>
void padovan(const int32_t& limit, const uint64_t& termCount) {
std::vector<int32_t> previous_terms = { 1, 1, 1 };
for ( int32_t N = 2; N <= limit; ++N ) {
std::vector<int32_t> next_terms = { previous_terms.begin(), previous_terms.begin() + N + 1 };
while ( next_terms.size() < termCount ) {
int32_t sum = 0;
for ( int32_t step_back = 2; step_back <= N + 1; ++step_back ) {
sum += next_terms[next_terms.size() - step_back];
}
next_terms.emplace_back(sum);
}
std::cout << N << ": ";
for ( const int32_t& term : next_terms ) {
std::cout << std::setw(4) << term;
}
std::cout << std::endl;;
previous_terms = next_terms;
}
}
int main() {
const int32_t limit = 8;
const uint64_t termCount = 15;
std::cout << "First " << termCount << " terms of the Padovan n-step number sequences:" << std::endl;
padovan(limit, termCount);
}
</syntaxhighlight>
{{ out }}
<pre>
First 15 terms of the Padovan n-step number sequences:
2: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
3: 1 1 1 2 3 4 6 9 13 19 28 41 60 88 129
4: 1 1 1 2 3 5 7 11 17 26 40 61 94 144 221
5: 1 1 1 2 3 5 8 12 19 30 47 74 116 182 286
6: 1 1 1 2 3 5 8 13 20 32 51 81 129 205 326
7: 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349
8: 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362
</pre>
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight>
t = 15
len p[] t
#
proc padovan n . .
if n < 2 or t < 3
for i = 1 to t
p[i] = 1
.
return
.
padovan n - 1
for i = n + 2 to t
p[i] = 0
for j = i - 2 downto i - n - 1
p[i] += p[j]
.
.
.
for n = 2 to 8
padovan n
write n & ": "
for i = 1 to t
write p[i] & " "
.
print ""
.
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
<syntaxhighlight 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>
{{out}}
<pre>
1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
1 1 1 2 3 4 6 9 13 19 28 41 60 88 129
1 1 1 2 3 5 7 11 17 26 40 61 94 144 221
1 1 1 2 3 5 8 12 19 30 47 74 116 182 286
1 1 1 2 3 5 8 13 20 32 51 81 129 205 326
1 1 1 2 3 5 8 13 21 33 53 85 136 218 349
1 1 1 2 3 5 8 13 21 34 54 87 140 225 362
</pre>
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
math.ranges prettyprint sequences ;
Line 566 ⟶ 970:
"Padovan n-step sequences" print
2 8 [a..b] [ 15 swap padn ] map simple-table.</
{{out}}
<pre>
Line 581 ⟶ 985:
=={{header|Go}}==
{{trans|Wren}}
<
import "fmt"
Line 609 ⟶ 1,013:
fmt.Printf("%d: %3d\n", n, padovanN(n, t))
}
}</
{{out}}
Line 624 ⟶ 1,028:
=={{header|Haskell}}==
<
import Data.List (transpose, uncons, unfoldr)
------------------ PADOVAN N-STEP SERIES -----------------
Line 632 ⟶ 1,036:
padovans n
| 0 > n = []
| otherwise = unfoldr
where
xs
| 3 > n = repeat 1
| otherwise = padovans $ pred n
recurrence :: Int -> [Int] -> Maybe (Int, [Int])
recurrence n =
( fmap
. second
. flip (<>)
. pure
. sum
. take n
)
<*> uncons
--------------------------- TEST -------------------------
Line 651 ⟶ 1,057:
main =
putStrLn $
"Padovan N-step series:\n\n"
[
<> fmap show (take 15 $ padovans n)
)
[2 .. 8]
)
------------------------ FORMATTING ----------------------
(Int -> Char -> String -> String) -> [[String]] -> String
spacedTable aligned rows =
unlines $
fmap
(
rows
where
columnWidths =
fmap
(maximum . fmap length)
(transpose rows)
justifyRight :: Int -> a -> [a] -> [a]
justifyRight n c = drop . length <*> (replicate n c <>)</
{{Out}}
<pre>Padovan N-step series:
8 -> 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362</pre>
=={{header|J}}==
<syntaxhighlight lang=J> padovanN=: {{ (, [: +/ (-m) {. }:)@]^:([-2:)&1 1 }}
{{(":,.y),.': ',"1":{{ y padovanN 15 }}&>y}} 2+i.7
2: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
3: 1 1 1 2 3 4 6 9 13 19 28 41 60 88 129
4: 1 1 1 2 3 5 7 11 17 26 40 61 94 144 221
5: 1 1 1 2 3 5 8 12 19 30 47 74 116 182 286
6: 1 1 1 2 3 5 8 13 20 32 51 81 129 205 326
7: 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349
8: 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362
</syntaxhighlight>
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
public final class PadovanNStep {
public static void main(String[] aArgs) {
final int limit = 8;
final int termCount = 15;
System.out.println("First " + termCount + " terms of the Padovan n-step number sequences:");
padovan(limit, termCount);
}
private static void padovan(int aLimit, int aTermCount) {
List<Integer> previous = List.of( 1, 1, 1 );
for ( int N = 2; N <= aLimit; N++ ) {
List<Integer> next = new ArrayList<Integer>(previous.subList(0, N + 1));
while ( next.size() < aTermCount ) {
int sum = 0;
for ( int stepBack = 2; stepBack <= N + 1; stepBack++ ) {
sum += next.get(next.size() - stepBack);
}
next.add(sum);
}
System.out.print(N + ": ");
next.forEach( term -> System.out.print(String.format("%4d", term)));
System.out.println();
previous = next;
}
}
}
</syntaxhighlight>
{{ out }}
<pre>
First 15 terms of the Padovan n-step number sequences:
2: 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
3: 1 1 1 2 3 4 6 9 13 19 28 41 60 88 129
4: 1 1 1 2 3 5 7 11 17 26 40 61 94 144 221
5: 1 1 1 2 3 5 8 12 19 30 47 74 116 182 286
6: 1 1 1 2 3 5 8 13 20 32 51 81 129 205 326
7: 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349
8: 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362
</pre>
=={{header|JavaScript}}==
<
"use strict";
Line 850 ⟶ 1,320:
// MAIN ---
return main();
})();</
{{Out}}
<pre>Padovan N-step series:
Line 863 ⟶ 1,333:
=={{header|Julia}}==
{{trans|Python}}
<
"""
First nterms terms of the first 2..max_nstep -step Padovan sequences.
Line 894 ⟶ 1,364:
print_Padovan_seq(nstep_Padovan())
</
:::: {| style="text-align: left;" border="4" cellpadding="2" cellspacing="2"
|+ Padovan <math>n</math>-step sequences
Line 915 ⟶ 1,385:
|-
|}
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[Padovan]
Padovan[2,tmax_]:=Module[{start,a,m},
start={1,1,1};
start=MapIndexed[a[#2[[1]]]==#1&,start];
RecurrenceTable[{a[m]==a[m-2]+a[m-3]}~Join~start,a, {m,tmax}]
]
Padovan[n_,tmax_]:=Module[{start,eq,a,m},
start=Padovan[n-1,n+1];
start=MapIndexed[a[#2[[1]]]==#1&,start];
eq=Range[2,n+1];
eq=Append[start,a[m]==Total[a[m-#]&/@eq]];
RecurrenceTable[eq,a, {m,tmax}]
]
Padovan[2,15]
Padovan[3,15]
Padovan[4,15]
Padovan[5,15]
Padovan[6,15]
Padovan[7,15]
Padovan[8,15]</syntaxhighlight>
{{out}}
<pre>{1,1,1,2,2,3,4,5,7,9,12,16,21,28,37}
{1,1,1,2,3,4,6,9,13,19,28,41,60,88,129}
{1,1,1,2,3,5,7,11,17,26,40,61,94,144,221}
{1,1,1,2,3,5,8,12,19,30,47,74,116,182,286}
{1,1,1,2,3,5,8,13,20,32,51,81,129,205,326}
{1,1,1,2,3,5,8,13,21,33,53,85,136,218,349}
{1,1,1,2,3,5,8,13,21,34,54,87,140,225,362}</pre>
=={{header|Nim}}==
<
proc rn(n, k: Positive): seq[int] =
Line 926 ⟶ 1,426:
for n in 2..8:
echo n, ": ", rn(n, 15).mapIt(($it).align(3)).join(" ")</
{{out}}
Line 938 ⟶ 1,438:
=={{header|Perl}}==
<
use warnings;
use feature <state say>;
Line 957 ⟶ 1,457:
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 969 ⟶ 1,469:
=={{header|Phix}}==
{{trans|Go}}
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">2</span> <span style="color: #008080;">or</span> <span style="color: #000000;">t</span><span style="color: #0000FF;"><</span><span style="color: #000000;">3</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</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;">if</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</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;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">t</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">p</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">15</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"%d: %d %d %d %d %d %d %d %2d %2d %2d %2d %2d %3d %3d %3d\n"</span>
<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;">"First %d terms of the Padovan n-step number sequences:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">8</span> <span style="color: #008080;">do</span>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 999 ⟶ 1,502:
===Python: Procedural===
Generates a wikitable formatted output
<
"""
First t terms of the first 2..max_n-step Padovan sequences.
Line 1,025 ⟶ 1,528:
if __name__ == '__main__':
p = pad_like()
pr(p)</
{{out}}
Line 1,062 ⟶ 1,565:
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
#
def
'''Non-finite series of N-step Padovan numbers,
defined by a recurrence relation.
'''
take(1 + n)(
repeat(1) if 3 > n else
nStepPadovan(n - 1)
)
)
)
# recurrence :: Int -> [Int] -> Int
def recurrence(n):
'''Recurrence relation in Fibonacci,
Padovan and Perrin sequences.
'''
def go(xs):
h, *t = xs
return h, t + [sum(take(n)(xs))]
return go
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''First 15 terms each
where n is drawn from [2..8]
'''
print('Padovan n-step series:\n')
print(
(
str(x) for x
in take(15)(nStepPadovan(n))
)
)),
(str(x) for x in xs),
xs
)))
)
# ----------------------- GENERIC ------------------------
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.
'''
def go(xs):
return (
xs[0:n]
if isinstance(xs, (list, tuple))
else list(islice(xs, n))
)
return go
# unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
def unfoldr(f):
Line 1,122 ⟶ 1,650:
valueResidue = f(valueResidue[1])
return go
# ---------------------- FORMATTING ----------------------
#
def spacedTable(rows):
'''A table with right-aligned columns.
'''
columnWidths = [
for col in
]
return '\n'.join(
'
lambda x, w: x.rjust(w, ' '),
))
for row in
)
# MAIN ---
if __name__ == '__main__':
main()
</syntaxhighlight>
{{Out}}
<pre>Padovan n-step series:
8 -> 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362</pre>
=={{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,199 ⟶ 1,705:
=={{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,229 ⟶ 1,735:
/*──────────────────────────────────────────────────────────────────────────────────────*/
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,242 ⟶ 1,748:
8 │ 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362
───┴──────────────────────────────────────────
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
« → n t
« '''IF''' n 2 ≤ t 3 ≤ OR '''THEN'''
1 n 1 + NDUPN →LIST
'''ELSE'''
n 1 - n <span style="color:blue">NPADOVAN</span>
'''END'''
'''WHILE''' DUP SIZE t < '''REPEAT'''
DUP DUP SIZE DUP n - SWAP 1 - SUB ∑LIST +
'''END'''
» » '<span style="color:blue">NPADOVAN</span>' STO
« n 15 <span style="color:blue">NPADOVAN</span> » 'n' 2 8 1 SEQ
{{out}}
<pre>
1: { { 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37 }
{ 1 1 1 2 3 4 6 9 13 19 28 41 60 88 129 }
{ 1 1 1 2 3 5 7 11 17 26 40 61 94 144 221 }
{ 1 1 1 2 3 5 8 12 19 30 47 74 116 182 286 }
{ 1 1 1 2 3 5 8 13 20 32 51 81 129 205 326 }
{ 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349 }
{ 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362 } }
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">def padovan(n_step)
return to_enum(__method__, n_step) unless block_given?
ar = [1, 1, 1]
loop do
yield sum = ar[..-2].sum
ar.shift if ar.size > n_step
ar << sum
end
end
t = 15
(2..8).each do |n|
print "N=#{n} :"
puts "%5d"*t % padovan(n).take(t)
end
</syntaxhighlight>
{{out}}
<pre>N=2 : 2 2 3 4 5 7 9 12 16 21 28 37 49 65 86
N=3 : 2 3 4 6 9 13 19 28 41 60 88 129 189 277 406
N=4 : 2 3 5 7 11 17 26 40 61 94 144 221 339 520 798
N=5 : 2 3 5 8 12 19 30 47 74 116 182 286 449 705 1107
N=6 : 2 3 5 8 13 20 32 51 81 129 205 326 518 824 1310
N=7 : 2 3 5 8 13 21 33 53 85 136 218 349 559 895 1433
N=8 : 2 3 5 8 13 21 34 54 87 140 225 362 582 936 1505
</pre>
=={{header|Rust}}==
<syntaxhighlight lang="rust">
fn padovan(n: u64, x: u64) -> u64 {
if n < 2 {
return 0;
}
match n {
2 if x <= n + 1 => 1,
2 => padovan(n, x - 2) + padovan(n, x - 3),
_ if x <= n + 1 => padovan(n - 1, x),
_ => ((x - n - 1)..(x - 1)).fold(0, |acc, value| acc + padovan(n, value)),
}
}
fn main() {
(2..=8).for_each(|n| {
print!("\nN={}: ", n);
(1..=15).for_each(|x| print!("{},", padovan(n, x)))
});
}
</syntaxhighlight>
{{out}}
<pre>
N=2: 1,1,1,2,2,3,4,5,7,9,12,16,21,28,37,
N=3: 1,1,1,2,3,4,6,9,13,19,28,41,60,88,129,
N=4: 1,1,1,2,3,5,7,11,17,26,40,61,94,144,221,
N=5: 1,1,1,2,3,5,8,12,19,30,47,74,116,182,286,
N=6: 1,1,1,2,3,5,8,13,20,32,51,81,129,205,326,
N=7: 1,1,1,2,3,5,8,13,21,33,53,85,136,218,349,
N=8: 1,1,1,2,3,5,8,13,21,34,54,87,140,225,362,
</pre>
=={{header|Sidef}}==
{{trans|Perl}}
<syntaxhighlight lang="ruby">func padovan(N) {
Enumerator({|callback|
var n = 2
var pn = [1, 1, 1]
loop {
pn << sum(pn[n-N .. (n++-1) -> grep { _ >= 0 }])
callback(pn[-4])
}
})
}
for n in (2..8) {
say "n = #{n} | #{padovan(n).first(25).join(' ')}"
}</syntaxhighlight>
{{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
n = 3 | 1 1 1 2 3 4 6 9 13 19 28 41 60 88 129 189 277 406 595 872 1278 1873 2745 4023 5896
n = 4 | 1 1 1 2 3 5 7 11 17 26 40 61 94 144 221 339 520 798 1224 1878 2881 4420 6781 10403 15960
n = 5 | 1 1 1 2 3 5 8 12 19 30 47 74 116 182 286 449 705 1107 1738 2729 4285 6728 10564 16587 26044
n = 6 | 1 1 1 2 3 5 8 13 20 32 51 81 129 205 326 518 824 1310 2083 3312 5266 8373 13313 21168 33657
n = 7 | 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349 559 895 1433 2295 3675 5885 9424 15091 24166 38698
n = 8 | 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362 582 936 1505 2420 3891 6257 10061 16178 26014 41830
</pre>
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
var padovanN // recursive
Line 1,262 ⟶ 1,880:
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}}
Line 1,274 ⟶ 1,892:
7: 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349
8: 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362
</pre>
=={{header|XPL0}}==
{{trans|ALGOL W}}
<syntaxhighlight lang "XPL0">
\Show some values 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
\ maximum number of elements for each sequence
\MaxS must be >= 2
procedure PadovanSequences ( R, MaxS, MaxE) ;
integer R, MaxS, MaxE;
integer X, N, P;
function Min( A, B );
integer A, B;
return if A < B then A else B;
begin
\Sequence 2
for X := 1 to Min( MaxE, 3 ) do R( 2, X ) := 1;
for X := 4 to MaxE do R( 2, X ) := R( 2, X - 2 ) + R( 2, X - 3 );
\Sequences 3 and above
for N := 3 to MaxS do begin
for X := 1 to Min( MaxE, N + 1 ) do R( N, X ) := R( N - 1, X );
for X := N + 2 to MaxE do begin
R( N, X ) := 0;
for P := X - N - 1 to X - 2 do R( N, X ) := R( N, X ) + R( N, P )
end \for X
end \for_N
end; \PadovanSequences
def MAX_SEQUENCES = 8,
MAX_ELEMENTS = 15;
\Array to hold the Padovan Sequences
integer R( (2+MAX_SEQUENCES), (1+MAX_ELEMENTS)), N, X;
begin \Calculate and show the sequences
\Construct the sequences
PadovanSequences( R, MAX_SEQUENCES, MAX_ELEMENTS );
\Show the sequences
Text(0, "Padovan n-step sequences:^m^j" );
Format(4, 0);
for N := 2 to MAX_SEQUENCES do begin
IntOut(0, N); Text(0, " |");
for X := 1 to MAX_ELEMENTS do
RlOut(0, float(R( N, X )));
CrLf(0);
end \for N
end</syntaxhighlight>
{{out}}
<pre>
Padovan n-step sequences:
2 | 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
3 | 1 1 1 2 3 4 6 9 13 19 28 41 60 88 129
4 | 1 1 1 2 3 5 7 11 17 26 40 61 94 144 221
5 | 1 1 1 2 3 5 8 12 19 30 47 74 116 182 286
6 | 1 1 1 2 3 5 8 13 20 32 51 81 129 205 326
7 | 1 1 1 2 3 5 8 13 21 33 53 85 136 218 349
8 | 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362
</pre>
|