Fibonacci n-step number sequences: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 63: | Line 63: | ||
{{trans|Python: Callable class}} |
{{trans|Python: Callable class}} |
||
< |
<syntaxhighlight lang="11l">T Fiblike |
||
Int addnum |
Int addnum |
||
[Int] memo |
[Int] memo |
||
Line 87: | Line 87: | ||
L(n, name) zip(2..10, ‘fibo tribo tetra penta hexa hepta octo nona deca’.split(‘ ’)) |
L(n, name) zip(2..10, ‘fibo tribo tetra penta hexa hepta octo nona deca’.split(‘ ’)) |
||
V fibber = Fiblike([1] [+] (0 .< n - 1).map(i -> Int(2 ^ i))) |
V fibber = Fiblike([1] [+] (0 .< n - 1).map(i -> Int(2 ^ i))) |
||
print(‘n=#2, #5nacci -> #. ...’.format(n, name, (0.<15).map(i -> String(@fibber(i))).join(‘ ’)))</ |
print(‘n=#2, #5nacci -> #. ...’.format(n, name, (0.<15).map(i -> String(@fibber(i))).join(‘ ’)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 105: | Line 105: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
< |
<syntaxhighlight lang="360asm">* Fibonacci n-step number sequences - 14/04/2020 |
||
FIBONS CSECT |
FIBONS CSECT |
||
USING FIBONS,R13 base register |
USING FIBONS,R13 base register |
||
Line 192: | Line 192: | ||
PG DS CL120 buffer |
PG DS CL120 buffer |
||
REGEQU |
REGEQU |
||
END FIBONS</ |
END FIBONS</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 204: | Line 204: | ||
=={{header|ACL2}}== |
=={{header|ACL2}}== |
||
< |
<syntaxhighlight lang="lisp">(defun sum (xs) |
||
(if (endp xs) |
(if (endp xs) |
||
0 |
0 |
||
Line 216: | Line 216: | ||
(list (sum prevs))))) |
(list (sum prevs))))) |
||
(cons (first next) |
(cons (first next) |
||
(n-bonacci next (1- limit))))))</ |
(n-bonacci next (1- limit))))))</syntaxhighlight> |
||
Output: |
Output: |
||
Line 229: | Line 229: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">DEFINE MAX="15" |
||
PROC GenerateSeq(CARD ARRAY init BYTE nInit CARD ARRAY seq BYTE nSeq) |
PROC GenerateSeq(CARD ARRAY init BYTE nInit CARD ARRAY seq BYTE nSeq) |
||
Line 303: | Line 303: | ||
Test("nonanacci",fibInit,9,MAX) |
Test("nonanacci",fibInit,9,MAX) |
||
Test("decanacci",fibInit,10,MAX) |
Test("decanacci",fibInit,10,MAX) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Fibonacci_n-step_number_sequences.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Fibonacci_n-step_number_sequences.png Screenshot from Atari 8-bit computer] |
||
Line 323: | Line 323: | ||
First, we specify a package Bonacci, that defines the type Sequence (of Positive numbers), a function Generate that takes a given Start sequence and outputs a generalized N-Bonacci Sequence of a spefified Length, and some constant start sequences. |
First, we specify a package Bonacci, that defines the type Sequence (of Positive numbers), a function Generate that takes a given Start sequence and outputs a generalized N-Bonacci Sequence of a spefified Length, and some constant start sequences. |
||
< |
<syntaxhighlight lang="ada">package Bonacci is |
||
type Sequence is array(Positive range <>) of Positive; |
type Sequence is array(Positive range <>) of Positive; |
||
Line 333: | Line 333: | ||
Start_Tetranacci: constant Sequence := (1, 1, 2, 4); |
Start_Tetranacci: constant Sequence := (1, 1, 2, 4); |
||
Start_Lucas: constant Sequence := (2, 1); |
Start_Lucas: constant Sequence := (2, 1); |
||
end Bonacci;</ |
end Bonacci;</syntaxhighlight> |
||
The implementation is quite straightforward. |
The implementation is quite straightforward. |
||
< |
<syntaxhighlight lang="ada">package body Bonacci is |
||
function Generate(Start: Sequence; Length: Positive := 10) return Sequence is |
function Generate(Start: Sequence; Length: Positive := 10) return Sequence is |
||
Line 356: | Line 356: | ||
end Generate; |
end Generate; |
||
end Bonacci;</ |
end Bonacci;</syntaxhighlight> |
||
Finally, we actually generate some sequences, as required by the task. For convenience, we define a procedure Print that outputs a sequence, |
Finally, we actually generate some sequences, as required by the task. For convenience, we define a procedure Print that outputs a sequence, |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO, Bonacci; |
||
procedure Test_Bonacci is |
procedure Test_Bonacci is |
||
Line 380: | Line 380: | ||
Print("Decanacci: ", |
Print("Decanacci: ", |
||
Bonacci.Generate((1, 1, 2, 4, 8, 16, 32, 64, 128, 256), 15)); |
Bonacci.Generate((1, 1, 2, 4, 8, 16, 32, 64, 128, 256), 15)); |
||
end Test_Bonacci;</ |
end Test_Bonacci;</syntaxhighlight> |
||
The output: |
The output: |
||
Line 391: | Line 391: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68"># returns an array of the first required count elements of an a n-step fibonacci sequence # |
||
# the initial values are taken from the init array # |
# the initial values are taken from the init array # |
||
PROC n step fibonacci sequence = ( []INT init, INT required count )[]INT: |
PROC n step fibonacci sequence = ( []INT init, INT required count )[]INT: |
||
Line 421: | Line 421: | ||
print sequence( "tetrabonacci", n step fibonacci sequence( ( 1, 1, 2, 4 ), 10 ) ); |
print sequence( "tetrabonacci", n step fibonacci sequence( ( 1, 1, 2, 4 ), 10 ) ); |
||
print sequence( "lucus ", n step fibonacci sequence( ( 2, 1 ), 10 ) ) |
print sequence( "lucus ", n step fibonacci sequence( ( 2, 1 ), 10 ) ) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 432: | Line 432: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">nStep ← {⊃(1↓⊢,+/)⍣(⍺-1)⊢⍵} |
||
nacci ← 2*0⌈¯2+⍳ |
nacci ← 2*0⌈¯2+⍳ |
||
↑((⍳10)nStep¨⊂)¨(nacci¨2 3 4),⊂2 1</ |
↑((⍳10)nStep¨⊂)¨(nacci¨2 3 4),⊂2 1</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 1 2 3 5 8 13 21 34 55 |
<pre>1 1 2 3 5 8 13 21 34 55 |
||
Line 443: | Line 443: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
===Functional=== |
===Functional=== |
||
< |
<syntaxhighlight lang="applescript">use AppleScript version "2.4" |
||
use framework "Foundation" |
use framework "Foundation" |
||
use scripting additions |
use scripting additions |
||
Line 687: | Line 687: | ||
end tell |
end tell |
||
end zipWith |
end zipWith |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>Lucas -> [2,1,3,4,7,11,18,29,47,76,123,199,322,521,843] |
<pre>Lucas -> [2,1,3,4,7,11,18,29,47,76,123,199,322,521,843] |
||
Line 702: | Line 702: | ||
===Simple=== |
===Simple=== |
||
< |
<syntaxhighlight lang="applescript">-- Parameters: |
||
-- n: …nacci step size as integer. Alternatively "Lucas". |
-- n: …nacci step size as integer. Alternatively "Lucas". |
||
-- F: Maximum …nacci index required. (0-based.) |
-- F: Maximum …nacci index required. (0-based.) |
||
Line 745: | Line 745: | ||
set AppleScript's text item delimiters to astid |
set AppleScript's text item delimiters to astid |
||
return output</ |
return output</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang="applescript">"fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 … |
||
tribonacci: 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136 … |
tribonacci: 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136 … |
||
tetranacci: 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, 2872, 5536 … |
tetranacci: 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, 2872, 5536 … |
||
Line 757: | Line 757: | ||
nonanacci: 0, 1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 511, 1021, 2040, 4076, 8144 … |
nonanacci: 0, 1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 511, 1021, 2040, 4076, 8144 … |
||
decanacci: 0, 1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1023, 2045, 4088, 8172 … |
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 …"</ |
Lucas: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, 322, 521, 843, 1364 …"</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">for i, seq in ["nacci", "lucas"] |
||
Loop, 9 { |
Loop, 9 { |
||
Out .= seq "(" A_Index + 1 "): " |
Out .= seq "(" A_Index + 1 "): " |
||
Line 776: | Line 776: | ||
} |
} |
||
return, a |
return, a |
||
}</ |
}</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
<pre>nacci(2): 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 |
<pre>nacci(2): 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 |
||
Line 798: | Line 798: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
function sequence(values, howmany) { |
function sequence(values, howmany) { |
||
init_length = length(values) |
init_length = length(values) |
||
Line 829: | Line 829: | ||
print("lucas :\t\t",sequence(a, 10)) |
print("lucas :\t\t",sequence(a, 10)) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 839: | Line 839: | ||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
< |
<syntaxhighlight lang="basic256"># Rosetta Code problem: https://www.rosettacode.org/wiki/Fibonacci_n-step_number_sequences |
||
# by Jjuanhdez, 06/2022 |
# by Jjuanhdez, 06/2022 |
||
Line 889: | Line 889: | ||
next j |
next j |
||
next i |
next i |
||
end subroutine</ |
end subroutine</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> fibonacci => 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ... |
<pre> fibonacci => 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ... |
||
Line 903: | Line 903: | ||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="dos"> |
||
@echo off |
@echo off |
||
Line 952: | Line 952: | ||
endlocal |
endlocal |
||
exit /b |
exit /b |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 970: | Line 970: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
The BBC BASIC '''SUM''' function is useful here. |
The BBC BASIC '''SUM''' function is useful here. |
||
< |
<syntaxhighlight lang="bbcbasic"> @% = 5 : REM Column width |
||
PRINT "Fibonacci:" |
PRINT "Fibonacci:" |
||
Line 996: | Line 996: | ||
NEXT |
NEXT |
||
f%(i%-1) = s% |
f%(i%-1) = s% |
||
ENDPROC</ |
ENDPROC</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 1,011: | Line 1,011: | ||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
< |
<syntaxhighlight lang="befunge">110p>>55+109"iccanaceD"22099v |
||
v9013"Tetranacci"9014"Lucas"< |
v9013"Tetranacci"9014"Lucas"< |
||
>"iccanobirT"2109"iccanobiF"v |
>"iccanobirT"2109"iccanobiF"v |
||
Line 1,018: | Line 1,018: | ||
_$.1+:77+`^vg03:_0g+>\:1+#^ |
_$.1+:77+`^vg03:_0g+>\:1+#^ |
||
50p-\30v v\<>\30g1-\^$$_:1- |
50p-\30v v\<>\30g1-\^$$_:1- |
||
05g04\g< >`#^_:40p30g0>^!:g</ |
05g04\g< >`#^_:40p30g0>^!:g</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,030: | Line 1,030: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
{{trans|PicoLisp}} |
{{trans|PicoLisp}} |
||
< |
<syntaxhighlight lang="bracmat">( ( nacci |
||
= Init Cnt N made tail |
= Init Cnt N made tail |
||
. ( plus |
. ( plus |
||
Line 1,070: | Line 1,070: | ||
& out$(str$(pad$!name ": ") nacci$(!Init.12)) |
& out$(str$(pad$!name ": ") nacci$(!Init.12)) |
||
) |
) |
||
);</ |
);</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> fibonacci: 1 1 2 3 5 8 13 21 34 55 89 144 |
<pre> fibonacci: 1 1 2 3 5 8 13 21 34 55 89 144 |
||
Line 1,084: | Line 1,084: | ||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
< |
<syntaxhighlight lang="bqn">NStep ← ⊑(1↓⊢∾+´)∘⊢⍟⊣ |
||
Nacci ← (2⋆0∾↕)∘(⊢-1˙) |
Nacci ← (2⋆0∾↕)∘(⊢-1˙) |
||
>((↕10) NStep¨ <)¨ (Nacci¨ 2‿3‿4) ∾ <2‿1</ |
>((↕10) NStep¨ <)¨ (Nacci¨ 2‿3‿4) ∾ <2‿1</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>┌─ |
<pre>┌─ |
||
Line 1,097: | Line 1,097: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">/* |
||
The function anynacci determines the n-arity of the sequence from the number of seed elements. 0 ended arrays are used since C does not have a way of determining the length of dynamic and function-passed integer arrays.*/ |
The function anynacci determines the n-arity of the sequence from the number of seed elements. 0 ended arrays are used since C does not have a way of determining the length of dynamic and function-passed integer arrays.*/ |
||
Line 1,139: | Line 1,139: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,158: | Line 1,158: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 1,241: | Line 1,241: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>Fibonacci 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, |
<pre>Fibonacci 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, |
||
Lucas 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, |
Lucas 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, |
||
Line 1,248: | Line 1,248: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <vector> |
||
#include <iostream> |
#include <iostream> |
||
#include <numeric> |
#include <numeric> |
||
Line 1,297: | Line 1,297: | ||
} |
} |
||
return 0 ; |
return 0 ; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>fibonacci : 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ... |
<pre>fibonacci : 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ... |
||
Line 1,322: | Line 1,322: | ||
This version focuses on a clean, simple class that adapts to any pair of starting numbers and any order. Rather than summing over all history every time, it uses an O(1) incremental update to a running total. Thus, performance remains essentially unchanged even for very large orders. |
This version focuses on a clean, simple class that adapts to any pair of starting numbers and any order. Rather than summing over all history every time, it uses an O(1) incremental update to a running total. Thus, performance remains essentially unchanged even for very large orders. |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 1,388: | Line 1,388: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn nacci [init] |
||
(letfn [(s [] (lazy-cat init (apply map + (map #(drop % (s)) (range (count init))))))] |
(letfn [(s [] (lazy-cat init (apply map + (map #(drop % (s)) (range (count init))))))] |
||
(s))) |
(s))) |
||
Line 1,399: | Line 1,399: | ||
(show "Tribonacci" [1 1 2]) |
(show "Tribonacci" [1 1 2]) |
||
(show "Tetranacci" [1 1 2 4]) |
(show "Tetranacci" [1 1 2 4]) |
||
(show "Lucas" [2 1]))</ |
(show "Lucas" [2 1]))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>first 20 Fibonacci (1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765) |
<pre>first 20 Fibonacci (1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765) |
||
Line 1,407: | Line 1,407: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">% Find the Nth element of a given n-step sequence |
||
n_step = proc (seq: sequence[int], n: int) returns (int) |
n_step = proc (seq: sequence[int], n: int) returns (int) |
||
a: array[int] := sequence[int]$s2a(seq) |
a: array[int] := sequence[int]$s2a(seq) |
||
Line 1,453: | Line 1,453: | ||
print_n(seq.seq, 10) |
print_n(seq.seq, 10) |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Fibonacci 1 1 2 3 5 8 13 21 34 55 |
<pre>Fibonacci 1 1 2 3 5 8 13 21 34 55 |
||
Line 1,461: | Line 1,461: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun gen-fib (lst m) |
(defun gen-fib (lst m) |
||
"Return the first m members of a generalized Fibonacci sequence using lst as initial values |
"Return the first m members of a generalized Fibonacci sequence using lst as initial values |
||
Line 1,479: | Line 1,479: | ||
(format t "Lucas series: ~a~%" (gen-fib '(2 1) 10)) |
(format t "Lucas series: ~a~%" (gen-fib '(2 1) 10)) |
||
(loop for i from 2 to 4 |
(loop for i from 2 to 4 |
||
do (format t "Fibonacci ~a-step sequence: ~a~%" i (gen-fib (initial-values i) 10))))</ |
do (format t "Fibonacci ~a-step sequence: ~a~%" i (gen-fib (initial-values i) 10))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Lucas series: (2 1 3 4 7 11 18 29 47 76) |
<pre>Lucas series: (2 1 3 4 7 11 18 29 47 76) |
||
Line 1,488: | Line 1,488: | ||
=={{header|D}}== |
=={{header|D}}== |
||
===Basic Memoization=== |
===Basic Memoization=== |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.algorithm, std.range, std.conv; |
import std.stdio, std.algorithm, std.range, std.conv; |
||
Line 1,516: | Line 1,516: | ||
15.iota.map!fibber); |
15.iota.map!fibber); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
<pre>[1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
||
Line 1,532: | Line 1,532: | ||
===Callable Struct=== |
===Callable Struct=== |
||
The output is similar. |
The output is similar. |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.conv; |
||
struct fiblike(T) { |
struct fiblike(T) { |
||
Line 1,566: | Line 1,566: | ||
n, name, 15.iota.map!fib); |
n, name, 15.iota.map!fib); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
===Struct With opApply=== |
===Struct With opApply=== |
||
The output is similar. |
The output is similar. |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.traits; |
||
struct Fiblike(T) { |
struct Fiblike(T) { |
||
Line 1,613: | Line 1,613: | ||
writefln("n=%2d, %5snacci -> %s", n, name, fib.takeApply(15)); |
writefln("n=%2d, %5snacci -> %s", n, name, fib.takeApply(15)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
===Range Generator Version=== |
===Range Generator Version=== |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.algorithm, std.range, std.concurrency; |
import std.stdio, std.algorithm, std.range, std.concurrency; |
||
Line 1,634: | Line 1,634: | ||
writefln("n=%2d, %5snacci -> %(%s, %), ...", n, name, fib.take(15)); |
writefln("n=%2d, %5snacci -> %(%s, %), ...", n, name, fib.take(15)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
<pre>[1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
||
Line 1,650: | Line 1,650: | ||
See [[#Pascal]]. |
See [[#Pascal]]. |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
;; generate a recursive lambda() for a x-nacci |
;; generate a recursive lambda() for a x-nacci |
||
;; equip it with memoïzation |
;; equip it with memoïzation |
||
Line 1,674: | Line 1,674: | ||
(make-nacci name seed) |
(make-nacci name seed) |
||
(printf "%s[%d] → %d" name (vector-length seed) (take name 16)))) |
(printf "%s[%d] → %d" name (vector-length seed) (take name 16)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,690: | Line 1,690: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="elixir">defmodule RC do |
||
def anynacci(start_sequence, count) do |
def anynacci(start_sequence, count) do |
||
n = length(start_sequence) |
n = length(start_sequence) |
||
Line 1,718: | Line 1,718: | ||
:io.format("~11s: ", [name]) |
:io.format("~11s: ", [name]) |
||
IO.inspect RC.anynacci(list, 15) |
IO.inspect RC.anynacci(list, 15) |
||
end)</ |
end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,735: | Line 1,735: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
-module( fibonacci_nstep ). |
-module( fibonacci_nstep ). |
||
Line 1,757: | Line 1,757: | ||
{Sum_ns, _Not_sum_ns} = lists:split( Nth, Ns ), |
{Sum_ns, _Not_sum_ns} = lists:split( Nth, Ns ), |
||
{Nth, [lists:sum(Sum_ns) | Ns]}. |
{Nth, [lists:sum(Sum_ns) | Ns]}. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,768: | Line 1,768: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROGRAM FIBON |
PROGRAM FIBON |
||
Line 1,812: | Line 1,812: | ||
FIB("Lucas","2,1") |
FIB("Lucas","2,1") |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">let fibinit = Seq.append (Seq.singleton 1) (Seq.unfold (fun n -> Some(n, 2*n)) 1) |
||
let fiblike init = |
let fiblike init = |
||
Line 1,838: | Line 1,838: | ||
(Seq.init prefix.Length (fun i -> (prefix.[i], i+2))) |
(Seq.init prefix.Length (fun i -> (prefix.[i], i+2))) |
||
printfn " lucas -> %A" (start (fiblike [2; 1])) |
printfn " lucas -> %A" (start (fiblike [2; 1])) |
||
0</ |
0</syntaxhighlight> |
||
Output |
Output |
||
<pre>n= 2, fibonacci -> [1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89; 144; 233; 377; 610] |
<pre>n= 2, fibonacci -> [1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89; 144; 233; 377; 610] |
||
Line 1,853: | Line 1,853: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<code>building</code> is a dynamic variable that refers to the sequence being built by <code>make</code>. This is useful when the next element of the sequence depends on previous elements. |
<code>building</code> is a dynamic variable that refers to the sequence being built by <code>make</code>. This is useful when the next element of the sequence depends on previous elements. |
||
< |
<syntaxhighlight lang="factor">USING: formatting fry kernel make math namespaces qw sequences ; |
||
: n-bonacci ( n initial -- seq ) [ |
: n-bonacci ( n initial -- seq ) [ |
||
Line 1,862: | Line 1,862: | ||
qw{ fibonacci tribonacci tetranacci lucas } |
qw{ fibonacci tribonacci tetranacci lucas } |
||
{ { 1 1 } { 1 1 2 } { 1 1 2 4 } { 2 1 } } |
{ { 1 1 } { 1 1 2 } { 1 1 2 4 } { 2 1 } } |
||
[ 10 swap n-bonacci "%-10s %[%3d, %]\n" printf ] 2each</ |
[ 10 swap n-bonacci "%-10s %[%3d, %]\n" printf ] 2each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,872: | Line 1,872: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: length @ ; \ length of an array is stored at its address |
||
: a{ here cell allot ; |
: a{ here cell allot ; |
||
: } , here over - cell / over ! ; |
: } , here over - cell / over ! ; |
||
Line 1,896: | Line 1,896: | ||
." tetranacci: " a{ 1 , 1 , 2 , 4 } show-nacci |
." tetranacci: " a{ 1 , 1 , 2 , 4 } show-nacci |
||
." lucas: " a{ 2 , 1 } show-nacci |
." lucas: " a{ 2 , 1 } show-nacci |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>fibonacci: 1 1 2 3 5 8 13 21 34 55 |
<pre>fibonacci: 1 1 2 3 5 8 13 21 34 55 |
||
Line 1,905: | Line 1,905: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
< |
<syntaxhighlight lang="fortran"> |
||
! save this program as file f.f08 |
! save this program as file f.f08 |
||
! gnu-linux command to build and test |
! gnu-linux command to build and test |
||
Line 1,970: | Line 1,970: | ||
end subroutine nacci |
end subroutine nacci |
||
end program f |
end program f |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 2,012: | Line 2,012: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
' Deduces the step, n, from the length of the dynamic array passed in |
' Deduces the step, n, from the length of the dynamic array passed in |
||
Line 2,062: | Line 2,062: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,073: | Line 2,073: | ||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
< |
<syntaxhighlight lang="funl">import util.TextTable |
||
native scala.collection.mutable.Queue |
native scala.collection.mutable.Queue |
||
Line 2,103: | Line 2,103: | ||
t.row( ([k] + [seqs(i)(k) | i <- 0:4]).toIndexedSeq() ) |
t.row( ([k] + [seqs(i)(k) | i <- 0:4]).toIndexedSeq() ) |
||
print( t )</ |
print( t )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,134: | Line 2,134: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
Solution using separate goroutines. |
Solution using separate goroutines. |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 2,172: | Line 2,172: | ||
fmt.Println() |
fmt.Println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,183: | Line 2,183: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
=====Solution===== |
=====Solution===== |
||
< |
<syntaxhighlight lang="groovy">def fib = { List seed, int k=10 -> |
||
assert seed : "The seed list must be non-null and non-empty" |
assert seed : "The seed list must be non-null and non-empty" |
||
assert seed.every { it instanceof Number } : "Every member of the seed must be a number" |
assert seed.every { it instanceof Number } : "Every member of the seed must be a number" |
||
Line 2,196: | Line 2,196: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=====Test===== |
=====Test===== |
||
< |
<syntaxhighlight lang="groovy">[ |
||
' fibonacci':[1,1], |
' fibonacci':[1,1], |
||
'tribonacci':[1,1,2], |
'tribonacci':[1,1,2], |
||
Line 2,214: | Line 2,214: | ||
println " lucas[0]: ${fib([2,1],0)}" |
println " lucas[0]: ${fib([2,1],0)}" |
||
println " tetra[3]: ${fib([1,1,2,4],3)}"</ |
println " tetra[3]: ${fib([1,1,2,4],3)}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> fibonacci: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
<pre> fibonacci: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
||
Line 2,230: | Line 2,230: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (tails) |
||
import Control.Monad (zipWithM_) |
import Control.Monad (zipWithM_) |
||
Line 2,247: | Line 2,247: | ||
zipWithM_ (\n name -> do putStr (name ++ "nacci -> ") |
zipWithM_ (\n name -> do putStr (name ++ "nacci -> ") |
||
print $ take 15 $ nstep n) |
print $ take 15 $ nstep n) |
||
[2..] (words "fibo tribo tetra penta hexa hepta octo nona deca")</ |
[2..] (words "fibo tribo tetra penta hexa hepta octo nona deca")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,264: | Line 2,264: | ||
Or alternatively, without imports – using only the default Prelude: |
Or alternatively, without imports – using only the default Prelude: |
||
< |
<syntaxhighlight lang="haskell">------------ FIBONACCI N-STEP NUMBER SEQUENCES ----------- |
||
nStepFibonacci :: Int -> [Int] |
nStepFibonacci :: Int -> [Int] |
||
Line 2,296: | Line 2,296: | ||
justifyLeft :: Int -> Char -> String -> String |
justifyLeft :: Int -> Char -> String -> String |
||
justifyLeft n c s = take n (s <> replicate n c)</ |
justifyLeft n c s = take n (s <> replicate n c)</syntaxhighlight> |
||
<pre>Lucas -> [2,1,3,4,7,11,18,29,47,76,123,199,322,521,843] |
<pre>Lucas -> [2,1,3,4,7,11,18,29,47,76,123,199,322,521,843] |
||
fibonaccci -> [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610] |
fibonaccci -> [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610] |
||
Line 2,310: | Line 2,310: | ||
or in terms of '''unfoldr''': |
or in terms of '''unfoldr''': |
||
{{Trans|Python}} |
{{Trans|Python}} |
||
< |
<syntaxhighlight lang="haskell">import Data.Bifunctor (second) |
||
import Data.List (transpose, uncons, unfoldr) |
import Data.List (transpose, uncons, unfoldr) |
||
Line 2,367: | Line 2,367: | ||
justifyRight :: Int -> Char -> String -> String |
justifyRight :: Int -> Char -> String -> String |
||
justifyRight n c = (drop . length) <*> (replicate n c <>)</ |
justifyRight n c = (drop . length) <*> (replicate n c <>)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Recurrence relation sequences: |
<pre>Recurrence relation sequences: |
||
Line 2,385: | Line 2,385: | ||
Works in both languages: |
Works in both languages: |
||
< |
<syntaxhighlight lang="unicon">procedure main(A) |
||
every writes("F2:\t"|right((fnsGen(1,1))\14,5) | "\n") |
every writes("F2:\t"|right((fnsGen(1,1))\14,5) | "\n") |
||
every writes("F3:\t"|right((fnsGen(1,1,2))\14,5) | "\n") |
every writes("F3:\t"|right((fnsGen(1,1,2))\14,5) | "\n") |
||
Line 2,399: | Line 2,399: | ||
suspend cache[i] |
suspend cache[i] |
||
} |
} |
||
end</ |
end</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,414: | Line 2,414: | ||
A slightly longer version of <tt>fnsGen</tt> that reduces the memory |
A slightly longer version of <tt>fnsGen</tt> that reduces the memory |
||
footprint is: |
footprint is: |
||
< |
<syntaxhighlight lang="unicon">procedure fnsGen(cache[]) |
||
every i := seq() do { |
every i := seq() do { |
||
if i := (i > *cache, *cache) then { |
if i := (i > *cache, *cache) then { |
||
Line 2,423: | Line 2,423: | ||
suspend cache[i] |
suspend cache[i] |
||
} |
} |
||
end</ |
end</syntaxhighlight> |
||
The output is identical. |
The output is identical. |
||
Line 2,429: | Line 2,429: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution''':< |
'''Solution''':<syntaxhighlight lang="j"> nacci =: (] , +/@{.)^:(-@#@]`(-#)`])</syntaxhighlight> |
||
'''Example''' ''(Lucas)'':< |
'''Example''' ''(Lucas)'':<syntaxhighlight lang="j"> 10 nacci 2 1 NB. Lucas series, first 10 terms |
||
2 1 3 4 7 11 18 29 47 76</ |
2 1 3 4 7 11 18 29 47 76</syntaxhighlight> |
||
'''Example''' ''(extended 'nacci series)'':< |
'''Example''' ''(extended 'nacci series)'':<syntaxhighlight lang="j"> TESTS =: }."1 fixdsv noun define [ require 'tables/dsv' NB. Tests from task description |
||
2 fibonacci 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ... |
2 fibonacci 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ... |
||
3 tribonacci 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 ... |
3 tribonacci 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 ... |
||
Line 2,454: | Line 2,454: | ||
octonacci ✓ |
octonacci ✓ |
||
nonanacci ✓ |
nonanacci ✓ |
||
decanacci ✓</ |
decanacci ✓</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 2,460: | Line 2,460: | ||
'''Code:''' |
'''Code:''' |
||
< |
<syntaxhighlight lang="java">class Fibonacci |
||
{ |
{ |
||
public static int[] lucas(int n, int numRequested) |
public static int[] lucas(int n, int numRequested) |
||
Line 2,504: | Line 2,504: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,528: | Line 2,528: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===ES5=== |
===ES5=== |
||
< |
<syntaxhighlight lang="javascript">function fib(arity, len) { |
||
return nacci(nacci([1,1], arity, arity), arity, len); |
return nacci(nacci([1,1], arity, arity), arity, len); |
||
} |
} |
||
Line 2,553: | Line 2,553: | ||
} |
} |
||
main();</ |
main();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>fib(2): 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610 |
<pre>fib(2): 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610 |
||
Line 2,575: | Line 2,575: | ||
===ES6=== |
===ES6=== |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 2,688: | Line 2,688: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
<pre>Lucas -> [2,1,3,4,7,11,18,29,47,76,123,199,322,521,843] |
<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] |
fibonacci -> [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610] |
||
Line 2,702: | Line 2,702: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{Works with|jq|1.4}} |
{{Works with|jq|1.4}} |
||
< |
<syntaxhighlight lang="jq"># Input: the initial array |
||
def nacci(arity; len): |
def nacci(arity; len): |
||
arity as $arity | len as $len |
arity as $arity | len as $len |
||
Line 2,716: | Line 2,716: | ||
def lucas(arity; len): |
def lucas(arity; len): |
||
arity as $arity | len as $len |
arity as $arity | len as $len |
||
| [2,1] | nacci($arity; $arity) | nacci($arity; $len) ;</ |
| [2,1] | nacci($arity; $arity) | nacci($arity; $len) ;</syntaxhighlight> |
||
'''Example''': |
'''Example''': |
||
< |
<syntaxhighlight lang="jq">def main: |
||
(range(2; 11) | "fib(\(.)): \(fib(.; 15))"), |
(range(2; 11) | "fib(\(.)): \(fib(.; 15))"), |
||
(range(2; 11) | "lucas(\(.)): \(lucas(.; 15))") |
(range(2; 11) | "lucas(\(.)): \(lucas(.; 15))") |
||
; |
; |
||
main</ |
main</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
$ jq -M -r -n -f fibonacci_n-step.jq |
$ jq -M -r -n -f fibonacci_n-step.jq |
||
Line 2,734: | Line 2,734: | ||
'''Generalized Fibonacci Iterator Definition''' |
'''Generalized Fibonacci Iterator Definition''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
type NFib{T<:Integer} |
type NFib{T<:Integer} |
||
n::T |
n::T |
||
Line 2,765: | Line 2,765: | ||
return (f, fs) |
return (f, fs) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Specification of the n-step Fibonacci Iterator''' |
'''Specification of the n-step Fibonacci Iterator''' |
||
The seeding for this series of sequences is <math>F_{1-n} = 1</math> and <math>F_{2-n} \ldots F_{0}=0</math>. |
The seeding for this series of sequences is <math>F_{1-n} = 1</math> and <math>F_{2-n} \ldots F_{0}=0</math>. |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
function fib_seeder{T<:Integer}(n::T) |
function fib_seeder{T<:Integer}(n::T) |
||
a = zeros(BigInt, n) |
a = zeros(BigInt, n) |
||
Line 2,780: | Line 2,780: | ||
NFib(n, k, fib_seeder) |
NFib(n, k, fib_seeder) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Specification of the Rosetta Code n-step Lucas Iterator''' |
'''Specification of the Rosetta Code n-step Lucas Iterator''' |
||
This iterator produces the task description's version of the Lucas Sequence ([https://oeis.org/A000032 OEIS A000032]) and its generalization to n-steps as was done by some of the other solutions to this task. The seeding for this series of sequences is <math>F_{1-n} = 3</math>, <math>F_{2-n} = -1</math> and, for <math>n > 2</math>, <math>F_{3-n} \ldots F_{0}=0</math>. |
This iterator produces the task description's version of the Lucas Sequence ([https://oeis.org/A000032 OEIS A000032]) and its generalization to n-steps as was done by some of the other solutions to this task. The seeding for this series of sequences is <math>F_{1-n} = 3</math>, <math>F_{2-n} = -1</math> and, for <math>n > 2</math>, <math>F_{3-n} \ldots F_{0}=0</math>. |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
function luc_rc_seeder{T<:Integer}(n::T) |
function luc_rc_seeder{T<:Integer}(n::T) |
||
a = zeros(BigInt, n) |
a = zeros(BigInt, n) |
||
Line 2,796: | Line 2,796: | ||
NFib(n, k, luc_rc_seeder) |
NFib(n, k, luc_rc_seeder) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Specification of the MathWorld n-step Lucas Iterator''' |
'''Specification of the MathWorld n-step Lucas Iterator''' |
||
This iterator produces the Mathworld version of the Lucas Sequence ([http://mathworld.wolfram.com/LucasNumber.html Lucas Number] and [https://oeis.org/A000204 OEIS A000204]) and its generalization to n-steps according to Mathworld ([http://mathworld.wolfram.com/Lucasn-StepNumber.html Lucas n-Step Number] and [https://cs.uwaterloo.ca/journals/JIS/VOL8/Noe/noe5.html Primes in Fibonacci n-step and Lucas n-step Sequences]). The seeding for this series of sequences is <math>F_{0} = n</math> and <math>F_{1-n} \ldots F_{-1}=-1</math>. |
This iterator produces the Mathworld version of the Lucas Sequence ([http://mathworld.wolfram.com/LucasNumber.html Lucas Number] and [https://oeis.org/A000204 OEIS A000204]) and its generalization to n-steps according to Mathworld ([http://mathworld.wolfram.com/Lucasn-StepNumber.html Lucas n-Step Number] and [https://cs.uwaterloo.ca/journals/JIS/VOL8/Noe/noe5.html Primes in Fibonacci n-step and Lucas n-step Sequences]). The seeding for this series of sequences is <math>F_{0} = n</math> and <math>F_{1-n} \ldots F_{-1}=-1</math>. |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
function luc_seeder{T<:Integer}(n::T) |
function luc_seeder{T<:Integer}(n::T) |
||
a = -ones(BigInt, n) |
a = -ones(BigInt, n) |
||
Line 2,811: | Line 2,811: | ||
NFib(n, k, luc_seeder) |
NFib(n, k, luc_seeder) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Main''' |
'''Main''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
lo = 2 |
lo = 2 |
||
hi = 10 |
hi = 10 |
||
Line 2,850: | Line 2,850: | ||
println() |
println() |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,889: | Line 2,889: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
fun fibN(initial: IntArray, numTerms: Int) : IntArray { |
fun fibN(initial: IntArray, numTerms: Int) : IntArray { |
||
Line 2,915: | Line 2,915: | ||
println("%2d %-10s %s".format(i + 2, names[i], values)) |
println("%2d %-10s %s".format(i + 2, names[i], values)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,933: | Line 2,933: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function nStepFibs (seq, limit) |
||
local iMax, sum = #seq - 1 |
local iMax, sum = #seq - 1 |
||
while #seq < limit do |
while #seq < limit do |
||
Line 2,952: | Line 2,952: | ||
io.write(sequence.name .. ": ") |
io.write(sequence.name .. ": ") |
||
print(table.concat(nStepFibs(sequence.values, 10), " ")) |
print(table.concat(nStepFibs(sequence.values, 10), " ")) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Fibonacci: 1 1 2 3 5 8 13 21 34 55 |
<pre>Fibonacci: 1 1 2 3 5 8 13 21 34 55 |
||
Line 2,960: | Line 2,960: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">numSequence := proc(initValues :: Array) |
||
local n, i, values; |
local n, i, values; |
||
n := numelems(initValues); |
n := numelems(initValues); |
||
Line 2,975: | Line 2,975: | ||
printf ("nacci(%d): %a\n", i, convert(numSequence(initValues), list)); |
printf ("nacci(%d): %a\n", i, convert(numSequence(initValues), list)); |
||
end do: |
end do: |
||
printf ("lucas: %a\n", convert(numSequence(Array([2, 1])), list));</ |
printf ("lucas: %a\n", convert(numSequence(Array([2, 1])), list));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,991: | Line 2,991: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<syntaxhighlight lang="mathematica"> |
|||
<lang Mathematica> |
|||
f2=Function[{l,k}, |
f2=Function[{l,k}, |
||
Module[{n=Length@l,m}, |
Module[{n=Length@l,m}, |
||
Line 2,998: | Line 2,998: | ||
Table[Last/@f2[{1,1}~Join~Table[0,{n-2}],15+n][[-18;;]],{n,2,10}]//TableForm |
Table[Last/@f2[{1,1}~Join~Table[0,{n-2}],15+n][[-18;;]],{n,2,10}]//TableForm |
||
Table[Last/@f2[{1,2}~Join~Table[0,{n-2}],15+n][[-18;;]],{n,2,10}]//TableForm |
Table[Last/@f2[{1,2}~Join~Table[0,{n-2}],15+n][[-18;;]],{n,2,10}]//TableForm |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 3,025: | Line 3,025: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">import sequtils, strutils |
||
proc fiblike(start: seq[int]): auto = |
proc fiblike(start: seq[int]): auto = |
||
Line 3,051: | Line 3,051: | ||
se.add(1 shl i) |
se.add(1 shl i) |
||
let fibber = fiblike(se) |
let fibber = fiblike(se) |
||
echo "n = ", align($n, 2), ", ", align(name, 5), "nacci -> ", toSeq(0..14).mapIt($fibber(it)).join(" "), " ..."</ |
echo "n = ", align($n, 2), ", ", align(name, 5), "nacci -> ", toSeq(0..14).mapIt($fibber(it)).join(" "), " ..."</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>@[1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
<pre>@[1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
||
Line 3,068: | Line 3,068: | ||
We will use lazy lists, so can get any amount of n-nacci numbers. |
We will use lazy lists, so can get any amount of n-nacci numbers. |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define (n-fib-iterator ll) |
(define (n-fib-iterator ll) |
||
(cons (car ll) |
(cons (car ll) |
||
(lambda () |
(lambda () |
||
(n-fib-iterator (append (cdr ll) (list (fold + 0 ll))))))) |
(n-fib-iterator (append (cdr ll) (list (fold + 0 ll))))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Testing: |
Testing: |
||
< |
<syntaxhighlight lang="scheme"> |
||
(print "2, fibonacci : " (ltake (n-fib-iterator '(1 1)) 15)) |
(print "2, fibonacci : " (ltake (n-fib-iterator '(1 1)) 15)) |
||
(print "3, tribonacci: " (ltake (n-fib-iterator '(1 1 2)) 15)) |
(print "3, tribonacci: " (ltake (n-fib-iterator '(1 1 2)) 15)) |
||
Line 3,089: | Line 3,089: | ||
5, pentanacci: (1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930) |
5, pentanacci: (1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930) |
||
2, lucas : (2 1 3 4 7 11 18 29 47 76 123 199 322 521 843) |
2, lucas : (2 1 3 4 7 11 18 29 47 76 123 199 322 521 843) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
Line 3,095: | Line 3,095: | ||
Use genV if you prefer to supply a different starting vector. |
Use genV if you prefer to supply a different starting vector. |
||
< |
<syntaxhighlight lang="parigp">gen(n)=k->my(v=vector(k,i,1));for(i=3,min(k,n),v[i]=2^(i-2));for(i=n+1,k,v[i]=sum(j=i-n,i-1,v[j]));v |
||
genV(n)=v->for(i=3,min(#v,n),v[i]=2^(i-2));for(i=n+1,#v,v[i]=sum(j=i-n,i-1,v[j]));v |
genV(n)=v->for(i=3,min(#v,n),v[i]=2^(i-2));for(i=n+1,#v,v[i]=sum(j=i-n,i-1,v[j]));v |
||
for(n=2,10,print(n"\t"gen(n)(10)))</ |
for(n=2,10,print(n"\t"gen(n)(10)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
<pre>2 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
||
Line 3,111: | Line 3,111: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free_Pascal}} |
{{works with|Free_Pascal}} |
||
< |
<syntaxhighlight lang="pascal">program FibbonacciN (output); |
||
type |
type |
||
Line 3,180: | Line 3,180: | ||
write(sequence[k], ' '); |
write(sequence[k], ' '); |
||
writeln; |
writeln; |
||
end.</ |
end.</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>% ./Fibbonacci |
<pre>% ./Fibbonacci |
||
Line 3,201: | Line 3,201: | ||
Fib(n)/Fib(n-1) tends to the golden ratio = 1.618... 1.618^100 > 2^64 |
Fib(n)/Fib(n-1) tends to the golden ratio = 1.618... 1.618^100 > 2^64 |
||
{{works with|Free_Pascal}} |
{{works with|Free_Pascal}} |
||
< |
<syntaxhighlight lang="pascal"> |
||
program FibbonacciN (output); |
program FibbonacciN (output); |
||
{$IFNDEF FPC} |
{$IFNDEF FPC} |
||
Line 3,314: | Line 3,314: | ||
write(NextNacci(Nacci),' '); |
write(NextNacci(Nacci),' '); |
||
writeln; |
writeln; |
||
END.</ |
END.</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature <say signatures>; |
use feature <say signatures>; |
||
Line 3,332: | Line 3,332: | ||
say $_-1 . ': ' . join ' ', (fib_n $_)[0..19] for 2..10; |
say $_-1 . ': ' . join ' ', (fib_n $_)[0..19] for 2..10; |
||
say "\nLucas: " . join ' ', fib_n(2, [2,1], 20);</ |
say "\nLucas: " . join ' ', fib_n(2, [2,1], 20);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 |
<pre>1: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 |
||
Line 3,347: | Line 3,347: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">nacci_noo</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;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">nacci_noo</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;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">)</span> |
||
Line 3,367: | Line 3,367: | ||
<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;">"%snacci: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">names</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">f</span><span style="color: #0000FF;">})</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;">"%snacci: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">names</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">f</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,377: | Line 3,377: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"><?php |
||
/** |
/** |
||
* @author Elad Yosifon |
* @author Elad Yosifon |
||
Line 3,458: | Line 3,458: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,474: | Line 3,474: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de nacci (Init Cnt) |
||
(let N (length Init) |
(let N (length Init) |
||
(make |
(make |
||
(made Init) |
(made Init) |
||
(do (- Cnt N) |
(do (- Cnt N) |
||
(link (apply + (tail N (made)))) ) ) ) )</ |
(link (apply + (tail N (made)))) ) ) ) )</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="picolisp"># Fibonacci |
||
: (nacci (1 1) 10) |
: (nacci (1 1) 10) |
||
-> (1 1 2 3 5 8 13 21 34 55) |
-> (1 1 2 3 5 8 13 21 34 55) |
||
Line 3,499: | Line 3,499: | ||
# Decanacci |
# Decanacci |
||
: (nacci (1 1 2 4 8 16 32 64 128 256) 15) |
: (nacci (1 1 2 4 8 16 32 64 128 256) 15) |
||
-> (1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172)</ |
-> (1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172)</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pl/i">(subscriptrange, fixedoverflow, size): |
||
n_step_Fibonacci: procedure options (main); |
n_step_Fibonacci: procedure options (main); |
||
declare line character (100) varying; |
declare line character (100) varying; |
||
Line 3,529: | Line 3,529: | ||
end; |
end; |
||
end; |
end; |
||
end n_step_Fibonacci;</ |
end n_step_Fibonacci;</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 3,549: | Line 3,549: | ||
=={{header|Powershell}}== |
=={{header|Powershell}}== |
||
< |
<syntaxhighlight lang="powershell">#Create generator of extended fibonaci |
||
Function Get-ExtendedFibonaciGenerator($InitialValues ){ |
Function Get-ExtendedFibonaciGenerator($InitialValues ){ |
||
$Values = $InitialValues |
$Values = $InitialValues |
||
Line 3,566: | Line 3,566: | ||
}.GetNewClosure() |
}.GetNewClosure() |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example of invocation to generate up to decanaci |
Example of invocation to generate up to decanaci |
||
< |
<syntaxhighlight lang="powershell">$Name = 'fibo tribo tetra penta hexa hepta octo nona deca'.Split() |
||
0..($Name.Length-1) | foreach { $Index = $_ |
0..($Name.Length-1) | foreach { $Index = $_ |
||
$InitialValues = @(1) + @(foreach ($I In 0..$Index) { [Math]::Pow(2,$I) }) |
$InitialValues = @(1) + @(foreach ($I In 0..$Index) { [Math]::Pow(2,$I) }) |
||
Line 3,580: | Line 3,580: | ||
} |
} |
||
} | Format-Table -AutoSize |
} | Format-Table -AutoSize |
||
</syntaxhighlight> |
|||
</lang> |
|||
Sample output |
Sample output |
||
<pre> |
<pre> |
||
Line 3,597: | Line 3,597: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<syntaxhighlight lang="purebasic"> |
|||
<lang PureBasic> |
|||
Procedure.i FibonacciLike(k,n=2,p.s="",d.s=".") |
Procedure.i FibonacciLike(k,n=2,p.s="",d.s=".") |
||
Line 3,654: | Line 3,654: | ||
Debug "" |
Debug "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Sample Output''' |
'''Sample Output''' |
||
Line 3,676: | Line 3,676: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Python: function returning a function=== |
===Python: function returning a function=== |
||
< |
<syntaxhighlight lang="python">>>> def fiblike(start): |
||
addnum = len(start) |
addnum = len(start) |
||
memo = start[:] |
memo = start[:] |
||
Line 3,708: | Line 3,708: | ||
n= 9, nonanacci -> 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ... |
n= 9, nonanacci -> 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ... |
||
n=10, decanacci -> 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ... |
n=10, decanacci -> 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ... |
||
>>> </ |
>>> </syntaxhighlight> |
||
===Python: Callable class=== |
===Python: Callable class=== |
||
< |
<syntaxhighlight lang="python">>>> class Fiblike(): |
||
def __init__(self, start): |
def __init__(self, start): |
||
self.addnum = len(start) |
self.addnum = len(start) |
||
Line 3,744: | Line 3,744: | ||
n= 9, nonanacci -> 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ... |
n= 9, nonanacci -> 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ... |
||
n=10, decanacci -> 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ... |
n=10, decanacci -> 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ... |
||
>>> </ |
>>> </syntaxhighlight> |
||
===Python: Generator=== |
===Python: Generator=== |
||
< |
<syntaxhighlight lang="python">from itertools import islice, cycle |
||
def fiblike(tail): |
def fiblike(tail): |
||
Line 3,765: | Line 3,765: | ||
fib = fiblike([1] + [2 ** i for i in xrange(n - 1)]) |
fib = fiblike([1] + [2 ** i for i in xrange(n - 1)]) |
||
items = list(islice(fib, 15)) |
items = list(islice(fib, 15)) |
||
print "n=%2i, %5snacci -> %s ..." % (n, name, items)</ |
print "n=%2i, %5snacci -> %s ..." % (n, name, items)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
<pre>[1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
||
Line 3,782: | Line 3,782: | ||
Defining the Lucas series and the N-Step Fibonacci series in terms of unfoldr (dual to functools.reduce). |
Defining the Lucas series and the N-Step Fibonacci series in terms of unfoldr (dual to functools.reduce). |
||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
< |
<syntaxhighlight lang="python">'''Fibonacci n-step number sequences''' |
||
from itertools import chain, count, islice |
from itertools import chain, count, islice |
||
Line 3,907: | Line 3,907: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Recurrence relation series: |
<pre>Recurrence relation series: |
||
Line 3,924: | Line 3,924: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ 0 swap witheach + ] is sum ( [ --> n ) |
||
[ tuck size - |
[ tuck size - |
||
Line 3,946: | Line 3,946: | ||
' [ fibonacci tribonacci tetranacci lucas ] |
' [ fibonacci tribonacci tetranacci lucas ] |
||
witheach |
witheach |
||
[ dup echo say ": " 10 swap do echo cr ]</ |
[ dup echo say ": " 10 swap do echo cr ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,957: | Line 3,957: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
;; fib-list : [Listof Nat] x Nat -> [Listof Nat] |
;; fib-list : [Listof Nat] x Nat -> [Listof Nat] |
||
Line 3,981: | Line 3,981: | ||
(cons 1 (build-list (sub1 n) (curry expt 2))))) |
(cons 1 (build-list (sub1 n) (curry expt 2))))) |
||
;; and with an initial (2 1) |
;; and with an initial (2 1) |
||
(show-fibs "lucas" '(2 1))</ |
(show-fibs "lucas" '(2 1))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,999: | Line 3,999: | ||
===Lazy List with Closure=== |
===Lazy List with Closure=== |
||
<lang |
<syntaxhighlight lang="raku" line>sub nacci ( $s = 2, :@start = (1,) ) { |
||
my @seq = |@start, { state $n = +@start; @seq[ ($n - $s .. $n++ - 1).grep: * >= 0 ].sum } … *; |
my @seq = |@start, { state $n = +@start; @seq[ ($n - $s .. $n++ - 1).grep: * >= 0 ].sum } … *; |
||
} |
} |
||
Line 4,005: | Line 4,005: | ||
put "{.fmt: '%2d'}-nacci: ", nacci($_)[^20] for 2..12 ; |
put "{.fmt: '%2d'}-nacci: ", nacci($_)[^20] for 2..12 ; |
||
put "Lucas: ", nacci(:start(2,1))[^20];</ |
put "Lucas: ", nacci(:start(2,1))[^20];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 2-nacci: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 |
<pre> 2-nacci: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 |
||
Line 4,023: | Line 4,023: | ||
A slightly more straight forward way of constructing a lazy list. |
A slightly more straight forward way of constructing a lazy list. |
||
{{works with|Rakudo|2015.12}} |
{{works with|Rakudo|2015.12}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub fib ($n, @xs is copy = [1]) { |
||
flat gather { |
flat gather { |
||
take @xs[*]; |
take @xs[*]; |
||
Line 4,037: | Line 4,037: | ||
say fib($n, [1])[^20]; |
say fib($n, [1])[^20]; |
||
} |
} |
||
say fib(2, [2,1])[^20];</ |
say fib(2, [2,1])[^20];</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program calculates and displays a N-step Fibonacci sequence(s). */ |
||
parse arg FibName values /*allows a Fibonacci name, starter vals*/ |
parse arg FibName values /*allows a Fibonacci name, starter vals*/ |
||
if FibName\='' then do; call nStepFib FibName,values; signal done; end |
if FibName\='' then do; call nStepFib FibName,values; signal done; end |
||
Line 4,074: | Line 4,074: | ||
say right(Fname,11)'[sum'right(N,3) "terms]:" strip(L) '···' |
say right(Fname,11)'[sum'right(N,3) "terms]:" strip(L) '···' |
||
return</ |
return</syntaxhighlight> |
||
'''output''' when using the default input: |
'''output''' when using the default input: |
||
<pre> |
<pre> |
||
Line 4,093: | Line 4,093: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Fibonacci n-step number sequences |
# Project : Fibonacci n-step number sequences |
||
Line 4,155: | Line 4,155: | ||
next |
next |
||
see svect |
see svect |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 4,172: | Line 4,172: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">def anynacci(start_sequence, count) |
||
n = start_sequence.length # Get the n-step for the type of fibonacci sequence |
n = start_sequence.length # Get the n-step for the type of fibonacci sequence |
||
result = start_sequence.dup # Create a new result array with the values copied from the array that was passed by reference |
result = start_sequence.dup # Create a new result array with the values copied from the array that was passed by reference |
||
Line 4,192: | Line 4,192: | ||
decanacci: [1,1,2,4,8,16,32,64,128,256] } |
decanacci: [1,1,2,4,8,16,32,64,128,256] } |
||
naccis.each {|name, seq| puts "%12s : %p" % [name, anynacci(seq, 15)]}</ |
naccis.each {|name, seq| puts "%12s : %p" % [name, anynacci(seq, 15)]}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="ruby"> 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] |
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] |
tribonacci : [1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136] |
||
Line 4,204: | Line 4,204: | ||
nonanacci : [1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 511, 1021, 2040, 4076, 8144] |
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] |
decanacci : [1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1023, 2045, 4088, 8172] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">a = fib(" fibonacci ", "1,1") |
||
a = fib("tribonacci ", "1,1,2") |
a = fib("tribonacci ", "1,1,2") |
||
a = fib("tetranacci ", "1,1,2,4") |
a = fib("tetranacci ", "1,1,2,4") |
||
Line 4,228: | Line 4,228: | ||
next i |
next i |
||
print |
print |
||
end function</ |
end function</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> fibonacci => 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, |
<pre> fibonacci => 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, |
||
Line 4,238: | Line 4,238: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
struct GenFibonacci { |
struct GenFibonacci { |
||
buf: Vec<u64>, |
buf: Vec<u64>, |
||
Line 4,280: | Line 4,280: | ||
print(vec![2,1], 10 - 2); |
print(vec![2,1], 10 - 2); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<lang> |
<syntaxhighlight lang="text"> |
||
Fib2: 1 1 2 3 5 8 13 21 34 55 |
Fib2: 1 1 2 3 5 8 13 21 34 55 |
||
Fib3: 1 1 2 4 7 13 24 44 81 149 |
Fib3: 1 1 2 4 7 13 24 44 81 149 |
||
Fib4: 1 1 2 4 8 15 29 56 108 208 |
Fib4: 1 1 2 4 8 15 29 56 108 208 |
||
Lucas: 2 1 3 4 7 11 18 29 47 76 |
Lucas: 2 1 3 4 7 11 18 29 47 76 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
===Simple Solution=== |
===Simple Solution=== |
||
< |
<syntaxhighlight lang="scala"> |
||
//we rely on implicit conversion from Int to BigInt. |
//we rely on implicit conversion from Int to BigInt. |
||
//BigInt is preferable since the numbers get very big, very fast. |
//BigInt is preferable since the numbers get very big, very fast. |
||
Line 4,300: | Line 4,300: | ||
inner(init.toVector) |
inner(init.toVector) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Optimizing=== |
===Optimizing=== |
||
< |
<syntaxhighlight lang="scala"> |
||
//in the optimized version we don't compute values until it's needed. |
//in the optimized version we don't compute values until it's needed. |
||
//the unoptimized version, computed k elements ahead, where k being |
//the unoptimized version, computed k elements ahead, where k being |
||
Line 4,315: | Line 4,315: | ||
init.to(LazyList) #::: inner(init.toVector) |
init.to(LazyList) #::: inner(init.toVector) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Optimizing Further=== |
===Optimizing Further=== |
||
< |
<syntaxhighlight lang="scala"> |
||
//instead of summing k elements each phase, we exploit the fact |
//instead of summing k elements each phase, we exploit the fact |
||
//that the last element is already the sum of all k preceding elements |
//that the last element is already the sum of all k preceding elements |
||
Line 4,330: | Line 4,330: | ||
v.to(LazyList) #::: inner(v) |
v.to(LazyList) #::: inner(v) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Printing=== |
===Printing=== |
||
< |
<syntaxhighlight lang="scala"> |
||
println(s"Fibonacci: ${fibStream(1,1).take(10).mkString(",")}") |
println(s"Fibonacci: ${fibStream(1,1).take(10).mkString(",")}") |
||
println(s"Tribonacci: ${fibStream(1,1,2).take(10).mkString(",")}") |
println(s"Tribonacci: ${fibStream(1,1,2).take(10).mkString(",")}") |
||
println(s"Tetranacci: ${fibStream(1,1,2,4).take(10).mkString(",")}") |
println(s"Tetranacci: ${fibStream(1,1,2,4).take(10).mkString(",")}") |
||
println(s"Lucas: ${fibStream(2,1).take(10).mkString(",")}") |
println(s"Lucas: ${fibStream(2,1).take(10).mkString(",")}") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,349: | Line 4,349: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(import (scheme base) |
(import (scheme base) |
||
(scheme write) |
(scheme write) |
||
Line 4,376: | Line 4,376: | ||
(display (n-fib '(2 1) 15)) |
(display (n-fib '(2 1) 15)) |
||
(newline) |
(newline) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 4,387: | Line 4,387: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func array integer: bonacci (in array integer: start, in integer: arity, in integer: length) is func |
const func array integer: bonacci (in array integer: start, in integer: arity, in integer: length) is func |
||
Line 4,423: | Line 4,423: | ||
print("Tetranacci", bonacci([] (1, 1), 4, 10)); |
print("Tetranacci", bonacci([] (1, 1), 4, 10)); |
||
print("Lucas", bonacci([] (2, 1), 2, 10)); |
print("Lucas", bonacci([] (2, 1), 2, 10)); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,435: | Line 4,435: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">func fib(n, xs=[1], k=20) { |
||
loop { |
loop { |
||
var len = xs.len |
var len = xs.len |
||
Line 4,447: | Line 4,447: | ||
say fib(i).join(' ') |
say fib(i).join(' ') |
||
} |
} |
||
say fib(2, [2, 1]).join(' ')</ |
say fib(2, [2, 1]).join(' ')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,463: | Line 4,463: | ||
Using matrix exponentiation: |
Using matrix exponentiation: |
||
< |
<syntaxhighlight lang="ruby">func fibonacci_matrix(k) is cached { |
||
Matrix.build(k,k, {|i,j| |
Matrix.build(k,k, {|i,j| |
||
((i == k-1) || (i == j-1)) ? 1 : 0 |
((i == k-1) || (i == j-1)) ? 1 : 0 |
||
Line 4,476: | Line 4,476: | ||
for k in (2..9) { |
for k in (2..9) { |
||
say ("Fibonacci of k=#{k} order: ", (15+k).of { fibonacci_kth_order(_, k) }) |
say ("Fibonacci of k=#{k} order: ", (15+k).of { fibonacci_kth_order(_, k) }) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,490: | Line 4,490: | ||
Faster algorithm: |
Faster algorithm: |
||
< |
<syntaxhighlight lang="ruby">func fibonacci_kth_order (n, k = 2) { |
||
return 0 if (n < k-1) |
return 0 if (n < k-1) |
||
Line 4,509: | Line 4,509: | ||
for k in (2..9) { |
for k in (2..9) { |
||
say ("Fibonacci of k=#{k} order: ", (15+k).of { fibonacci_kth_order(_, k) }) |
say ("Fibonacci of k=#{k} order: ", (15+k).of { fibonacci_kth_order(_, k) }) |
||
}</ |
}</syntaxhighlight> |
||
(same output as above) |
(same output as above) |
||
=={{header|Tailspin}}== |
=={{header|Tailspin}}== |
||
< |
<syntaxhighlight lang="tailspin"> |
||
templates fibonacciNstep&{N:} |
templates fibonacciNstep&{N:} |
||
templates next |
templates next |
||
Line 4,543: | Line 4,543: | ||
' |
' |
||
' -> !OUT::write |
' -> !OUT::write |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,554: | Line 4,554: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.6}} |
{{works with|Tcl|8.6}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.6 |
||
proc fibber {args} { |
proc fibber {args} { |
||
Line 4,583: | Line 4,583: | ||
print10 [fibber 1 1 2 4] |
print10 [fibber 1 1 2 4] |
||
puts "LUCAS" |
puts "LUCAS" |
||
print10 [fibber 2 1]</ |
print10 [fibber 2 1]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,597: | Line 4,597: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang="vb">Option Explicit |
||
Sub Main() |
Sub Main() |
||
Line 4,658: | Line 4,658: | ||
Next |
Next |
||
Fibonacci_Step = R |
Fibonacci_Step = R |
||
End Function</ |
End Function</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 4,667: | Line 4,667: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
'function arguments: |
'function arguments: |
||
'init - initial series of the sequence(e.g. "1,1") |
'init - initial series of the sequence(e.g. "1,1") |
||
Line 4,699: | Line 4,699: | ||
WScript.StdOut.Write "lucas: " & generate_seq("2,1",15) |
WScript.StdOut.Write "lucas: " & generate_seq("2,1",15) |
||
WScript.StdOut.WriteLine |
WScript.StdOut.WriteLine |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 4,712: | Line 4,712: | ||
{{trans|Visual Basic}} |
{{trans|Visual Basic}} |
||
{{works with|Visual Basic .NET|2011}} |
{{works with|Visual Basic .NET|2011}} |
||
< |
<syntaxhighlight lang="vbnet">' Fibonacci n-step number sequences - VB.Net |
||
Public Class FibonacciNstep |
Public Class FibonacciNstep |
||
Line 4,749: | Line 4,749: | ||
End Function 'FibonacciN |
End Function 'FibonacciN |
||
End Class 'FibonacciNstep</ |
End Class 'FibonacciNstep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,762: | Line 4,762: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="vlang">fn fib_n(initial []int, num_terms int) []int { |
||
n := initial.len |
n := initial.len |
||
if n < 2 || num_terms < 0 {panic("Invalid argument(s).")} |
if n < 2 || num_terms < 0 {panic("Invalid argument(s).")} |
||
Line 4,795: | Line 4,795: | ||
println(values.map('${it:4}').join(' ')) |
println(values.map('${it:4}').join(' ')) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,815: | Line 4,815: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
var fibN = Fn.new { |initial, numTerms| |
var fibN = Fn.new { |initial, numTerms| |
||
Line 4,844: | Line 4,844: | ||
Fmt.write("$2d $-10s", i + 2, names[i]) |
Fmt.write("$2d $-10s", i + 2, names[i]) |
||
Fmt.aprint(values, 4, 0, "") |
Fmt.aprint(values, 4, 0, "") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,862: | Line 4,862: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
||
proc Nacci(N, F0); \Generate Fibonacci N-step sequence |
proc Nacci(N, F0); \Generate Fibonacci N-step sequence |
||
Line 4,884: | Line 4,884: | ||
Text(0, "Tetranacci: "); Nacci(4, [1, 1, 2, 4]); |
Text(0, "Tetranacci: "); Nacci(4, [1, 1, 2, 4]); |
||
Text(0, " Lucas: "); Nacci(2, [2, 1]); |
Text(0, " Lucas: "); Nacci(2, [2, 1]); |
||
]</ |
]</syntaxhighlight> |
||
Output: |
Output: |
||
Line 4,896: | Line 4,896: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|Lua}} |
{{trans|Lua}} |
||
< |
<syntaxhighlight lang="yabasic">sub nStepFibs$(seq$, limit) |
||
local iMax, sum, numb$(1), lim, i |
local iMax, sum, numb$(1), lim, i |
||
Line 4,916: | Line 4,916: | ||
print "Tribonacci:", nStepFibs$("1,1,2", 10) |
print "Tribonacci:", nStepFibs$("1,1,2", 10) |
||
print "Tetranacci:", nStepFibs$("1,1,2,4", 10) |
print "Tetranacci:", nStepFibs$("1,1,2,4", 10) |
||
print "Lucas:", nStepFibs$("2,1", 10)</ |
print "Lucas:", nStepFibs$("2,1", 10)</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn fibN(ns){ fcn(ns){ ns.append(ns.sum()).pop(0) }.fp(vm.arglist.copy()); }</syntaxhighlight> |
||
This stores the initial n terms of the sequence and returns a function that, at each call, appends the sum of the terms to the sequence then pops the leading value and returns it. |
This stores the initial n terms of the sequence and returns a function that, at each call, appends the sum of the terms to the sequence then pops the leading value and returns it. |
||
< |
<syntaxhighlight lang="zkl">N:=15; |
||
lucas:=fibN(2,1); do(N){ lucas().print(","); } println(); // Lucas |
lucas:=fibN(2,1); do(N){ lucas().print(","); } println(); // Lucas |
||
ns:=L(1); foreach _ in ([ns.len()+1..10]){ // Fibonacci n-step for 2 .. 10 |
ns:=L(1); foreach _ in ([ns.len()+1..10]){ // Fibonacci n-step for 2 .. 10 |
||
Line 4,927: | Line 4,927: | ||
"%2d: ".fmt(ns.len()).print(); |
"%2d: ".fmt(ns.len()).print(); |
||
(N).pump(List,fibN(ns.xplode())).println(); |
(N).pump(List,fibN(ns.xplode())).println(); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |