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 »
Sandbox

Comments on Length of a Sumac Sequence[FINALIZED]

Post

Length of a Sumac Sequence[FINALIZED]

+2
−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
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 (5 comments)
General comments
msh210‭ wrote about 4 years ago

"If there is a negative number in the input, remove everything after it, and compute the length." By "after it" I assume you mean in the sequence ⟨tₖ⟩? In that case, shouldn't we be removing it also?

Razetime‭ wrote about 4 years ago

What do you mean by "it"?

Quintec‭ wrote about 4 years ago

I agree with msh - the length should not include the negative number even if one of the inputs is negative to be consistent with the other cases. So the last two examples should be 1 and 0 respectively. Another option is to just guarantee the inputs are positive - its up to you.

Razetime‭ wrote about 4 years ago

@Quintec That makes a lot more sense. Edited.

Quintec‭ wrote about 4 years ago

Looks good to me now, I think you can post it