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

Dashboard
Notifications
Mark all as read
Challenges

Length of a Sumac Sequence

+6
−0

Heavily based on this closed challenge from SE.

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$ (exclusive). No negative integers should be present in the sequence.

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(including the negative number), and compute the length. The length can be 0.

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

Test Cases

Taken from the original question.

    t1   t2      length   
    120  71      5
    101  42      3
    500  499     4
    387  1       3
    3   -128     1
   -2    3       0
    0    0       0
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

4 comments

What should be the result for t1=t2=0? celtschk‭ about 2 months ago

@celtschk sequence ends when tn is ≤ 0, so it should be 0. Razetime‭ about 2 months ago

@Razetime: You should probably add that as a test case, as several currently posted solutions fail in this case. celtschk‭ about 2 months ago

ok, added it in. Razetime‭ about 2 months ago

7 answers

+3
−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!

Why does this post require moderator attention?
You might want to add some details to your flag.

3 comments

44 bytes: a=lambda x,y:(x>=0)*(1if y<0else 1+a(y,x-y)) Hakerh400‭ 2 months ago

32 bytes if you don't mind 0 being False else 35 Jo King‭ 2 months ago

y'all are too good at this Quintec‭ 2 months ago

+2
−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!

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment

@JoKing oh right, I don't Moshi‭ 2 months ago

+2
−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
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+2
−0

C (gcc), 32 bytes

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

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−0

Jelly, 9 bytes

[email protected]п@L

Try it online!

Takes $t_1$ on the left and $t_2$ on the right. R}ȧ[email protected]п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.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−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
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−0

JavaScript (Node.js), 31 bytes

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

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!