23
1
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.
Task
Given two integers determine the smallest Lucas Kernel that generates them.
This is a code-golf 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 – ETHproductions – 2017-03-01T21:08:40.113
Also related – James – 2017-03-01T21:14:10.227
These are the numbers that follow the smallest member in the sequence in absolute value, right? – xnor – 2017-03-01T21:24:41.410
@xnor I am not sure what you are asking. – Post Rock Garf Hunter – 2017-03-01T21:25:59.127
I think what xnor is asking is how you determine which Lucas kernel is the smallest. – Dennis – 2017-03-01T21:58:13.353
No, I mean how smallest is defined here. How do you compare two kernels
a b
andc d
? – Dennis – 2017-03-01T22:00:23.717@Dennis Oh that is the absolute difference between the two members of the Kernel, I will update the question to be more explicit. – Post Rock Garf Hunter – 2017-03-01T22:02:09.727
I meant that any two consecutive elements
a,b
are preceded byb-a
, so them having the smallest absolute different means that preceding element is the smallest element in the sequence in absolute value. – xnor – 2017-03-02T02:00:57.547@xnor I see what you are saying. That is not True however. If
a, b
is a Kernel it could also be followed by the item with the smallest absolute value. However the converse is True, the item with the smallest absolute value will always be followed by a Kernel. – Post Rock Garf Hunter – 2017-03-02T02:03:47.500I believe the answer for the test case
45 80
should actually be43 45
, seeing as that kernel has only a difference of 2 and would generate the sequence:... -119, 80, -39, 41, 2, 43, 45, 88 ...
– notjagan – 2017-03-03T20:11:42.753@notjagan Good catch I realize where I went wrong. – Post Rock Garf Hunter – 2017-03-03T20:29:10.430