# Find the Fibonacci Kernel

You've probably heard of the Fibonacci numbers; they're pretty famous. Each number in the Fibonacci sequence is the sum of the last two in the sequence with the first and second numbers being 1. The sequence looks like this:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073 4807526976 7778742049 12586269025 20365011074 32951280099 53316291173 86267571272 139583862445 225851433717 365435296162 591286729879 956722026041 1548008755920 2504730781961 4052739537881 6557470319842 10610209857723 17167680177565 27777890035288 44945570212853 72723460248141 117669030460994 190392490709135 308061521170129 498454011879264 806515533049393 1304969544928657 2111485077978050 3416454622906707 5527939700884757 8944394323791464 14472334024676221 23416728348467685 37889062373143906 61305790721611591 99194853094755497 160500643816367088 259695496911122585 420196140727489673 679891637638612258 1100087778366101931 1779979416004714189 2880067194370816120 4660046610375530309 7540113804746346429 12200160415121876738 19740274219868223167 31940434634990099905 51680708854858323072 83621143489848422977 135301852344706746049 218922995834555169026 354224848179261915075 573147844013817084101 927372692193078999176 1500520536206896083277 2427893228399975082453 3928413764606871165730 6356306993006846248183 10284720757613717413913 16641027750620563662096 26925748508234281076009 43566776258854844738105 70492524767089125814114 114059301025943970552219 184551825793033096366333 298611126818977066918552 483162952612010163284885 781774079430987230203437 1264937032042997393488322


Similarly, Lucas sequences are the result of substituting the rather arbitrary 1 1 that start the Fibonacci sequence with any two arbitrary integers. Additionally unlike the Fibonacci sequence Lucas sequences also go backwards infinitely. For example 1 1 not only generates all of the numbers in the Fibonacci sequence but all the numbers that would lead up to it:

... 13 -8 5 -3 2 -1 1 0 1 1 2 3 5 8 13 ...


The Kernel of a Lucas sequence is the closest two consecutive members of the sequence. For example the Kernel of the Fibonacci sequence is 1 1 because they are 0 apart and thus must be the closest two numbers.

The size of the Kernel is measured as the absolute difference between the two members of the Kernel.

Since every pair of numbers is generated by at least one Lucas Sequence, and each sequence has a unique Kernel, for each pair of numbers there is a set of Kernels that generate them. The smallest Lucas Kernel is the smallest Kernel that generates two numbers.

For example take 8 and 21.

Here are a couple of sequences that have both 8 and 21 in them:

... 1 1 2 3 5 8 13 21 ...
... 18 -5 13 8 21 29 50 79 ...
... 21 -13 8 -5 3 -2 1 -1 0 -1 -1 ...
... 34 -13 21 8 29 37 68 ...


Now if we find the Kernels of each of these sequences we get:

1 1
13 8
-1 -1
29 37


The smallest Kernels are 1 1 and -1 -1 (they are tied). We can know this without checking any other sequences because they are of size 0 and it is impossible to find any Kernels smaller than size 0.

Given two integers determine the smallest Lucas Kernel that generates them.

This is a question so the goal is to write code that performs this task in as few bytes as possible.

Standard input and output formats are accepted and enforced. You must handle negative numbers.

In cases where there are multiple valid solutions you need only output one

## Test Cases

8   21 -> 1   1
137 66 -> 66  67
45  80 -> 43  45
-6  45 -> 39  45
37 149 -> 18  19
37  97 -> -2  -3

• Related Commented Mar 1, 2017 at 21:08
• Also related Commented Mar 1, 2017 at 21:14
• These are the numbers that follow the smallest member in the sequence in absolute value, right?
– xnor
Commented Mar 1, 2017 at 21:24
• @xnor I am not sure what you are asking. Commented Mar 1, 2017 at 21:25
• I think what xnor is asking is how you determine which Lucas kernel is the smallest. Commented Mar 1, 2017 at 21:58

# Python 2, 444391 372 bytes

