Fibonacci n-step number sequences: Difference between revisions

Added various BASIC dialects (Chipmunk Basic, QBasic, and QB64)
m (syntax highlighting fixup automation)
(Added various BASIC dialects (Chipmunk Basic, QBasic, and QB64))
 
(12 intermediate revisions by 10 users not shown)
Line 758:
decanacci: 0, 1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1023, 2045, 4088, 8172 …
Lucas: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, 322, 521, 843, 1364 …"</syntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">naccis: #[
lucas: [2 1]
fibonacci: [1 1]
tribonacci: [1 1 2]
tetranacci: [1 1 2 4]
pentanacci: [1 1 2 4 8]
hexanacci: [1 1 2 4 8 16]
heptanacci: [1 1 2 4 8 16 32]
octonacci: [1 1 2 4 8 16 32 64]
nonanacci: [1 1 2 4 8 16 32 64 128]
decanacci: [1 1 2 4 8 16 32 64 128 256]
]
 
anyNacci: function [start, count][
n: size start
result: new start
do.times: count-n ->
result: result ++ sum last.n:n result
 
return join.with:", " to [:string] result
]
 
loop naccis [k,v][
print [pad (k ++ ":") 12 anyNacci v 15]
]</syntaxhighlight>
 
{{out}}
 
<pre> lucas: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, 322, 521, 843
fibonacci: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610
tribonacci: 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136
tetranacci: 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, 2872, 5536
pentanacci: 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, 464, 912, 1793, 3525, 6930
hexanacci: 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, 492, 976, 1936, 3840, 7617
heptanacci: 1, 1, 2, 4, 8, 16, 32, 64, 127, 253, 504, 1004, 2000, 3984, 7936
octonacci: 1, 1, 2, 4, 8, 16, 32, 64, 128, 255, 509, 1016, 2028, 4048, 8080
nonanacci: 1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 511, 1021, 2040, 4076, 8144
decanacci: 1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1023, 2045, 4088, 8172</pre>
 
=={{header|AutoHotkey}}==
Line 838 ⟶ 878:
</pre>
 
=={{header|BASIC256BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256"># Rosetta Code problem: https://www.rosettacode.org/wiki/Fibonacci_n-step_number_sequences
# by Jjuanhdez, 06/2022
Line 901 ⟶ 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,649 ⟶ 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,126 ⟶ 2,315:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Fibonacci_n-step_number_sequences}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
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 '''[https://formulae.org/?example=Fibonacci_n-step_number_sequences this]''' page you can see the program(s) related to this task and their results.
 
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 2,230 ⟶ 2,473:
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import DataControl.ListMonad (tailszipWithM_)
import ControlData.MonadList (zipWithM_tails)
 
fiblike :: [Integer] -> [Integer]
fiblike st = xs where
where
xs = st ++ map (sum . take n) (tails xs)
xs = st <> map (sum . take n) (tails xs)
n = length st
n = length st
 
nstep :: Int -> [Integer]
nstep n = fiblike $ take n $ 1 : iterate (2 *) 1
 
main :: IO ()
main = do
mapM_ (print $. take 10 $. fiblike) [[1, 1], [2, 1]]
zipWithM_
print $ take 10 $ fiblike [2,1]
zipWithM_ ( \n name -> do putStr (name ++ "nacci -> ")
putStr (name <> "nacci -> ")
print $ take 15 $ nstep n)
print $ take 15 $ nstep n
[2..] (words "fibo tribo tetra penta hexa hepta octo nona deca")</syntaxhighlight>
)
[2 ..]
(words "fibo tribo tetra penta hexa hepta octo nona deca")</syntaxhighlight>
{{out}}
<pre>
Line 3,315 ⟶ 3,562:
writeln;
END.</syntaxhighlight>
 
=={{header|PascalABC.NET}}==
===Unfold===
I first define a high order function to generate infinite sequences given a lambda and a seed.
<syntaxhighlight lang="pascal">
// unfold infinite sequences. Nigel Galloway: September 8th., 2022
function unfold<gN,gG>(n:Func<gG,(gN,gG)>; g:gG): sequence of gN;
begin
var (x,r):=n(g);
yield x;
yield sequence unfold(n,r);
end;
function unfold<gN,gG>(n:Func<array of gG,(gN,array of gG)>;params g:array of gG): sequence of gN := unfold(n,g);
</syntaxhighlight>
 
===The Task===
Like the Pascal above but not iffy, not loopy, and not as long!
<syntaxhighlight lang="pascal">
// Fibonacci n-step number sequences. Nigel Galloway: September 8th., 2022
var nFib:=function(n:array of biginteger): (biginteger,array of biginteger)->(n.First,n[1:].Append(n.Sum).ToArray);
begin
var fib:=unfold(nFib,1bi,1bi);
fib.Take(20).Println;
var tri:=unfold(nFib,fib.Take(3));
tri.Take(20).Println;
var tet:=unfold(nFib,tri.Take(4));
tet.Take(20).Println;
var pen:=unfold(nFib,tet.Take(5));
pen.Take(20).Println;
var hex:=unfold(nFib,pen.Take(6));
hex.Take(20).Println;
var hep:=unfold(nFib,hex.Take(7));
hep.Take(20).Println;
var oct:=unfold(nFib,hep.Take(8));
oct.Take(20).Println;
var non:=unfold(nFib,oct.Take(9));
non.Take(20).Println;
var dec:=unfold(nFib,non.Take(10));
dec.Take(20).Println;
var luc:=unfold(nFib,2bi,1bi);
luc.Take(20).Println;
end.
</syntaxhighlight>
{{out}}
<pre>
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 5768 10609 19513 35890 66012
1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 10671 20569 39648 76424 147312
1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 13624 26784 52656 103519 203513
1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617 15109 29970 59448 117920 233904
1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936 15808 31489 62725 124946 248888
1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080 16128 32192 64256 128257 256005
1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 16272 32512 64960 129792 259328
1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 16336 32656 65280 130496 260864
2 1 3 4 7 11 18 29 47 76 123 199 322 521 843 1364 2207 3571 5778 9349
</pre>
 
=={{header|Perl}}==
Line 4,169 ⟶ 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,439 ⟶ 4,763:
var len = xs.len
len >= k && break
xs << xs.ftslice(max(0, len - n)).sum
}
return xs
Line 4,760 ⟶ 5,084:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Wren}}
<syntaxhighlight lang="v (vlang)">fn fib_n(initial []int, num_terms int) []int {
n := initial.len
if n < 2 || num_terms < 0 {panic("Invalid argument(s).")}
Line 4,815 ⟶ 5,139:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var fibN = Fn.new { |initial, numTerms|
2,122

edits