24
\$\begingroup\$

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 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
\$\endgroup\$
  • \$\begingroup\$ Related \$\endgroup\$ – ETHproductions Mar 1 '17 at 21:08
  • \$\begingroup\$ Also related \$\endgroup\$ – DJMcMayhem Mar 1 '17 at 21:14
  • \$\begingroup\$ These are the numbers that follow the smallest member in the sequence in absolute value, right? \$\endgroup\$ – xnor Mar 1 '17 at 21:24
  • \$\begingroup\$ @xnor I am not sure what you are asking. \$\endgroup\$ – Sriotchilism O'Zaic Mar 1 '17 at 21:25
  • \$\begingroup\$ I think what xnor is asking is how you determine which Lucas kernel is the smallest. \$\endgroup\$ – Dennis Mar 1 '17 at 21:58
10
\$\begingroup\$

Python 2, 444 391 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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Very impressive! A few golfing suggestions. \$\endgroup\$ – Dennis Mar 4 '17 at 0:21
  • \$\begingroup\$ I am a little confused by your explanation, particularly how you are defining c. \$\endgroup\$ – Sriotchilism O'Zaic Mar 4 '17 at 0:24
  • \$\begingroup\$ @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. \$\endgroup\$ – notjagan Mar 4 '17 at 1:19
  • 1
    \$\begingroup\$ A few more bytes. The output is different, but I think they're equivalent. \$\endgroup\$ – Dennis Mar 4 '17 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.