Crossed out 444 is still regular 444 ;(

Huge thanks to @Dennis for a whopping -52 -71 bytes!

k=lambda c,a,b:abs(a+c*a-c*b)-c<abs(b-a)>0and k(c,b-c*a,a+b-c*b)or(a,b)
def f(*t):
a,b=sorted(t);m=b-a+1,0;g=lambda _:min([k(1,*k(0,*s)),m][_!=b:],key=lambda(x,y):abs(x-y))
if b<0:x,y=f(-a,-b);return-x,-y
for c in range(-b,b+1):
for s in(c,a),(a,c):
x,y=s
if min(s)>0:
while y<b:x,y=y,x+y
m=g(y)
x,y=s
while(x!=b)&((x>b)^(b>0)):x,y=y-x,x
m=g(x)
return m


Try it online!

The solution can be run by calling f(a, b) on the two input integers. It is based on the idea that if both a and b are within at least one of the same sequence (where a and b are ordered beforehand such that a ≤ b), it follows that there is at least one integer c equivalent to an adjacent value of a in a shared sequence of a and b for which the sequence generated by a and c contains b in it.

Furthermore, if at least one of the two integers is positive, all values of c must be bounded by -b ≤ c ≤ b for it to even be possible to generate the value of b on either side of the starting pair. Thus, the solution simply brute-forces values of c between -b and b that in combination with a are capable of generating b within the sequence, and finds the one for which the difference of the kernel values for a and c are minimal (this is possible because finding the kernel for two adjacent numbers in a sequence is trivial).

If neither a nor b is positive, the solution simply negates both and returns the negative of the kernel generated for the negated pair.

• Very impressive! A few golfing suggestions. Commented Mar 4, 2017 at 0:21
• I am a little confused by your explanation, particularly how you are defining c. Commented Mar 4, 2017 at 0:24
• @WheatWizard To clarify my definition (which I realize now is brutally wordy), here's an example. Say we have 8 and 21, and we define a = 8 and b = 21. By the premise of the problem, they both share at least one Lucas sequence. Let's say we examine the one generated by the kernel 1, 1, which contains both 8 and 21. In this sequence, a is adjacent to both 5 and 13, meaning if we were to use either in combination with a, we inherently generate a sequence containing 21. It then just comes down to picking the best c for minimizing kernel difference. Commented Mar 4, 2017 at 1:19
• A few more bytes. The output is different, but I think they're equivalent. Commented Mar 4, 2017 at 4:54

# APL (Dyalog Unicode), 81 75 bytes

⌊{⌽q⊃⍨⊃⍋|-/¨q←⊃,/2,/¨p/⍨B∊¨p←2(+/⍤↑,⊢,¯2-/⍤↑⊢)⍣{∧/B≤|¯2↑1⌽⍵}¨⍺,¨(-,⊢)⍳B←⍵}⌈


Try it online!

## Explanation

This performs a similar algorithm as @notjagan's answer. For each C between -B and B inclusive, it produces the Lucas sequence containing A,C (where A and B are the minimum and maximum of the two arguments, respectively).

The part where this solution gets interesting is how the Lucas sequences and kernels are computed. Since a Lucas sequence is infinite, we can only generate a finite number of terms. Starting with A,C, we add one term to the beginning and one term to the end until the subsequence computed includes (at minimum) all terms with absolute value at most B (and thus B, iff the Lucas sequence contains B) and includes the kernel. This occurs if the beginning and end of the subsequence have absolute value at least B and the subsequence has at least 4 terms.

⌊{ ... B←⍵}⌈  ⍝ Find ⍺=min(x, y); B=max(x, y):
(-,⊢)⍳B     ⍝ (-1,-2,-3...-B,1,2,3,..B)
⍝ (Don't need 0 since sequence from (A,0) is same as (A,A), which occurs since A≤B)
⍺,¨           ⍝ Prepend ⍺ to each to get all possible (A, C) pairs
... ¨        ⍝ For each (A, C) pair:
2(+/⍤↑,⊢,¯2-/⍤↑⊢)     ⍝ Extend the Lucas sequence once in each direction
⍝ (This is the reverse of the normal recurrence, so f(a-1) = f(a) + f(a+1)):
+/2↑,               ⍝ Sum of the left two,
⊢             ⍝ Existing sequence,
¯2-/⍤↑⊢     ⍝ Difference of the last two
⍣{    ..         }  ⍝ Extend until:
∧/B≤|¯2↑1⌽⍵         ⍝ The element with minimum absolute value is not one of the two endpoints
⍝ (At this point, the sequence must contain B if it ever will,
⍝ and it must contain the element with minimum absolute value,
⍝ so the kernel is just pair of elements with minimum absolute difference)
p/⍨B∊¨p←          ⍝ Filter for those that contain B
2,/¨              ⍝ Find all consecutive pairs of terms in each sequence
⌽q⊃⍨⊃⍋|-/¨q←⊃,/   ⍝ Choose the pair of terms with minimum absolute difference; it must be a kernel

• Removing redundant parentheses, opening unnecessary brackets, using 18.0's atop: 75