| Bytes | Lang | Time | Link |
|---|---|---|---|
| 051 | AWK | 241125T212440Z | xrs |
| 153 | Regex ECMAScript | 241011T010213Z | Deadcode |
| 007 | Uiua | 241008T014226Z | noodle p |
| 003 | Vyxal 3 | 241008T135247Z | pacman25 |
| 041 | Python 3.8 prerelease | 241007T215348Z | aeh5040 |
| 012 | x8664 assembly | 220526T030603Z | Chris |
| 024 | C gcc | 230715T183638Z | Hunaphu |
| 010 | x8664 machine code | 230716T012439Z | engineer |
| 016 | m68k assembly GNU | 230715T201031Z | Saladin |
| 004 | Thunno 2 | 230715T164409Z | The Thon |
| 043 | Scala | 230418T061335Z | 138 Aspe |
| 017 | Rattle | 220531T145049Z | d01 |
| 040 | Raku | 221130T155800Z | Mark Ree |
| 014 | J | 221111T082039Z | south |
| 017 | ><> | 221111T073708Z | Emigna |
| nan | 221013T152436Z | bigyihsu | |
| nan | Fig | 220924T190119Z | Seggan |
| 035 | Prolog SWI | 220901T175739Z | naffetS |
| 013 | TIBASIC | 220901T193455Z | absolute |
| 029 | Ruby | 220901T133415Z | G B |
| 022 | Desmos | 220525T230933Z | Aiden Ch |
| 045 | Brev | 220622T140630Z | Sandra |
| 094 | Brainfuck | 220622T132455Z | MennoK |
| 093 | BitCycle u | 220617T201825Z | Nilster |
| 010 | Pip | 220612T200609Z | naffetS |
| 017 | APLDyalog Unicode | 220612T082051Z | Kamila S |
| 011 | K ngn/k | 220611T143230Z | coltim |
| 022 | K ngn/k | 220602T135418Z | oeuf |
| 010 | Stax | 220531T162348Z | recursiv |
| 006 | Husk | 220525T225334Z | Dominic |
| 006 | Vyxal | 220525T201814Z | naffetS |
| 005 | Husk | 220527T153457Z | Leo |
| 048 | Python 3 | 220526T214336Z | axolotl |
| 020 | R | 220525T223316Z | Dominic |
| 017 | JavaScript | 220525T202719Z | Unmitiga |
| 027 | C gcc | 220526T234420Z | c-- |
| 004 | Jelly | 220526T232411Z | Luis Men |
| 037 | Haskell | 220526T213852Z | flawr |
| 5714 | Haskell | 220526T160142Z | staletid |
| 031 | PowerShell | 220526T181420Z | another |
| 024 | Python 3 | 220525T213755Z | Luis Men |
| 016 | PARI/GP | 220526T040207Z | alephalp |
| 004 | MathGolf | 220525T221022Z | Kevin Cr |
| 036 | Cgcc | 220525T222718Z | Lala5th |
| 014 | Factor + math.unicode | 220525T202436Z | chunes |
| 026 | Retina 0.8.2 | 220525T225556Z | Neil |
| 017 | Wolfram Language Mathematica | 220525T224643Z | att |
| 024 | Charcoal | 220525T223524Z | Neil |
| 004 | Vyxal | 220525T211849Z | emanresu |
| 004 | 05AB1E | 220525T215731Z | Kevin Cr |
| 031 | rSNBATWPL | 220525T215716Z | rydwolf |
| 041 | Python 3 | 220525T212013Z | Noodle9 |
| 032 | rSNBATWPL | 220525T203354Z | naffetS |
| 032 | JavaScript | 220525T200912Z | rydwolf |
| nan | 220525T202325Z | Giuseppe |
AWK, 51 bytes
{(n=$1/(((1+sqrt(5))/2)))-int(n)>=.5&&n++}$0=int(n)
To test:
awk '{(n=$1/(((1+sqrt(5))/2)))-int(n)>=.5&&n++}$0=int(n)' <<< 1597
{(n=$1/(((1+sqrt(5))/2))) # magic algorithm
-int(n)>=.5&&n++} # handle rounding
$0=int(n) # set output
Regex (ECMAScript), 153 bytes
(?=(x*).*(?=x{4}(x{5}(\1{5}))(?=\2*$)\3+$)(|x{4})(?=xx(x*)\5)\4(x(x*))(?=(\6*)\7+$)(?=\6*$\8)\6*(?=x\1\7+$)(x*)(\9x?)|)((\5\10)(\9\10\12?)?|x{5}|xx?x?)\B
Takes its input in unary, as a string of x characters whose length represents the number. Returns its output as the length of the match.
This is based on Am I a Fibonacci Number? (161 bytes), with some modifications that take advantage of the difference between that problem and this one.
# tail = N = input number; no need for an anchor, because all
# Fibonacci inputs except for 1 will return a match
(?=
(x*) # \1+1 = potential number for which 5*(\1+1)^2 ± 4 is a
# perfect square; this is true iff \1+1 is a Fibonacci number,
# which we shall call F_n. Outside the surrounding lookahead
# block, \1+1 is guaranteed to be the largest number for which
# this is true such that \1 + 5*(\1+1)^2 + 4 <= N.
.*
(?= # tail = (\1+1) * (\1+1) * 5 + 4
x{4}
( # \2 = (\1+1) * 5
x{5}
(\1{5}) # \3 = \1 * 5
)
(?=\2*$)
\3+$
)
(|x{4}) # \4 = parity - determined by whether the index of Fibonacci
# number \1+1 is odd or even;
(?=
xx # tail = arithmetic mean of (\1+1) * (\1+1) * 5 and \6 * \6
# = ((F_n * F_n * 5) + (L_n * L_n)) / 2 = L_{2n}
(x*)\5 # \5 = floor(tail / 2) = floor(L_{2n} / 2)
)
\4
# require that the current tail is a perfect square
(x(x*)) # \6 = potential square root, which will be the square root
# after the following two lookaheads; \7 = \6-1
(?=(\6*)\7+$) # \8 = must be zero for \6 to be a valid square root
(?=\6*$\8)
# \6 is now the Lucas number L_n corresponding to the Fibonacci number F_n.
\6*
(?=x\1\7+$) # tail = F_{2n} = L_n * F_n = \6 * (\1+1), where \6 is larger
(x*)(\9x?) # \9 = floor(tail / 2);
# \10 = ceil(tail / 2); the remainder tail % 2 will always be
# the same as the remainder discarded by \5, because
# F_{2n} is odd iff L_{2n} is odd, thus this ceil()
# can complement the floor() of \5 when adding \5 + \10
| # Allow everything above to be skipped, resulting in all
# capture groups being unset.
)
(
# Note that if the above was skipped using the empty alternative in the lookahead,
# the following will evaluate to 0. This relies on ECMAScript NPCG behavior.
(\5\10) # \12 = F_{2n+1} = (L_{2n} + F_{2n})/2 = \5 + \10; head=\12
(
\9\10 # head = F_{2n+2} = F_{2n} + F_{2n+1}
# = \9+\10 + \12
\12? # head = F_{2n+3} = F_{2n+2} + F_{2n+1} (optionally)
# = head + \12;
# This is needed only for an input of N = 21, which is an
# exception because 2^2*5+4 = 24 > 21, thus the algorithm
# chooses 1^2*5+4 = 9 as its square instead. This won't break
# any subsequent Fibonacci inputs, because executing this
# optional will in those cases set tail = 0, breaking the
# final assertion.
)? # Do the above optionally
|
x{5}|xx?x? # The cases 2→1, 3→2, 5→3, and 8→5 cannot be handled by our
# main algorithm, so match them here.
)
\B # Assert head ≠ 0 and tail ≠ 0
Regex (ECMAScript), 162 bytes
\B(?=(x*).*(?=x{4}(x{5}(\1{5}))(?=\2*$)\3+$)(|x{4})(?=xx(x*)\5)\4(x(x*))(?=(\6*)\7+$)(?=\6*$\8)\6*(?=x\1\7+$)(x*)(\9x?)|)(\9\10(\5\10)\12?|x{21}|x{8}|x{5}|xx?x?)$
This is an exact clone of Am I a Fibonacci Number? (161 bytes) with the ^ anchor replaced with \B, which is a shorthand for (?!^|$). So the regex engine goes forward one character at a time looking for a match, thus matching the largest Fibonacci number less than the input. As such this is much slower than the 153 byte version.
(What we'd really like is (?!^) instead of \B, so we could return an output of \$0\$ from an input of \$1\$, but that would take 3 more bytes, and the problem doesn't require handling \$1\$.)
Uiua, 7 bytes
⍢⤙+⋅≠.1
Try it: Uiua pad
Starting with two ones, repeatedly add them while bringing the second number to the top until the second number is equal to the input. Then, the top number is the previous fibonacci.
Alternatively, using the method of dividing by the golden ratio:
Uiua, 8 bytes
⁅÷÷2+1√5
Try it: Uiua pad
Uiua is read right to left and stack-based, so written with more traditional infix this is equivalent to:
⁅(x÷((1+√5)÷2))
(where ⁅ means round to the nearest integer)
Python 3.8 (pre-release), 41 bytes
A different method. Impractically slow for large values without caching. For n between two Fibonaccis it returns the lower one.
g=lambda n:n and[a:=g(n-1),n-1][g(a)+a<n]
x86-64 assembly, 13 12 bytes
6a 01 58 99 92 01 c2 39 fa 75 f9 c3
In assembly:
previous:
push 1
pop rax
cdq # zeroes rdx
previous_loop:
xchg eax,edx
add edx,eax
cmp edx,edi
jne previous_loop
ret
Uses the usual calling convention of taking the first argument in rdi and returning in rax. It's also valid x86-32, just with edi and eax instead.
It's pretty simple- it just explicitly computes Fibonacci numbers and returns if the new number is equal to the input. At the end of a loop, rdx contains the next Fibonacci number and rax contains the previous. If rdx is equal to the input rdi, then the loop terminates (and thus rax, the previous number, is returned). Otherwise the two are swapped, and the loop begins again.
This uses 32-bit registers to save a few bytes, so it only works up to the 48th Fibonacci number, 4807526976. This does not fit in a 32-bit integer but its result does so and it is not the same as any previous number modulo 2^32 so the math works out.
It can be expanded to work properly with 64-bit numbers by changing all the registers to 64-bit, at the cost of 2 bytes:
6a 01 58 99 48 0f c1 c2 48 39 fa 75 f7 c3
The added bytes are the REX.W (0x48) prefix that swaps the operand size to 64-bit. xchg rax,rdx; add rdx,rax is also replaced with xadd rdx,rax, which is shorter because it uses one fewer REX.W prefix.
-1 byte thanks to @PeterCordes.
C (gcc), 26 24 bytes
-4 thanks to c--.
This uses int and overflows at the 46th fib (first exceeding \$2^{31}\$) but
p(n){n=round(n*2/(1+sqrt(5)));} breaks at 43 and is longer.
p(n){n=n*.618033989+.5;}
x86-64 machine code, 10 bytes
Expects input in edx and outputs to eax.
b8 dd bc 1b cf f7 f0 d1 e8 c3
Explanation
Disassembly:
b8 dd bc 1b cf mov eax,0xcf1bbcdd
f7 f0 div eax
d1 e8 shr eax,1
c3 ret
The function approximates \$\operatorname{round}(\frac{n}{\phi})=\lfloor\frac{n+\frac{\phi}{2}}{\phi}\rfloor=\lfloor\frac{1}{2}\cdot\frac{2^{32}n+2^{31}\phi}{2^{31}\phi}\rfloor\$, where \$\phi=\frac{1+\sqrt{5}}{2}\$. \$2^{31}\phi\approx3474701533\$, which fits nicely into a 32-bit integer as 0xcf1bbcdd. Since div r32 works on edx:eax, n can be loaded into edx and 0xcf1bbcdd can be loaded into eax to create the numerator of the fraction, while also having eax as the denominator of the fraction after div eax. Afterwards, all that is left is to divide by two, which is handled by shr eax, 1.
m68k assembly (GNU), 16 bytes
70 00 72 01 d0 81 c1 41 b2 af 00 04 66 f6 4e 75
Or in assembly:
begin:
moveq.l #0, %d0;
moveq.l #1, %d1;
loop:
add.l %d1, %d0;
exg.l %d0, %d1;
cmp.l 4(%sp), %d1;
bne loop;
rts;
Works with both the Linux and the SysV calling conventions (parameters on stack, return in %d0, %d0-%d1 are scratch registers).
The c function definition is:
unsigned long prev_fibonacci(unsigned long num);
All calculations are done in 32 bit mode. 16bit is the same size, only faster.
Code execution takes 22 cycles plus 46 cycles per loop iteration. (On a 68000 w/ a 16bit bus and no waitstates)
Thunno 2, 4 bytes
kG/V
Explanation
# Implicit input...
/ # ...divided by...
kG # ...phi...
V # ...then rounded
# Implicit output
Rattle, 17 bytes
|r`=+s[$+~[\gq]]0
Explanation
| take input
r` save input as special arg
= set top of stack to 0
+ increment
s save value of 1 to storage
[...]0 infinite loop
$ swap top of stack with value in storage
+~ increment top of stack by value in storage
[\...] if the top of the stack is equal to the special arg...
g get the value in storage (i.e. the previous Fibonacci value)
q quit and print implicitly
J, 12 14 bytes
1.61803<.@%~>:
Pretty self-explanatory.
1.61803<.@%~>:
>: NB. increment input
1.61803 NB. golden ratio
%~ NB. flip arg order and divide
<.@ NB. then floor the result
I saw 1.618 might be too inaccurate. -:>:%:5 results in 1.61803, so I added the extra digits.
><>, 17 bytes
01\~n;
=?\:@+:{:}
Explanation
Starts at 0,1.
Calculates the next Fibonacci number until we reach the input.
Prints the second newest.
Go, 59 bytes
import."math"
func f(n float64)float64{return Round(n/Phi)}
wow built-in constants
Calculated version, 72 bytes
import."math"
func f(n float64)float64{return Round(2*n/(1+Pow(5,0.5)))}
Fig, \$5\log_{256}(96)\approx\$ 4.116 bytes
_\mG}
No round builtins, so I use the old increment and floor trick.
_\mG}
} # The input number incremented
\ # Divided by
mG # The golden ratio
_ # Floor
Prolog (SWI), 35 bytes
1+0.
N+M:-nth1(M,_,_),X is N-M,M+X.
-9 bytes thanks to Jo King
Of course, it's shorter to just use floating point:
Prolog (SWI), 31 bytes
N/M:-M is round(2*N/(1+5^0.5)).
TI-BASIC, 13 bytes
round(2Ans(1+√(5))⁻¹,0
Hexdump:
(Token hex-values found here.)
12 32 72 10 31 70 BC 35 11 11 0C 2B 30 | round(2Ans(1+√(5))⁻¹,0
Takes input in Ans and prints the requested output in the challenge.
Explanation:
round(2Ans(1+√(5))⁻¹,0 ; full program
1+√(5) ; phi * 2
( )⁻¹ ; 1 / (phi * 2)
Ans ; Ans / (phi * 2)
2 ; Ans / phi
round( ,0 ; round to nearest integer
Note: TI-BASIC is a tokenized language. Character count does not equal byte count.
TI-BASIC only has decimal precision up to 14 decimals for calculations and 10 decimals for equivalence.
Byte count is determined via the following steps:
- Find the program's size via
MEM>Mem Mgmt/Del…>Prgm… - Subtract the length of the program name
- Subtract the program header length, which is 9 bytes
Desmos, 25 22 bytes
-3 bytes thanks to @Steffan
pp-p~1
f(n)=round(n/p)
Port of some of the other answers.
Brev, 50 45 bytes
(define((p x y)n)(if(= n y)x((p y(+ x y))n)))
Example:
(map (p 0 1) '(34 13 55 2))
=> (21 8 34 1)
Brainfuck, 94 bytes
>>>>+<<<+[[-]<[->+>+<<]>>[-<<+>>]>>[->+>+<<]>>[-<<+>>]<<<[->>+<<]>[-<+>]>[-<+<<<->>>>]<<<<]>>
Input should be entered into the first address. Output is at the address pointed to when the program terminates.
Might add an explanation later.
BitCycle (-u), 93 bytes
v / <^ ~+AA/v
1v ~B B vC v> ^/ <
v ~ ?\BB\v ?= ~ !
1D~ D ^ +C >~
ADA~^ + ^ B@
The program generates a Fibonacci number, doubles it, and compares it to the input. If it's greater than or equal to the input, it is halved and output, otherwise it's discarded and the next number is generated.
the question mark [input] hits a splitter [\]. one bit is redirected while the rest of the bits move to top B [input].
1v ~B top D [2fib(n-1)], which starts with 2, is emptied to right A [2fib(n)] and left A [intermittent].
v ~ ?\B bottom D [2fib(n-2)] empties to right A.
1D~ right A empties to top D and top B [2fib(n)].
ADA~ left A empties to bottom D [2fib(n-2)].
v / < *'top B' refers to BOTH B's; 'bottom B' refers to BOTH B's*
B B vC if bottom B [input] isn't empty, it hits a splitter. one bit is redirected to D [intermittent] while the rest of the bits re-enter bottom B.
BB\v top B [2fib(n)] hits a splitter. one bit is redirected to top C [2fib(n)] while the rest re-enter top B.
D ^ +C if bottom B is empty, top B hits another splitter. one bit is redirected to bottom C ["2fib(n) >= input"?].
^ + ^ D will eventually empty to bottom B.
^ ~+AA/v *'A' refers to BOTH A's
C v> ^/ < if bottom C ["2fib(n) >= input"?] isn't empty, it hits a switch. the bits are redirected to B [terminator]
?= ~ ! if bottom C is empty, top C [2fib(n)] hits a switch. the bits are redirected into the question mark, destroying them.
C >~ if bottom C isn't empty, top C empties to A [fib(n)]
B@ A hits 2 splitters. one bit is redirected away, and another is redirected into the exclamation mark [output]. the rest of the bits re-enter A.
B empties into the at-sign, which terminates the program.
K (ngn/k), 12 11 bytes
--2!(1-%5)*
Applies @Albert.Lang's approach from this comment.
(1-%5)*calculate 1 minus the square root of 5 (i.e.-1.2360679774997898) and multiply it by the (implicit) input-2!integer divide by 2 (rounding towards 0, e.g.-8!-7returns-1)-negate (and implicitly return)
Stax, 10 bytes
Çâ'Ç,¼)í"─
Approach
- Start with an infinite Fibonacci generator
- Keep values while they are less than the input, resulting in array e.g.
[1, 1, 2, 3, 5]. - Extract last element.
Husk, 6 bytes
i*½←√5
Same strategy as my R answer (and also those of Luis Mendo and unmitigated).
(Husk has a fibonacci sequence built-in, but using it here seems to come-out longer at 7 bytes: S!o←€İf).
Update: proven wrong by Leo
Husk, 5 bytes
→←xİf
Makes use of the built-in list of Fibonacci numbers.
Explanation
→←xİf
İf Take the list of Fibonacci numbers
x Split it on occurrences of the input
← Get the first sub-list
→ Get the last element of that
R, 23 22 20 bytes
Edit: -1 byte thanks to pajonk, then -2 bytes thanks to att
\(x)(x*5^.5-x+1)%/%2
The ratio between consecutive fibonacci numbers famously converges towards the golden ratio phi, equal to (1+5^.5)/2.
This is sufficiently accurate that rounding produces the correct value for all fibonacci numbers, including even the second 1 at the start which we don't need to handle here.
(Edit after reading some other answers: this was independently-derived, but is also the approach taken by Luis Mendo and unmitigated)
Haskell, 38 37 bytes
This works for an aribrary size of inputs. We iterate through the Fibonacci sequence until we find some number x, that is larger than half the input n. This is works because the previous fibonacci number is roughly 0.618*n.
The Fibonacci list f was borrowed from R. Martinho Fernandes.
g n=[x|x<-f,2*x>=n]!!0
f=0:scanl(+)1f
Haskell, 57 bytes (or 14 bytes)
Complete solution which handles arbitrarily large input:
f n=(x!!).flip(-)1.length.fst.span(<n)$x
x=scanl(+)0(1:x)
Explanation:
Let
xbe a an infinite list of fibonacci numbers (x = scanl (+) 0 (1:x)).Partition list into a tuple (
[Int], [Int]), the leftmost portion being all numbers<than the input.Take the first portion with
fst.Subtract 1 from the length of that list.
Extract (
!!) the number with the resultant (idx--) index from the infinite list.
Simple solution:
round.(/0.618)
PowerShell, 31 bytes
[int]("$args"/1.61803398874989)
The ratio of two adjacent numbers in the Fibonacci series rapidly approaches ((1 + sqrt(5)) / 2). So if N is divided by ((1 + sqrt(5)) / 2) and then rounded, the resultant number will be the previous Fibonacci number...
at least according to this article.
-16 thanks mazzy
Python 3, 29 28 27 24 bytes
The footer uses @Noodle9's checker code.
1 byte saved thanks to @Chris!
3 more bytes off thanks to Albert.Lang!
Function that returns an integer-valued float. It fails for large inputs due to floating-point inaccuracies.
lambda n:~n*(1-5**.5)//2
PARI/GP, 16 bytes
n->n*2\/(1+5^.5)
Port of Unmitigated's JavaScript answer. a\/b is a short way to write round(a/b).
PARI/GP's floating point number is 128-bit by default. This gives the correct result until the 184th Fibonacci number (127127879743834334146972278486287885163).
MathGolf, 6 4 bytes
)φ/i
-2 bytes porting @emanresuA's Vyxal answer, which is in turn a port of @Unmitigated's JavaScript answer.
Original 6 bytes answer:
╒fg<┤Þ
g< could alternatively also be á>: try it online.
Explanation:
) # Increase the (implicit) input-integer by 1
φ/ # Divide it by the golden ratio 1.618033988749895
i # Truncate it to an integer
# (the +1 and truncate act as a round builtin here, which MathGolf lacks)
# (after which the entire stack is output implicitly as result)
╒ # Push a list in the range [1, (implicit) input-integer]
f # Get the 0-based n'th Fibonacci number for each value in this list
g # Filter it by:
< # Where it's smaller than the (implicit) input-integer
┤ # Extract the trailing value
Þ # Discard the list from the stack, keeping just that value
# (after which the entire stack is output implicitly)
C(gcc), 57,55,53,50,45, 36 bytes
x;y;f(a){for(x=y=1;y-a-x;y+=x=y-x);}
Naive approach based on calculating all previous Fibonacci numbers, breaking when we reach the input.
Edit: Thanks for Neil, golfing 2 5 bytes.
Edit2: Thanks Dominic van Essen for golfing an other 5 bytes.
Edit3: Thanks att for golfing 9 bytes.
Factor + math.unicode, 21 19 18 14 bytes
[ φ / round ]
-4 thanks to @DominicvanEssen!
Divide the input by the golden ratio and round the result.
Retina 0.8.2, 26 bytes
.+
$*
(\2?(\1)|1)+1
$1$2
1
Try it online! Link includes test cases. Explanation: Loosely based on @MartinEnder's Retina answer to Am I a Fibonacci Number?.
.+
$*
Convert to unary.
(\2?(\1)|1)+1
Match as many Fibonacci numbers as possible. The first iteration only matches the leading 1 while subsequent iterations match iteration of the loop matches the penultimate Fibonacci number (if any), then the previous Fibonacci number, while saving it as the next penultimate Fibonacci number. The match actually sums the Fibonacci sequence, resulting in a sequence one less than the Fibonacci sequence, so a final 1 needs to be matched.
$1$2
Because the match is summing the sequence, the final capture of the sequence $1 is actually the Fibonacci number before the one we want, so we need to add on the penultimate matched Fibonacci number $2.
1
Convert to decimal.
Charcoal, 24 bytes
NθF²⊞υιW‹⌈υθ⊞υΣ…⮌υ²I§υ±²
Try it online! Link is to verbose version of code. Explanation:
Nθ
Input the target.
F²⊞υι
Start with [0, 1].
W‹⌈υθ
Repeat until the target is reached.
⊞υΣ…⮌υ²
Generate the next Fibonacci number.
I§υ±²
Output the highest one less than the input.
05AB1E, 4 bytes
ÅF¨θ
Try it online or verify some more test cases.
¨θ could be a lot of different alternatives, like Á¤ or `\.
Explanation:
ÅF # Push a list of Fibonacci numbers lower than or equal to the (implicit) input
¨ # Remove the last one (the input)
θ # Keep the next last one
# (after which it is output implicitly as result)
rSNBATWPL, 31 bytes
n~(y=1;x~cond{y!n}{{y=y+x}}$x)$
Port of my JS answer:
This is a recursive solution, based off one of the two most common recursive fibonacci algorithms, which involves a function
f(x, y) = f(y, x + y)that stops at some point. With this function,xis always a fibonacci number, andyis always the fibonacci number after it, so onceyis the fibonacci number we were given as input, we knowxis the one before it.
JavaScript, Node.js, 32 bytes
-1 thanks to Arnauld
n=>(y=1,f=x=>y-n?f(y,y+=x):x)(0)
Explanation:
This is a recursive solution, based off one of the two most common recursive fibonacci algorithms, which involves a function f(x, y) = f(y, x + y) that stops at some point. With this function, x is always a fibonacci number, and y is always the fibonacci number after it, so once y is the fibonacci number we were given as input, we know x is the one before it.
R, 27 bytes
\(x){while(x>T)T=F+(F=T);F}
Similar to this answer, iterates through the Fibonacci sequence until T==x and returns F.