Fibonacci n-step number sequences: Difference between revisions

Added various BASIC dialects (Chipmunk Basic, QBasic, and QB64)
m (BASIC256 moved to the BASIC section.)
(Added various BASIC dialects (Chipmunk Basic, QBasic, and QB64))
 
(4 intermediate revisions by 4 users not shown)
Line 942:
decanacci => 1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1023, 2045, 4088, ...
lucas => 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, 322, 521, ...</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|BASIC256}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 sub fib(a())
110 erase f
120 dim f(24)
130 b = 0
140 for x = 1 to ubound(a)
150 b = b+1
160 f(x) = a(x)
170 next x
180 for i = b to 12+b
190 print using "#### ";f(i-b+1);
200 for j = (i-b+1) to i
210 f(i+1) = f(i+1)+f(j)
220 next j
230 next i
240 print
250 end sub
260 cls
270 print " fibonacci =>";
280 dim a(2)
290 a(1) = 1 : a(2) = 1
300 fib(a())
310 print " tribonacci =>";
320 dim a(3)
330 a(1) = 1 : a(2) = 1 : a(3) = 2
340 fib(a())
350 print " tetranacci =>";
360 dim c(4)
370 c(1) = 1 : c(2) = 1 : c(3) = 2 : c(4) = 4
380 fib(c())
390 print " lucas =>";
400 dim d(2)
410 d(1) = 2 : d(2) = 1
420 fib(d())
430 end</syntaxhighlight>
 
==={{header|QBasic}}===
{{trans|BASIC256}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|QB64}}
<syntaxhighlight lang="qbasic">DECLARE SUB fib (a() AS INTEGER)
 
CLS
PRINT " fibonacci =>";
DIM a(1 TO 2) AS INTEGER
a(1) = 1: a(2) = 1
CALL fib(a())
PRINT " tribonacci =>";
DIM b(1 TO 3) AS INTEGER
b(1) = 1: b(2) = 1: b(3) = 2
CALL fib(b())
PRINT " tetranacci =>";
DIM c(1 TO 4) AS INTEGER
c(1) = 1: c(2) = 1: c(3) = 2: c(4) = 4
CALL fib(c())
PRINT " lucas =>";
DIM d(1 TO 2) AS INTEGER
d(1) = 2: d(2) = 1
CALL fib(d())
END
 
SUB fib (a() AS INTEGER)
DIM f(24)
b = 0
FOR x = 1 TO UBOUND(a)
b = b + 1
f(x) = a(x)
NEXT x
FOR i = b TO 12 + b
PRINT USING "#### "; f(i - b + 1);
FOR j = (i - b + 1) TO i
f(i + 1) = f(i + 1) + f(j)
NEXT j
NEXT i
END SUB</syntaxhighlight>
 
==={{header|QB64}}===
{{trans|BASIC256}}
<syntaxhighlight lang="qbasic">Rem $Dynamic
 
Cls
Print " fibonacci =>";
Dim a(1 To 2) As Integer
a(1) = 1
a(2) = 1
Call fib(a())
Print " tribonacci =>";
ReDim _Preserve a(1 To 3)
a(3) = 2
Call fib(a())
Print " tetranacci =>";
ReDim _Preserve a(1 To 4)
a(4) = 4
Call fib(a())
Print " lucas =>";
ReDim a(1 To 2)
a(1) = 2
a(2) = 1
Call fib(a())
End
 
Sub fib (a() As Integer)
Dim f(24)
b = 0
For x = 1 To UBound(a)
b = b + 1
f(x) = a(x)
Next x
For i = b To 12 + b
Print Using "#### "; f(i - b + 1);
For j = (i - b + 1) To i
f(i + 1) = f(i + 1) + f(j)
Next j
Next i
End Sub</syntaxhighlight>
 
=={{header|Batch File}}==
Line 1,690 ⟶ 1,809:
=={{header|Delphi}}==
See [[#Pascal]].
=={{header|EasyLang}}==
<syntaxhighlight>
proc sequ n$ val[] n . .
write n$ & ": "
il = len val[]
len val[] n
for i = il + 1 to n
for j = 1 to il
val[i] += val[i - j]
.
.
for v in val[]
write v & " "
.
print ""
.
sequ "Fibonacci" [ 1 1 ] 10
sequ "Tribonacci" [ 1 1 2 ] 10
sequ "Tetrabonacci" [ 1 1 2 4 ] 10
sequ "Lucas" [ 2 1 ] 10
</syntaxhighlight>
{{out}}
<pre>
Fibonacci: 1 1 2 3 5 8 13 21 34 55
Tribonacci: 1 1 2 4 7 13 24 44 81 149
Tetrabonacci: 1 1 2 4 8 15 29 56 108 208
Lucas: 2 1 3 4 7 11 18 29 47 76
</pre>
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
Line 2,168 ⟶ 2,316:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Fibonacci_n-step_number_sequences}}
 
'''Solution'''
 
According to the requirements, the program must generate a series, and the order (Fibonacci, Tribonacci, etc) should be determined according with the initial values.
 
In this case, the number n indicates how many terms of the series will be generated.
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 01.png]]
 
The following generates a Fibonacci series of 15 terms:
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 02.png]]
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 03.png]]
 
The following generates a Lucas series of 15 terms:
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 04.png]]
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 05.png]]
 
The following generates a Tribonacci series of 15 terms:
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 06.png]]
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 07.png]]
 
'''Generating initial values.''' The initial values can be generated by the following function:
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 08.png]]
 
Note that it is a recursive function, and it calls the previously defined function. It requires the initial values as a seed: (1, 1) for Fibonacci style (Fibonacci, Tribonacci, etc), and (2, 1) for Lucas style.
 
The following generates the initial values for Fibonacci series.
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 09.png]]
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 10.png]]
 
The following generates the initial values for Lucas series.
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 11.png]]
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 12.png]]
 
'''Generating tables of series for Fibonacci and Lucas'''
 
This generates a tables of series for Fibonacci (15 terms), for orders 2 to 10 (Fibonacci, Tribonacci, etc.)
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 13.png]]
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 14.png]]
 
This generates a tables of series for Lucas (15 terms), for orders 2 to 15:
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 15.png]]
 
[[File:Fōrmulæ - Fibonacci n-step number sequences 16.png]]
 
=={{header|Go}}==
Line 4,266 ⟶ 4,472:
Lucas:
2 1 3 4 7 11 18 29 47 76 123 199 ...
</pre>
 
=={{header|RPL}}==
≪ OVER SIZE → len n
≪ LIST→
<span style="color:red">1</span> + len '''FOR''' j
n DUPN
<span style="color:red">2</span> n '''START''' + '''NEXT'''
'''NEXT''' len →LIST
≫ ≫ ‘<span style="color:blue">NFIB</span>’ STO
 
<span style="color:red">{1 1} 15</span> <span style="color:blue">NFIB</span>
DUP <span style="color:red">1 3</span> SUB <span style="color:red">15</span> <span style="color:blue">NFIB</span>
DUP <span style="color:red">1 4</span> SUB <span style="color:red">15</span> <span style="color:blue">NFIB</span>
<span style="color:red">{2 1} 15</span> <span style="color:blue">NFIB</span>
{{out}}
<pre>
4: { 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 }
3: { 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 }
2: { 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 }
1: { 2 1 3 4 7 11 18 29 47 76 123 199 322 521 843 }
</pre>
 
Line 4,912 ⟶ 5,139:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var fibN = Fn.new { |initial, numTerms|
2,122

edits