Comments on Length of a Sumac Sequence
Parent
Length of a Sumac Sequence
Heavily based on this closed challenge from SE.
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 it's 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 code-golf. Shortest answer in each language wins.
[Raku], 19 bytes {( …
4y ago
[Python 3], 49 47 44 35 bytes …
4y ago
[C (gcc)], 32 bytes …
4y ago
Japt `-N`, 9 bytes ¨T©Ò …
4y ago
[JavaScript (Node.js)], 27 25 …
4y ago
[Scala], 83 bytes x …
3y ago
BQN, 15 bytesSBCS ``` {𝕨(1 …
3y ago
[Jelly], 9 bytes Rȧ@Rп …
4y ago
[JavaScript (Node.js)], 31 byt …
4y ago
Post
Python 3, 49 47 44 35 bytes
a=lambda x,y:+(x>=0)and(1+a(y,x-y))
-3 bytes thanks to @Hakerh400
-9 bytes thanks to @Jo King
Python 2, 50 bytes
x,y=input()
a=0
while x>0:x,y,a=y,x-y,a+1
print(a)
An alternate solution I also came up with that I swear I should be able to make shorter, but can't think of it currently.
1 comment thread