11
1
The N-bonacci sequence, originally invented by @DJMcMayhem in this question, is a sequence generated by starting with the integers 0 and 1, and then adding the previous N numbers to generate the next number. The special N-bonacci sequence is an N-bonacci sequence beginning with a pair of numbers other than 0 and 1, which will be named X and Y. If N is greater than the number of terms already in the sequence, simply add all available terms.
So for example the normal fibonacci sequence has an N of 2 (takes the previous two items), and an X and Y of 0 and 1, or 1 and 1, depending on who you ask.
Your Task:
You are to write a program or function that checks whether an inputted integer (A) is part of the special N-bonacci sequence generated by the next three integers (using the second input as N, and the third and fourth as X and Y). Ensure that you handle the special case of N=1.
Input:
Four non-negative integers, A, N, X, and Y.
Output:
A truthy/falsy value that indicates whether A is part of the N-bonacci sequence generated by the N, X, and Y inputs.
Test Cases:
Input: Output:
13,2,0,1->truthy
12,3,1,4->falsy
4,5,0,1-->truthy
8,1,8,9-->truthy
9,1,8,9-->truthy
12,5,0,1->falsy [0,1]>[0,1,1]>[0,1,1,2]>[0,1,1,2,4]>[0,1,1,2,4,8]>[0,1,1,2,4,8,16]>etc.
Scoring:
This is code-golf, so the lowest score in bytes wins.
1
N==1
is such a weird case. – Magic Octopus Urn – 2017-06-16T14:43:17.927Yep, but weird cases are what makes this fun :) – Gryphon – 2017-06-16T14:44:05.650
If you do indeed want answers to handle the case
N=1
, you might want to call it out in the question, since many answers (including all current answers, I think) will have a failure condition that assumes a strictly increasing series. Also, canX
andY
be negative? That will probably also invalidate all existing answers. – apsillers – 2017-06-16T19:43:53.230No, X and Y are "non-negative integers" (in the question, under input). N=1 has been edited into the question. – Gryphon – 2017-06-16T20:42:26.523
1I think all existing answers fail to handle the non-increasing case where both X and Y are zero. Is it necessary to handle that case as well? – apsillers – 2017-06-16T23:11:47.117
1I think you should add the truthy cases
8,1,8,9
and9,1,8,9
to ensure thatN=1
case handling detects the non-repeatedX
value as well as theY
value. (If you want to handle0,0
cases you should add that as well.) – apsillers – 2017-06-16T23:19:03.037Test case which I suspect very few answers get right:
2,1,1,0
. – Ørjan Johansen – 2017-06-16T23:51:32.447