| Bytes | Lang | Time | Link |
|---|---|---|---|
| 082 | SAKO | 250326T172145Z | Acrimori |
| 009 | Japt | 170519T073427Z | Shaggy |
| 510 | Nibbles | 221116T111745Z | Dominic |
| 016 | ><> | 221116T102108Z | Emigna |
| 005 | Vyxal | 221116T080209Z | tybocopp |
| 015 | Braingolf | 170517T140437Z | Mayube |
| 007 | MATL | 170517T154524Z | Luis Men |
| 038 | Perl 5 pl | 200925T175715Z | Xcali |
| 009 | Arn | 200925T173148Z | ZippyMag |
| 027 | Add++ | 200925T163348Z | caird co |
| 013 | APL Dyalog Extended | 200925T153620Z | rak1507 |
| 105 | Rockstar | 200915T162401Z | Shaggy |
| 007 | x86 machine code | 200920T023327Z | Peter Co |
| 008 | GolfScript | 180521T203225Z | wastl |
| 023 | Pyth | 180422T013436Z | hakr14 |
| 029 | C gcc | 180422T001158Z | PikalaxA |
| 029 | br**nfuck | 180421T224656Z | Khuldrae |
| 085 | Prolog SWI | 170517T142115Z | Leaky Nu |
| 039 | SmileBASIC | 180320T154837Z | 12Me21 |
| 040 | PowerShell | 180312T122412Z | Nik Weis |
| 024 | Perl 6 | 180109T201500Z | Sean |
| 000 | Common Lisp | 180109T194620Z | Bolce Bu |
| 030 | Forth gforth | 180109T152135Z | reffu |
| 026 | JavaScript ES6 | 170517T140126Z | Shaggy |
| 008 | cQuents | 170702T121127Z | Stephen |
| 025 | Mathematica | 170708T075000Z | user2027 |
| 055 | PHP>=7.1 | 170517T143415Z | Jör |
| 027 | Ruby | 170519T095528Z | G B |
| 058 | Java | 170519T093647Z | Olivier |
| 039 | AWK | 170518T154943Z | Robert B |
| 036 | dc | 170518T102058Z | R. Kap |
| 024 | Pari/GP | 170518T091915Z | alephalp |
| 036 | Mathematica | 170517T233821Z | ZaMoC |
| 024 | Octave | 170518T085608Z | alephalp |
| 039 | R | 170518T071322Z | JAD |
| 015 | k | 170518T005240Z | zgrep |
| 032 | TIBasic | 170518T002304Z | pizzapan |
| 037 | Retina | 170517T232150Z | eush77 |
| 077 | Prolog SWI | 170517T224434Z | eush77 |
| 057 | Axiom | 170517T210249Z | user5898 |
| 030 | Haskell | 170517T170005Z | Laikoni |
| 183 | Klein | 170517T210633Z | Wheat Wi |
| 044 | Lua | 170517T210103Z | Felipe N |
| 037 | Python 2 | 170517T140851Z | ovs |
| 009 | CJam | 170517T144837Z | FrodCube |
| 004 | TAESGL | 170517T135316Z | Tom |
| 003 | Jelly | 170517T142409Z | Dennis |
| 009 | 05AB1E | 170517T135523Z | Emigna |
| 006 | Jelly | 170517T140651Z | fireflam |
| 112 | Python 2 | 170517T141150Z | totallyh |
| 040 | Python 2 | 170517T140555Z | Dead Pos |
| 038 | BrainFlak | 170517T140515Z | Riley |
SAKO, 82 bytes
PODPROGRAM:F(N,X,Y)
M=1-0*N
*1)Y=M×X+Y
X=M×Y-X×(-M)
POWTORZ:I=1(1)N+0*N
F()=X
WROC
This function is 0-indexed. Getting the 0th element was kind of tricky, so maybe there is a shorter way to do that.
Full programme version, 91 bytes
CZYTAJ:N,X,Y
M=1-0*N
*1)Y=M×X+Y
X=M×Y-X×(-M)
POWTORZ:I=1(1)N+0*N
DRUKUJ(9,0):X
STOP1
KONIEC
Nibbles, 5 bytes (10 nibbles)
=@.~~$+<2
Input is [x,y] n.
=@.~~$+<2
.~~ # append until null (here this means forever)
$ # starting with first arg
+ # sum of
<2 # take 2 elements
=@ # finally, get the second-arg-th element
If we prefer to input x, y and n as individual numbers, instead of combining [x,y] as an array, add 1 byte (2 nibbles):
either =_.~~:$@+<2 (:$@ combines arg1 and arg2)
or =+$~.~~_+<2 (_ treats all args in STDIN as an array, but now we need to add one to the index (+$~) since we've prepended a useless value to the start of the sequence)

><>, 16 bytes
:?!v1-}:@+{
{n;>
Explanation
:?!v # if n is 0 move to next row
1-} # decrement n and move it to bottom of stack
:@ # save the largest number for next iteration
+ # add the current numbers
{ # move n to top of stack
{n;> # print bottom of stack (smaller number) and exit
Vyxal, 5 bytes
⁽+dḞi
Takes input as [x, y] then n.
Explanation:
Ḟ # Create an infinite list from the initial vector [x, y] from:
d # a dyadic,
⁽ # single element lambda
+ # which adds its two arguments.
i # Index into that list with the second input (n).
Braingolf, 15 bytes
VR<2-M[R!+v]R_;
_; is no longer needed on the latest version of Braingolf, however that's as of ~5 minutes ago. This could be 13 bytes by removing that.
MATL, 7 bytes
:"wy+]x
Output is 0-based.
Try it at MATL Online!
Explanation
Let the inputs be denoted n (index), a, b (initial terms).
:" % Implicitly input n. Do this n times
% At this point in each iteration, the stack contains the two most
% recently computed terms of the sequence, say s, t. In the first
% iteration the stack is empty, but a, b will be implicitly input
% by the next statement
w % Swap. The stack contains t, s
y % Duplicate from below. The stack contains t, s, t
+ % Add. The stack contains t, s+t. These are now the new two most
% recently comnputed terms
] % End
x % Delete (we have computed one term too many). Implicitly display
Perl 5 -pl, 38 bytes
($a,$n)=<>;($_,$a)=($a,$a+$_)while$n--
Output is 0-indexed. Input format is:
x
y
n
Arn, 9 bytes
á‹-²À-'Z"
Try it!
Takes input in order n, x, y separated by spaces
Explained
Unpacked: [?1_?2{+]?
[ Begin sequence
_ Variable initialized to STDIN; implied
? Indexed
1 Second entry
_
?
2 Third entry
{ Block (calculates all other entries)
+ Last two entries added
} End block; implied
] End sequence
?
_ Implied
Add++, 27 bytes
n:?
y:?
+?
Fn,z:y,y:x,z+
Oy
Just implements the standard Fibonacci algorithm of x, y = y, x+y but with x and y set by the input rather than as \$0, 1\$ or \$1, 1\$
APL (Dyalog Extended), 16 15 13 bytes
⊃(1↓⊢,+/)⍣⎕⊢⎕
Takes input in the form f x y n
1 fewer byte thanks to TessellatingHeckler
2 fewer bytes thanks to Adám
Old answer
{⊃(1↓⊢,+/)⍣⍺⊢⍵}
Takes input in the form n f x y
Rockstar, 110 105 bytes
listen to N
listen to X
listen to Y
cast Y
while N
let Z be X-0+Y
let X be Y
let Y be Z
let N be-1
say X
Try it here (Code will need to be pasted in, with each input on an individual line in the order n, x, y)
x86 machine code, 7 bytes
EDX = x = fib[1], EAX = y = fib[2], RCX = n (1-indexed)
returns in EAX. Works in all 3 modes (16, 32, and 64).
Try it online!
(64-bit NASM source with a _start that parses 3 integers from argv strings)
0000000000401070 <cfib>:
401070: eb .byte 0xeb # jmp rel8 consuming the 01 add opcode as a rel8
0000000000401071 <cfib.loop>:
401071: 01 d0 add eax,edx
# loop entry point on first iteration, jumping over the ModRM byte of the ADD
401073: 92 xchg edx,eax
401074: e2 fb loop 401071 <cfib.loop>
401076: c3 ret
This "jumps" into the loop with machine code that decodes 2 different ways: with a jmp +1 when falling into the loop from the top of the function, or with that 01 byte being an ADD opcode when the loop instruction jumps backwards. Equivalent to jmp .entry but with only 1 byte outside the loop instead of 2.
(Credit to @Ira Baxter for pointing out the general idea of falling into a loop with 1 byte outside that consumes some loop bytes to do something different on the first iteration, e.g. the opcode for cmp al, imm8 as a 1-byte encoding for a jump forward by 1. I got lucky that 01 add could be consumed by jmp rel8 to skip a total of 2 bytes without needing a 66 prefix for cmp ax, imm16 or being in 16-bit mode. 01 or 03 add didn't work well as a modrm for anything like add r/m32, imm8; we need to avoid a memory operand. Note that this trick is not good for performance on some modern CPUs, e.g. AMD that caches instruction boundaries in L1i tags, and probably not newer Intel/AMD with uop caches.)
Stripping labels lets objdump decode as the CPU would on the first pass:
cfib:
401070: eb 01 jmp 0x401073 # jmp rel8 = +1
401072: d0 .byte 0xd0 # jumped over
401073: 92 xchg edx,eax
401074: e2 fb loop 0x401071
401076: c3 ret
It's easy to see that the EDX input will be the EAX return value for ECX=1 (exit right away, loop is never taken so add never runs).
Use strace ./custom-fib 14 2308 4261 to see the full integer value passed to the exit() system call before truncation to an 8-bit exit status.
In 32-bit mode, the same machine code is callable with GCC regparm(3) calling convention as __attribute__((regparm(3))) unsigned custom_fib(unsigned y /*eax*/, unsigned x /*edx*/, unsigned n /*ecx*/);, returning in EAX as usual.
Without the 2-way decode hack: x86 machine code, 8 bytes
0-indexed n in RCX, x (fib[0]) in EAX, y (fib[1]) in EDX - note opposite arg order
0000000000401070 <cfib>:
401070: e3 05 jrcxz 401077 <cfib.done>
0000000000401072 <cfib.loop>:
401072: 0f c1 c2 xadd edx,eax
401075: e2 fb loop 401072 <cfib.loop>
0000000000401077 <cfib.done>:
401077: c3 ret
This way shows off x86's xadd instruction, normally only used with a memory destination and a lock prefix to implement fetch_add.
The reg,reg form of xadd decodes to only 3 uops on Intel Skylake, same as xchg reg,reg (https://uops.info/). (The loop instruction is slow on Intel so this is hardly optimized for speed. If you want a speed-efficient asm implementation, unroll by 2 to remove the xchg as shown in my SO answer using clever loop setup to avoid excess branching for odd vs. even n for standard 0,1 Fibonacci.)
GolfScript, 8 bytes
~{.@+}*;
Takes input in order x y n. Actually generates first n+1 values and pops last.
Explanation:
~{.@+}*; Full program, implicit input
~ Evaluate
{ }* Pop n and repeat: Stack: x y
. Duplicate y x y y
@ Get x y y x
+ Add y x+y
; Pop last
br**nfuck, 39 29 bytes
Thanks to @JoKing for -10!
,<,<,[>[>+>+<<-]<[>+<-]>-]>>.
TIO won't work particularly well for this (or for any BF solution to a problem involving numbers). I strongly suggest @Timwi's EsotericIDE (or implementing BF yourself).
Takes x, then y, then n. 0-indexed. Assumes an unbounded or wrapping tape.
Explanation
,<,<, Take inputs. Tape: [n, y, x]
[ While n:
> [->+>+<<] Add y to x, copying it to the next cell along as well. Tape: [n, 0, x+y, y]
< [>+<-] Move n over. Tape: [0, n, x+y, y]
>- Decrement n.
] >>. End loop. Print cell 2 to the right (x for n == 0).
Prolog (SWI), 85 bytes
l(0,X,Y,X).
l(1,X,Y,Y).
l(N,X,Y,C):-M is N-1,P is N-2,l(M,X,Y,A),l(P,X,Y,B),C is A+B.
0-indexed.
SmileBASIC, 44 39 bytes
crossed out 44 is still regular 44 ;(
DEF F N,X,Y
IF!N THEN?X
F N-1,Y,X+Y
END
Recursive function; prints the result once N reaches 0, ends with a stack overflow.
Common Lisp, 49 Bytes, 0-indexed
(defun fib(n x y)(if(= 0 n)x(fib(1- n)y(+ x y))))
I'm a Lisp noob so any tips would be appreciated ;)
Explanation:
(defun fib(n x y) | Define a function taking 3 arguments
(if(= 0 n)x | If n = 0, return x
(fib(1- n)y(+ x y)))) | Otherwise, call fib with n-1, y, and x+y
JavaScript (ES6), 27 26 bytes
Nothing fancy here, just a standard JS Fibonacci function with the initial values of 0 & 1 removed.
n=>g=(x,y)=>n--?g(y,x+y):x
Try it
f=
n=>g=(x,y)=>n--?g(y,x+y):x
o.value=f(i.value=13)(j.value=2308,k.value=4261)
oninput=_=>o.value=f(+i.value)(+j.value,+k.value)
*{font-family:sans-serif;}
input{margin:0 5px 0 0;width:50px;}
#o{width:75px;}
<label for=i>n: </label><input id=i type=number><label for=j>x: </label><input id=j type=number><label for=k>y: </label><input id=k type=number><label for=o>= </label><input id=o>
cQuents, 8 bytes
=A,B:z+y
Takes input as x y n, where n is 1-indexed.
Explanation
=A,B Set sequence start to first two inputs
: Mode: Sequence
z+y Each item in the sequence is the previous two added together
If there is a third input, that input is `n`. Get the `n`th (1-indexed) item in the sequence
If there is not a third input, output the whole sequence.
Mathematica, 25 bytes
Although this is quite old, and there has already been a Mathematica answer using LinearRecurrence built-in, mine solution is shorter.
{##2}.Fibonacci/@{#-1,#}&
PHP>=7.1, 55 Bytes
for([,$n,$x,$y]=$argv;$n--;$x=$y,$y=$t)$t=$x+$y;echo$x;
PHP>=7.1, 73 Bytes
for([,$n,$x,$y]=$argv,$r=[$x,$y];$i<$n;)$r[]=$r[+$i]+$r[++$i];echo$r[$n];
Ruby, 27 bytes
->a,b,n{n.times{b=a+a=b};a}
Java, 58 bytes
int f(int n,int x,int y){return n<3?n<2?x:y:f(n-1,y,x+y);}
This function is 1-indexed.
Alternatively, one could write the following lambda with recursion, for 55 bytes:
interface B{F f=(n,x,y)->n<3?n<2?x:y:B.f.f(n-1,y,x+y);}
This requires the following description, which isn't usually counted in Java Lambdas:
interface F{int f(int n,int x,int y);}
Test
public class Main {
static
int f(int n,int x,int y){return n<3?n<2?x:y:f(n-1,y,x+y);}
public static void main(String[] args) {
int[][] tests = {
{6, 0, 0, 0},
{7, 0, 1, 8},
{7, 1, 1, 13},
{3, 5, 5, 10},
{11, 2, 2, 178},
{4, 3, 10, 23},
{14, 2308, 4261, 1325165},
{1, 0, 1, 0},
{2, 0, 1, 1}
};
for (int[] test: tests) {
System.out.printf("%2d %4d %4d %7d%n", test[0], test[1], test[2], f(test[0],test[1],test[2]));
}
}
}
Mathematica, 36 bytes
LinearRecurrence[{1,1},{##2},{#+1}]&
input
[n,x,y]
R, 39 bytes
f=function(x,y,n)'if'(n,f(y,x+y,n-1),x)
A simple recursive function. Funnily enough this is shorter than anything I can come up with for the regular Fibonacci sequence (without built-ins), because this doesn't have to assign 1 to both x and y =P
Calculates n+1 numbers of the sequence, including the initial values. Each recursion is calculates with n-1 and stopped when n==0. The lowest of the two numbers is then returned, giving back the n-th value.
TI-Basic, 32 bytes
Prompt N,X,Y
While N
X+Y➡Z
Y➡X
Z➡Y
DS<(N,0
End
X
Retina, 37 bytes
\d+
$*1
+`(1*) (1*) 1
$2 $1$2
.*
1
0-based, takes x y n separated by space. Calculates in unary.
Prolog (SWI), 77 bytes
f(N,Y,Z):-M is N-1,f(M,X,Y),Z is X+Y.
l(N,A,B,X):-asserta(f(0,A,B)),f(N,X,_).
Started off golfing Leaky Nun's answer and arrived at something completely different.
This one has a rule for (Nᵗʰ, (N+1)ᵗʰ) in terms of ((N-1)ᵗʰ, Nᵗʰ) and uses database management to assert 0ᵗʰ and 1ˢᵗ elements at runtime.
f(N,X,Y) means Nᵗʰ element is X and (N+1)ᵗʰ element is Y.
Axiom, 88 57 bytes
f(k,x,y)==(repeat(k<=0=>break;c:=y;y:=x+y;x:=c;k:=k-1);x)
this would pass the test proposed (0 indexed)
(14) -> f(5,0,0)
(14) 0
Type: NonNegativeInteger
(15) -> f(6,0,1)
(15) 8
Type: PositiveInteger
(16) -> f(2,5,5)
(16) 10
Type: PositiveInteger
(17) -> f(10,2,2)
(17) 178
Type: PositiveInteger
(18) -> f(3,3,10)
(18) 23
Type: PositiveInteger
(19) -> f(13,2308,4261)
(19) 1325165
Type: PositiveInteger
Haskell, 30 bytes
x#y=(f!!)where f=x:scanl(+)y f
Try it online! 0-indexed. Use as (x#y)n, e.g. (0#1)5 for the fifth element of the original sequence.
The most likely shortest way to get the Fibonacci sequence in Haskell is f=0:scanl(+)1f, which defines an infinite list f=[0,1,1,2,3,5,8,...] containing the sequence. Replacing 0 and 1 with arguments x and y yields the custom sequence. (f!!) is then a function returning the nth element of f.
Python 2, 37 bytes
f=lambda x,y,n:n and f(y,x+y,n-1)or x
0-indexed, you may need to adjust the recursion limit for n≥999
CJam, 14 9 bytes
l~{_@+}*;
Input format is "x y n". I'm still a noob at this, so I'm 100% sure there are better ways to do this, but please instead of telling me "do this" try to only give me hints so that I can find the answer myself and get better. Thanks!
TAESGL, 4 bytes
ēB)Ė
1-indexed
Explanation
Input taken as n,[x,y]
ēB)Ė
AēB) get implicit input "A" Fibonacci numbers where "B" is [x,y]
Ė pop the last item in the array
Jelly, 3 bytes
+¡ạ
Takes x, y, and n (0-indexed) as separate command-line arguments, in that order.
How it works
+¡ạ Main link. Left argument: x. Right argument: y. Third argument: n
ạ Yield abs(x - y) = y - x, the (-1)-th value of the Lucas sequence.
+¡ Add the quicklink's left and right argument (initially x and y-x), replacing
the right argument with the left one and the left argument with the result.
Do this n times and return the final value of the left argument.
05AB1E, 9 bytes
`©GDŠ+}®@
Explanation
` # split inputs as separate to stack
© # store n in register
G # n-1 times do
D # duplicate top of stack
Š # move down 2 places on stack
+ # add top 2 values of stack
} # end loop
®@ # get the value nth value from the bottom of stack
Jelly, 6 bytes
;SḊµ¡I
Explanation
µ¡ - repeat n times (computes the n+1th and n+2th element):
S - take the sum of the elements of the previous iteration (starting at (x,y))
; - append to the end of the previous iteration
Ḋ - remove the first element
I - Take the difference of the n+1th and n+2th to get the n-th.
Python 2, 112 bytes
1-indexed.
import itertools
def f(x,y):
while 1:yield x;x,y=y,x+y
def g(x,y,n):return next(itertools.islice(f(x,y),n-1,n))
Brain-Flak, 38 bytes
{({}[()]<(({}<>)<>{}<(<>{}<>)>)>)}{}{}
{({}[()]< >)} # For n .. 0
(({}<>)<> ) # Copy TOS to the other stack and add it to...
{} # The second value
<(<>{}<>)> # Copy what was TOS back
{}{} # Pop the counter and the n+1th result
