| Bytes | Lang | Time | Link |
|---|---|---|---|
| 022 | BQN | 250301T192643Z | panadest |
| 3975 | Binary Lambda Calculus | 240802T234231Z | short_c1 |
| 035 | JavaScript Babel Node | 240727T132433Z | l4m2 |
| 048 | Perl 5 p | 201231T222204Z | Xcali |
| 028 | K ngn/k | 201230T210651Z | coltim |
| 008 | Japt g | 190827T084956Z | Shaggy |
| 022 | Brachylog | 190827T082006Z | Unrelate |
| 048 | Haskell | 180331T061459Z | Angs |
| 019 | Pyth | 180330T195008Z | user4854 |
| 027 | Pyth | 180330T165211Z | hakr14 |
| nan | 170726T013022Z | Brad Gil | |
| 069 | Common Lisp | 170721T124638Z | Renzo |
| 037 | Hexagony | 170721T040453Z | Brigmor |
| 2321 | Taxi | 170720T154448Z | Engineer |
| 006 | Pyke | 170720T145856Z | Blue |
| 074 | Python | 170720T120900Z | Anders K |
| 051 | Q/KDB+ | 170720T123758Z | aodNap |
| 056 | C# .NET Core | 170719T182554Z | jkelm |
| 103 | Python 3 | 170719T182720Z | sTertooy |
| 056 | Java OpenJDK 8 | 170719T210023Z | Nevay |
| 052 | Python | 170719T153224Z | ovs |
| 076 | C gcc | 170719T185343Z | cleblanc |
| 024 | x8664 Machine Code | 170719T200501Z | Cody Gra |
| 060 | R | 170719T152759Z | Giuseppe |
| 043 | Python 2 | 170719T191603Z | xnor |
| 052 | dc | 170719T163743Z | brhfl |
| 007 | Jelly | 170719T152002Z | nmjcman1 |
| 041 | JavaScript ES6 | 170719T162755Z | Neil |
| 041 | JavaScript Babel Node | 170719T160355Z | Business |
| 234 | Java 7 | 170719T155137Z | 0x45 |
| 067 | JavaScript ES6 | 170719T155622Z | Arnauld |
| 074 | PowerShell | 170719T153205Z | AdmBorkB |
| 005 | Neim | 170719T150320Z | Okx |
| 006 | 05AB1E | 170719T152844Z | Okx |
| 084 | Python 3 | 170719T152814Z | notjagan |
| 030 | Mathematica | 170719T150446Z | ZaMoC |
Binary Lambda Calculus, 44.375 39.75 bytes
00010100 01000001 01010101 01010110 11100111 00000101 11001010 11100000 11011101
00000001 00000110 01110000 01100111 00000100 10001011 00001010 10101011 10111001
10000010 00000010 00001100 10000010 11001110 00001001 01011100 00011000 00000111
00101111 01101001 11000001 01010000 10110000 01000000 11101011 00000000 10101111
00000011 00111011 11000110 001010
This function takes in a Church numeral as input and returns a Church numeral as output. It's a reduced version of
λn.closer (boundpair n) n
where
succ := λn.λf.λx.f (n f x)
+ := λm.λn.m succ n
⊤ := λx.λy.y
⊥ := λx.λy.x
pred := λn.λf.λx.n (λg.λh.h (g f)) (λu.x) (λu.u)
- := λm.λn.n pred m
is0 := λn.n (λx.⊥) ⊤
≤ := λm.λn.is0 (- m n)
nextfib := λp.λf.f (p ⊥) (+ (p ⊤) (p ⊥))
boundpair := λn.n (λp.≤ (p ⊥) n (nextfib p) p) (λf.f 0 1)
closer := λp.λn.≤ (- n (p ⊤)) (- (p ⊥) n) (p ⊤) (p ⊥)
All the blocks above nextfib are standard lambda calculus definitions, so I won't go into detail on them.
nextfib takes in a pair and returns a pair with the second element and the sum of the two elements. If initialised with (0, 1), repeated application will create successive terms of the fibonacci sequence.
boundpair n finds the pair of consecutive fibonacci terms that are lower and upper bounds on n. The first function applied to n takes a pair of consecutive fibonacci numbers and checks if the second element is less than or equal to n. If it is, we return nextfib of that pair. If the second element is greater than n we return the pair. If we repeatedly apply that to (0, 1) we check a sliding window of fibonacci numbers until we find the bounds. Applying it any more will just repeatedly return that pair. This used to be done recursively, stopping once we hit the pair, but the program still works if we just apply it n times. This is because n ≤ F(n + 1), so we're guarenteed to hit our desired pair by n applications. This takes longer but produces a shorter program.
closer finds the difference between each of the elements in the pair and n. Whichever element has the smaller difference gets returned.
To reduce the size, I manually applied functions where the input was only used in one spot and I factored out pred to be an argument to the rest of the program. A similar tactic may be applicable for ⊤ and ⊥.
JavaScript (Babel Node), 35 bytes
f=(n,x=0,y=1)=>2*n<x+y?x:f(n,y,x+y)
Based on Alix Eisenhardt's JS solution
K (ngn/k), 28 bytes
{F@&x=&/x|:-x-:F:x(+':1,)/1}
F:x(+':1,)/1generate the firstx+1terms of the fibonacci sequence, storing inF(adapted from @ngn's answer here)x-:Fget the difference betweenxand each term of the sequence, updatingx(which now becomes a list)x|:-xtake the absolute value of each, updatingx&x=&/xidentify indices in sequence with the smallest valueF@index into the actual sequence
Brachylog, 22 bytes
;I≜-.∧{0;1⟨t≡+⟩ⁱhℕ↙.!}
Really all I've done here is paste together Fatalize's classic Return the closest prime number solution and my own Am I a Fibonacci Number? solution. Fortunately, the latter already operates on the output variable; unfortunately, it also includes a necessary cut which has to be isolated for +2 bytes, so the only choice point it discards is ⁱ, leaving ≜ intact.
Pyth, 19 bytes
JU2VQ=+Js>2J)hoaNQJ
Explanation
JU2VQ=+Js>2J)hoaNQJ
JU2 Set J = [0, 1].
VQ=+Js>2J) Add the next <input> Fibonacci numbers.
oaNQJ Sort them by distance to <input>.
h Take the first.
Pyth, 27 bytes
J[Z1)WgQeJ=aJ+eJ@J_2)hoaNQJ
Python 3 translation:
Q=eval(input())
def a(x):
return abs(Q-x)
J=[0,1]
while Q>=J[-1]:
J.append(J[-1]+J[-2])
print(sorted(J,key=a)[0])
Perl 6, 38 bytes
{(0,1,*+*...*>$_).sort((*-$_).abs)[0]}
{ # bare block lambda with implicit parameter 「$_」
( # generate Fibonacci sequence
0, 1, # seed the sequence
* + * # WhateverCode lambda that generates the rest of the values
... # keep generating until
* > $_ # it generates one larger than the original input
# (that larger value is included in the sequence)
).sort( # sort it by
( * - $_ ).abs # the absolute difference to the original input
)[0] # get the first value from the sorted list
}
For a potential speed-up add .tail(2) before .sort(…).
In the case of a tie, it will always return the smaller of the two values, because sort is a stable sort. (two values which would sort the same keep their order)
Common Lisp, 69 bytes
(lambda(n)(do((x 0 y)(y 1(+ x y)))((< n y)(if(<(- n x)(- y n))x y))))
Hexagony, 37 bytes
?\=-${&"*.2}+".|'=='.@.&}1.!_|._}$_}{
ungolfed:
? \ = -
$ { & " *
. 2 } + " .
| ' = = ' . @
. & } 1 . !
_ | . _ }
$ _ } {
Broken down:
start:
? { 2 ' * //set up 2*target number
" ' 1 //initialize curr to 1
main loop:
} = + //next + curr + last
" - //test = next - (2*target)
branch: <= 0 -> continue; > 0 -> return
continue:
{ } = & //last = curr
} = & //curr = next
return:
{ } ! @ //print last
Like some other posters, I realized that when the midpoint of last and curr is greater than the target, the smaller of the two is the closest or tied for closest.
The midpoint is at (last + curr)/2. We can shorten that because next is already last + curr, and if we instead multiply our target integer by 2, we only need to check that (next - 2*target) > 0, then return last.
Taxi, 2321 bytes
Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Trunkers.Go to Trunkers:n 1 l 1 l.0 is waiting at Starchild Numerology.1 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 l 2 l.Pickup a passenger going to Bird's Bench.Pickup a passenger going to Cyclone.Go to Cyclone:w 1 r 4 l.[a]Pickup a passenger going to Rob's Rest.Pickup a passenger going to Magic Eight.Go to Bird's Bench:n 1 r 2 r 1 l.Go to Rob's Rest:n.Go to Trunkers:s 1 l 1 l 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:w 2 r.Pickup a passenger going to Trunkers.Pickup a passenger going to Magic Eight.Go to Zoom Zoom:n.Go to Trunkers:w 3 l.Go to Magic Eight:e 1 r.Switch to plan "b" if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:w 1 r.Go to Rob's Rest:w 1 l 1 r 1 l 1 r 1 r.Pickup a passenger going to Cyclone.Go to Bird's Bench:s.Pickup a passenger going to Addition Alley.Go to Cyclone:n 1 r 1 l 2 l.Pickup a passenger going to Addition Alley.Pickup a passenger going to Bird's Bench.Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 1 l.Switch to plan "a".[b]Go to Trunkers:w 1 l.Pickup a passenger going to Cyclone.Go to Bird's Bench:w 1 l 1 r 1 l.Pickup a passenger going to Cyclone.Go to Rob's Rest:n.Pickup a passenger going to Cyclone.Go to Cyclone:s 1 l 1 l 2 l.Pickup a passenger going to Sunny Skies Park.Pickup a passenger going to What's The Difference.Pickup a passenger going to What's The Difference.Go to What's The Difference:n 1 l.Pickup a passenger going to Magic Eight.Go to Sunny Skies Park:e 1 r 1 l.Go to Cyclone:n 1 l.Pickup a passenger going to Sunny Skies Park.Pickup a passenger going to What's The Difference.Go to Sunny Skies Park:n 1 r.Pickup a passenger going to What's The Difference.Go to What's The Difference:n 1 r 1 l.Pickup a passenger going to Magic Eight.Go to Magic Eight:e 1 r 2 l 2 r.Switch to plan "c" if no one is waiting.Go to Sunny Skies Park:w 1 l 1 r.Pickup a passenger going to The Babelfishery.Go to Cyclone:n 1 l.Switch to plan "d".[c]Go to Cyclone:w 1 l 2 r.[d]Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 2 r 1 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 l 1 r.
Try it online!
Try it online with comments!
Un-golfed with comments:
[ Find the Fibonacci number closest to the input ]
[ Inspired by: https://codegolf.stackexchange.com/q/133365 ]
[ n = STDIN ]
Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 1st right.
Pickup a passenger going to Trunkers.
Go to Trunkers: north 1st left 1st left.
[ Initialize with F0=0 and F1=1 ]
0 is waiting at Starchild Numerology.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: west 1st left 2nd left.
Pickup a passenger going to Bird's Bench.
Pickup a passenger going to Cyclone.
Go to Cyclone: west 1st right 4th left.
[ For (i = 1; n > F(i); i++) { Store F(i) at Rob's Rest and F(i-1) at Bird's Bench } ]
[a]
Pickup a passenger going to Rob's Rest.
Pickup a passenger going to Magic Eight.
Go to Bird's Bench: north 1st right 2nd right 1st left.
Go to Rob's Rest: north.
Go to Trunkers: south 1st left 1st left 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: west 2nd right.
Pickup a passenger going to Trunkers.
Pickup a passenger going to Magic Eight.
Go to Zoom Zoom: north.
Go to Trunkers: west 3rd left.
Go to Magic Eight: east 1st right.
Switch to plan "b" if no one is waiting.
[ n >= F(i) so iterate i ]
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: west 1st right.
Go to Rob's Rest: west 1st left 1st right 1st left 1st right 1st right.
Pickup a passenger going to Cyclone.
Go to Bird's Bench: south.
Pickup a passenger going to Addition Alley.
Go to Cyclone: north 1st right 1st left 2nd left.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Bird's Bench.
Go to Addition Alley: north 2nd right 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st left 1st left.
Switch to plan "a".
[b]
[ F(i) > n which means n >= F(i-1) and we need to figure out which is closer and print it]
Go to Trunkers: west 1st left.
Pickup a passenger going to Cyclone.
Go to Bird's Bench: west 1st left 1st right 1st left.
Pickup a passenger going to Cyclone.
Go to Rob's Rest: north.
Pickup a passenger going to Cyclone.
Go to Cyclone: south 1st left 1st left 2nd left.
Pickup a passenger going to Sunny Skies Park.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to What's The Difference.
[ Passengers:n, n, F(i-1) ]
Go to What's The Difference: north 1st left.
Pickup a passenger going to Magic Eight.
[ Passengers:n, n-F(i-1) ]
Go to Sunny Skies Park: east 1st right 1st left.
Go to Cyclone: north 1st left.
Pickup a passenger going to Sunny Skies Park.
Pickup a passenger going to What's The Difference.
[ Passengers: n-F(i-1), F(i-1), F(i) ]
Go to Sunny Skies Park: north 1st right.
Pickup a passenger going to What's The Difference.
[ Passengers: n-F(i-1), F(i), n ]
Go to What's The Difference: north 1st right 1st left.
[ Passengers: n-F(i-1), F(i)-n ]
Pickup a passenger going to Magic Eight.
Go to Magic Eight: east 1st right 2nd left 2nd right.
Switch to plan "c" if no one is waiting.
[ If no one was waiting, then {n-F(i-1)} < {F(i)-n} so we return F(i-1) ]
Go to Sunny Skies Park: west 1st left 1st right.
Pickup a passenger going to The Babelfishery.
Go to Cyclone: north 1st left.
Switch to plan "d".
[c]
[ Otherwise {n-F(i-1)} >= {F(i)-n} so we return F(i) ]
[ Note: If we didn't switch to plan c, we still pickup F(i) but F(i-1) will be the *first* passenger and we only pickup one at The Babelfishery ]
[ Note: Because of how Magic Eight works, we will always return F(i) in the event of a tie ]
Go to Cyclone: west 1st left 2nd right.
[d]
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 2nd right 1st right.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st left 1st right.
Pyke, 6 bytes
}~F>R^
} - input*2
~F - infinite list of the fibonacci numbers
> - ^[:input]
R^ - closest_to(^, input)
Python, 74 bytes
import math
r=5**.5
p=r/2+.5
lambda n:round(p**int(math.log(n*2*r,p)-1)/r)
How it works
For all k ≥ 0, since |φ−k/√5| < 1/2, Fk = φk/√5 + φ−k/√5 = round(φk/√5). So the return value switches from Fk − 1 to Fk exactly where k = logφ(n⋅2√5) − 1, or n = φk + 1/(2√5), which is within 1/4 of Fk + 1/2 = (Fk − 1 + Fk)/2.
Q/KDB+, 51 bytes
{w where l=min l:abs neg[x]+w:{x,sum -2#x}/[x;0 1]}
C# (.NET Core), 63 56 bytes
-1 byte thanks to @Neil
-6 bytes thanks to @Nevay
n=>{int a=0,b=1;for(;b<n;a=b-a)b+=a;return n-a>b-n?b:a;}
Python 3, 103 bytes
import math
def f(n):d=5**.5;p=.5+d/2;l=p**int(math.log(d*n,p))/d;L=[l,p*l];return round(L[2*n>sum(L)])
Sadly, had to use a def instead of lambda... There's probably much room for improvement here.
Original (incorrect) answer:
Python 3, 72 bytes
lambda n:r(p**r(math.log(d*n,p))/d)
import math
d=5**.5
p=.5+d/2
r=round
My first PPCG submission. Instead of either calculating Fibonacci numbers recursively or having them predefined, this code uses how the n-th Fibonacci number is the nearest integer to the n-th power of the golden ratio divided by the root of 5.
Java (OpenJDK 8), 60 57 56 bytes
c->{int s=1,r=2;for(;c>r;s=r-s)r+=s;return c-s>r-c?r:s;}
-1 byte thanks to @Neil
C (gcc), 86 85 83 76 bytes
f(n){n=n<2?:f(n-1)+f(n-2);}i,j,k;g(n){for(;k<n;j=k,k=f(i++));n=k-n<n-j?k:j;}
x86-64 Machine Code, 24 bytes
31 C0 8D 50 01 92 01 C2 39 FA 7E F9 89 D1 29 FA 29 C7 39 D7 0F 4F C1 C3
The above bytes of code define a function in 64-bit x86 machine code that finds the closest Fibonacci number to the specified input value, n.
The function follows the System V AMD64 calling convention (standard on Gnu/Unix systems), such that the sole parameter (n) is passed in the EDI register, and the result is returned in the EAX register.
Ungolfed assembly mnemonics:
; unsigned int ClosestFibonacci(unsigned int n);
xor eax, eax ; initialize EAX to 0
lea edx, [rax+1] ; initialize EDX to 1
CalcFib:
xchg eax, edx ; swap EAX and EDX
add edx, eax ; EDX += EAX
cmp edx, edi
jle CalcFib ; keep looping until we find a Fibonacci number > n
mov ecx, edx ; temporary copy of EDX, because we 'bout to clobber it
sub edx, edi
sub edi, eax
cmp edi, edx
cmovg eax, ecx ; EAX = (n-EAX > EDX-n) ? EDX : EAX
ret
The code basically divides up into three parts:
The first part is very simple: it just initializes our working registers.
EAXis set to 0, andEDXis set to 1.The next part is a loop that iteratively calculates the Fibonacci numbers on either side of the input value,
n. This code is based on my previous implementation of Fibonacci with subtraction, but…um…isn't with subtraction. :-) In particular, it uses the same trick of calculating the Fibonacci number using two variables—here, these are theEAXandEDXregisters. This approach is extremely convenient here, because it gives us adjacent Fibonacci numbers. The candidate potentially less thannis held inEAX, while the candidate potentially greater thannis held inEDX. I'm quite proud of how tight I was able to make the code inside of this loop (and even more tickled that I re-discovered it independently, and only later realized how similar it was to the subtraction answer linked above).Once we have the candidate Fibonacci values available in
EAXandEDX, it is a conceptually simple matter of figuring out which one is closer (in terms of absolute value) ton. Actually taking an absolute value would cost way too many bytes, so we just do a series of subtractions. The comment out to the right of the penultimate conditional-move instruction aptly explains the logic here. This either movesEDXintoEAX, or leavesEAXalone, so that when the functionRETurns, the closest Fibonacci number is returned inEAX.
In the case of a tie, the smaller of the two candidate values is returned, since we've used CMOVG instead of CMOVGE to do the selection. It is a trivial change, if you'd prefer the other behavior. Returning both values is a non-starter, though; only one integer result, please!
R, 70 67 64 62 60 bytes
-2 bytes thanks to djhurio!
-2 more bytes thanks to djhurio (boy can he golf!)
F=1:0;while(F<1e8)F=c(F[1]+F[2],F);F[order((F-scan())^2)][1]
Since we only have to handle values up to 10^8, this works.
Reads n from stdin. the while loop generates the fibonacci numbers in F (in decreasing order); in the event of a tie, the larger is returned. This will trigger a number of warnings because while(F<1e8) only evaluates the statement for the first element of F with a warning
Originally I used F[which.min(abs(F-n))], the naive approach, but @djhurio suggested (F-n)^2 since the ordering will be equivalent, and order instead of which.min. order returns a permutation of indices to put its input into increasing order, though, so we need [1] at the end to get only the first value.
faster version:
F=1:0;n=scan();while(n>F)F=c(sum(F),F[1]);F[order((F-n)^2)][1]
only stores the last two fibonacci numbers
Python 2, 43 bytes
f=lambda n,a=0,b=1:a*(2*n<a+b)or f(n,b,a+b)
Iterates through pairs of consecutive Fibonacci numbers (a,b) until it reaches one where the input n is less than their midpoint (a+b)/2, then returns a.
Written as a program (47 bytes):
n=input()
a=b=1
while 2*n>a+b:a,b=b,a+b
print a
f=lambda n,a=0,b=1:b/2/n*(b-a)or f(n,b,a+b)
dc, 52 bytes
si1d[dsf+lfrdli>F]dsFxli-rlir-sdd[lild-pq]sDld<Dli+p
Takes input at run using ?
Edited to assume top of stack as input value, -1 byte.
Input is stored in register i. Then we put 1 and 1 on the stack to start the Fibonacci sequence, and we generate the sequence until we hit a value greater than i. At this point we have two numbers in the Fibonacci sequence on the stack: one that is less than or equal to i, and one that is greater than i. We convert these into their respective differences with i and then compare the differences. Finally we reconstruct the Fibonacci number by either adding or subtracting the difference to i.
Oops, I was loading two registers in the wrong order and then swapping them, wasting a byte.
Jelly, 9 7 bytes
-2 bytes thanks to @EriktheOutgolfer
‘RÆḞạÐṂ
Golfing tips welcome :). Takes an int for input and returns an int-list.
' input -> 4
‘ ' increment -> 5
R ' range -> [1,2,3,4,5]
ÆḞ ' fibonacci (vectorizes) -> [1,1,2,3,5,8]
ÐṂ ' filter and keep the minimum by:
ạ ' absolute difference -> [3,3,2,1,1,4]
' after filter -> [3,5]
JavaScript (ES6), 41 bytes
f=(n,x=0,y=1)=>y<n?f(n,y,x+y):y-n>n-x?x:y
<input type=number min=0 value=0 oninput=o.textContent=f(this.value)><pre id=o>0
Rounds up by preference.
JavaScript (Babel Node), 41 bytes
f=(n,i=1,j=1)=>j<n?f(n,j,j+i):j-n>n-i?i:j
Based on ovs's awesome Python answer
Ungolfed
f=(n,i=1,j=1)=> // Function f: n is the input, i and j are the most recent two Fibonacci numbers, initially 1, 1
j<n? // If j is still less than n
f(n,j,j+i) // Try again with the next two Fibonacci numbers
: // Else:
j-n>n-i?i:j // If n is closer to i, return i, else return j
Java 7, 244 234 Bytes
String c(int c){for(int i=1;;i++){int r=f(i);int s=f(i-1);if(r>c && s<c){if(c-s == r-c)return ""+r+","+s;else if(s-c > r-c)return ""+r;return ""+s;}}} int f(int i){if(i<1)return 0;else if(i==1)return 1;else return f(i-2)+f(i-1);}
JavaScript (ES6), 67 bytes
f=(n,k,y)=>(g=k=>x=k>1?g(--k)+g(--k):k)(k)>n?x+y>2*n?y:x:f(n,-~k,x)
Test cases
f=(n,k,y)=>(g=k=>x=k>1?g(--k)+g(--k):k)(k)>n?x+y>2*n?y:x:f(n,-~k,x)
console.log(f(1 )) // -> 1
console.log(f(3 )) // -> 3
console.log(f(4 )) // -> 3 or 5 or 3, 5
console.log(f(6 )) // -> 5
console.log(f(7 )) // -> 8
console.log(f(11 )) // -> 13
console.log(f(17 )) // -> 13 or 21 or 13, 21
console.log(f(63 )) // -> 55
console.log(f(101 )) // -> 89
console.log(f(377 )) // -> 377
console.log(f(467 )) // -> 377
console.log(f(500 )) // -> 610
console.log(f(1399)) // -> 1597
PowerShell, 80 74 bytes
param($n)for($a,$b=1,0;$a-lt$n;$a,$b=$b,($a+$b)){}($b,$a)[($b-$n-gt$n-$a)]
(Try it online! temporarily unresponsive)
Iterative solution. Takes input $n, sets $a,$b to be 1,0, and then loops with Fibonacci until $a is larger than the input. At that point, we index into ($b,$a) based on Boolean of whether the difference between the first element and $n is greater than between $n and the second element. That's left on the pipeline, output is implicit.
Neim, 5 bytes
f𝐖𝕖S𝕔
Explanation:
f Push infinite Fibonacci list
𝐖 93
𝕖 Select the first ^ elements
This is the maximum amount of elements we can get before the values overflow
which means the largest value we support is 7,540,113,804,746,346,429
S𝕔 Closest value to the input in the list
In the newest version of Neim, this can be golfed to 3 bytes:
fS𝕔
As infinite lists have been reworked to only go up to their maximum value.
Python 3, 84 bytes
lambda k:min(map(f,range(2*k)),key=lambda n:abs(n-k))
f=lambda i:i<3or f(i-1)+f(i-2)
It may work, but it's certainly not fast...
Outputs True instead of 1, but in Python these are equivalent.