Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Length of a Sumac Sequence

+9
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (4 comments)

9 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+2
−0

JavaScript (Node.js), 31 bytes

f=(a,b)=>a<0?0:b<0?1:1+f(b,a-b)

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
+4
−0

Python 3, 49 47 44 35 bytes

a=lambda x,y:+(x>=0)and(1+a(y,x-y))

Try it online!

-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.

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (3 comments)
+4
−0

Raku, 19 bytes

{(|@_,*-*...0>*)-1}

Try it online!

{                 }  # Anonymous code block
         ...         # Create a sequence
  |@_                # Starting with the input
     ,*-*            # With each element being the difference between the previous two
            0>*      # Until it is negative
 (             )-1   # And return the length of the list minus one
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

JavaScript (Node.js), 27 25 bytes

(-2 thanks to JoKing)

f=(a,b)=>a<0?0:1+f(b,a-b)

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)
+3
−0

C (gcc), 32 bytes

f(a,b){return a>0?f(b,a-b)+1:0;}

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
+3
−0

Japt -N, 9 bytes

¨T©ÒßVVnU

Try it

¨T©ÒßVVnU     :Implicit input of integers U & V
¨             :U is >=
 T            :  0
  ©           :Logical AND with
   Ò          :Negate the bitwise NOT of
    ß         :Recursive call with arguments
     V        :  U=V
      VnU     :  V=U-V
              :Implicit output of final result as an integer
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Jelly, 9 bytes

Rȧ_@Rп@L

Try it online!

Takes $t_1$ on the left and $t_2$ on the right. R}ȧ_@RпL works given the arguments in the opposite order.

R            Ascending range from 1 to t_1 (truthy if positive),
 ȧ           and:
     п      collecting intermediate results, loop while
    R        positive
       @     on the reversed arguments:
  _          subtract
   @         the first from the second.
        L    Return the length of the conjunction.

¿ and family, given a dyad, replace the right argument with the previous left argument on each successive iteration. This is incredibly bothersome for some tasks, but perfect for working with this exact kind of sequence.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Scala, 83 bytes

x=>x.init#::Stream.iterate(x)(x=>x++(x,x.tail).zipped.map(_-_))indexWhere(_.last<1)

Try it online!

Takes input as a list of two numbers. The fact that the length can be 0 makes this a bit longer.

x =>  //List(t1, t2) a.k.a. the second partial sequence
x.init  //List(t1) a.k.a. the first partial sequence
#::     //prepended to
Stream  //an infinite stream of partial sequences
.iterate(x)  //starting with the second partial sequence
(x =>        //to which this function is repeatedly applied:
x ++         //Concatenate the previous sequence to the next values
(x, x.tail).zipped  //Zip the previous sequence and its tail
.map(_-_))          //And subtract each i-1th element from the ith element
indexWhere   //Find the index of the sequence where
(_.last<1)   //The last element is not positive
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

BQN, 15 bytesSBCS

{𝕨(1+⊢𝕊-)⟜𝕩⍟>0}

Run online!

A block function that takes $t_1$ as the left argument 𝕨 and $t_2$ as the right argument 𝕩. In BQN, 𝕊 indicates recursion on the current block function. Inside the function the idea is just to return 0 if 𝕨≤0 and otherwise advance the sequence by one, recurse, and add one to the resulting sequence length.

{𝕨(1+⊢𝕊-)⟜𝕩⍟>0}
             0   # Default result of 0
 𝕨         ⍟>    # If 𝕨>0:
  (     )⟜𝕩      # Use 𝕩 as right arg (left is still 𝕨)
     ⊢ -         # Right argument ⊢ and difference -
      𝕊          # Recurse
   1+            # Add one

The unrelated 0s for sequence length and minimum 𝕨 are combined by making 0 the right argument and using Repeat (). With 0 repetitions, Repeat will return the right argument 0, and with 1 repetition, it will call the operand function, which immediately replaces 0 with 𝕩 using After (𝕩 is a number, so it acts as a constant function).

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »