12
\$\begingroup\$

Heavily based on this closed challenge.

Codidact post, Sandbox

Description

A Sumac sequence starts with two non-zero integers \$t_1\$ and \$t_2.\$

The next term, \$t_3 = t_1 - t_2\$

More generally, \$t_n = t_{n-2} - t_{n-1}\$

The sequence ends when \$t_n ≤ 0\$. All values in the sequence must be positive.

Challenge

Given two integers \$t_1\$ and \$t_2\$, compute the Sumac sequence, and output its length.

If there is a negative number in the input, remove everything after it, and compute the length.

You may take the input in any way (Array, two numbers, etc.)

Test Cases

(Sequence is included for clarification)

[t1,t2]   Sequence          n
------------------------------
[120,71]  [120,71,49,22,27] 5
[101,42]  [101,42,59]       3
[500,499] [500,499,1,498]   4
[387,1]   [387,1,386]       3
[3,-128]  [3]               1
[-2,3]    []                0
[3,2]     [3,2,1,1]         4

Scoring

This is . Shortest answer in each language wins.

\$\endgroup\$
  • \$\begingroup\$ Bonus points for finding(or outgolfing) my 10 byte Husk answer. \$\endgroup\$ – Razetime yesterday
  • 1
    \$\begingroup\$ Infinite lists! \$\endgroup\$ – Razetime yesterday
  • 1
    \$\begingroup\$ Hey, wait, you've changed your bonus, right? Didn't the original comment say you'd got a 9-byte Husk answer...? Or did I imagine it? I've been struggling to find it for the last few hours... \$\endgroup\$ – Dominic van Essen yesterday
  • 1
    \$\begingroup\$ yes, was a mistake. Sorry about that. \$\endgroup\$ – Razetime yesterday
  • 4
    \$\begingroup\$ For obvious reasons, decreasing consecutive Fibonacci numbers will yield the longest sequences \$\endgroup\$ – Digital Trauma yesterday

17 Answers 17

7
\$\begingroup\$

JavaScript (ES6), 24 bytes

f=(a,b)=>a>0&&1+f(b,a-b)

Try it online!

\$\endgroup\$
7
\$\begingroup\$

x86 machine code (8086), 15 13 bytes

-2 bytes thanks to @EasyasPi.

00000000  31 C9 48 7C 07 40 41 29-D8 93 EB F6 C3            1.H|.@A).....

Callable function.

Expects AX = t1, BX = t2. Output is to CX.

Disassembly:

31C9          XOR     CX,CX    ; Set CX to 0
          LOP:
48            DEC     AX       ; --AX
7C07          JL      END      ; If AX < 0, jump to 'END'
40            INC     AX       ; ++AX
41            INC     CX       ; ++CX
29D8          SUB     AX,BX    ; AX -= BX
93            XCHG    BX,AX    ; Swap AX and BX
EBF6          JMP     LOP      ; Jump to tag 'LOP'
          END:
C3            RET              ; Return to caller

Example run

Tested with DOS debug:

-r
AX=0078  BX=0047  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
DS=0B17  ES=0B17  SS=0B17  CS=1000  IP=0000   NV UP EI PL NZ NA PO NC
1000:0000 31C9          XOR     CX,CX
-t

AX=0078  BX=0047  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
DS=0B17  ES=0B17  SS=0B17  CS=1000  IP=0002   NV UP EI PL ZR NA PE NC
1000:0002 48            DEC     AX
...
-t

AX=FFFA  BX=0020  CX=0005  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
DS=0B17  ES=0B17  SS=0B17  CS=1000  IP=000C   NV UP EI NG NZ NA PE CY
1000:000C C3            RET
```
\$\endgroup\$
  • 1
    \$\begingroup\$ It appears you are using the wrong encoding for xchg ax, bx (it should be 0x93) and you can use the flags from sub to trip the loop instead of that fat cmp ax, 0. \$\endgroup\$ – EasyasPi yesterday
  • \$\begingroup\$ Yeah. If you xchg a register with (e)ax, it should be one byte. If it isn't, you have a bad assembler 😂 \$\endgroup\$ – EasyasPi yesterday
  • \$\begingroup\$ @EasyasPi Ahh I see, I should have written xchg bx, ax! My assembler is indeed horrible. \$\endgroup\$ – 2x-1 yesterday
  • 1
    \$\begingroup\$ Note that I didn't test that yet \$\endgroup\$ – EasyasPi yesterday
  • 1
    \$\begingroup\$ Lol no, it is jump if signed. Although it should be jl, my bad. Try it online! \$\endgroup\$ – EasyasPi yesterday
4
\$\begingroup\$

05AB1E, 7 bytes

λ-}(d1k

Try it online! or Try all cases!

Commented:

λ }      # start a recursive environment:
         #   this produces an infinite sequence starting with input
 -       #   and calculates t_n according to t_n = t_{n-2} - t_{n-1}
   (     # negate every value
    d    # for every value: is it non-negative?
     1k  # find the first index of a 1 (smallest n such that -t_n >= 0)
\$\endgroup\$
4
\$\begingroup\$

Charcoal, 33 24 bytes

NθNηW‹⁰θ«⊞υω≦⁻θη≧⁻ηθ»ILυ

Try it online! Link is to verbose version of code. Edit: Saved 9 bytes when @Razetime pointed out I was using an inefficient algorithm. Explanation:

NθNη

Input the initial values.

W‹⁰θ«

Repeat while the first value is positive.

⊞υω

Keep count of the number of iterations.

≦⁻θη

Subtract the second number from the first number, storing the result in the second number.

≧⁻ηθ

Subtract that result from the first number, resulting in the original second number.

»ILυ

Finally print the number of iterations.

\$\endgroup\$
3
\$\begingroup\$

TI-BASIC, 27 Bytes

While A>0
A-B->B
A-B->A
N+1->N
End
N

Takes t1 and t2 as input in A and B. Nothing fancy, the key is that when A is updated, B is the previous t1-t2, so the third line is able to recover t2 via t1-(t1-t2)->A without juggling a 3rd variable. There's a way to generate the sequence in 27 bytes as well with input as a list in Ans and dim( calls, but it proves too complex to trim out negative input and retrieve the true length of the sequence.

\$\endgroup\$
3