Padovan n-step number sequences: Difference between revisions

m
(Added FreeBasic)
m (→‎{{header|Wren}}: Minor tidy)
 
(9 intermediate revisions by 5 users not shown)
Line 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}}==
Line 577 ⟶ 856:
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#}}==
Line 595 ⟶ 957:
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}}
Line 619 ⟶ 982:
8 | 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362
</pre>
 
=={{header|FreeBASIC}}==
{{trans|C}}
Rosetta Code problem: https://rosettacode.org/wiki/Padovan_n-step_number_sequences
by Jjuanhdez, 05/2023
<syntaxhighlight lang="vb">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
Exit Sub
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|Go}}==
Line 800 ⟶ 1,110:
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.util.ArrayList;
import java.util.List;
Line 1,439 ⟶ 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>
 
Line 1,503 ⟶ 1,864:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var padovanN // recursive
9,476

edits