Am I a Special N-bonacci Number?

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 , so the lowest score in bytes wins.

Gryphon

Posted 2017-06-16T13:27:58.067

Reputation: 6 697

1N==1 is such a weird case. – Magic Octopus Urn – 2017-06-16T14:43:17.927

Yep, 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, can X and Y be negative? That will probably also invalidate all existing answers. – apsillers – 2017-06-16T19:43:53.230

No, 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 and 9,1,8,9 to ensure that N=1 case handling detects the non-repeated X value as well as the Y value. (If you want to handle 0,0 cases you should add that as well.) – apsillers – 2017-06-16T23:19:03.037

Test case which I suspect very few answers get right: 2,1,1,0. – Ørjan Johansen – 2017-06-16T23:51:32.447

Answers

5

Jelly, 12 bytes

ḣ⁴S;µṀ<⁵µ¿⁵e

A full program taking [X,Y], N, A.

Try it online!

How?

ḣ⁴S;µṀ<⁵µ¿⁵e - Main link (monadic): [X,Y]
    µ   µ¿   - while:
     Ṁ       -   maximum value of the list
       ⁵     -   5th command line argument (3rd input) = A
      <      -   less than?
             - ...do:
 ⁴           -   4th command line argument (2nd input) = N
ḣ            -   head (get the first N (or less) items from the list)
  S          -   sum
   ;         -   concatenate (add the result to the front of the list)
          ⁵  - 5th command line argument (3rd input) = A
           e - exists in the resulting list?

Jonathan Allan

Posted 2017-06-16T13:27:58.067

Reputation: 67 804

Excellent. Seems to work for me, anyway. +1 – Gryphon – 2017-06-16T14:10:22.747

To instead see the reversed N-bonacci sequence up to a value greater than or equal to A just remove the ⁵e from the end; much easier to tell it will work then (noting that the order of the first two terms is of no consequence). – Jonathan Allan – 2017-06-16T14:19:24.863

Tried a bunch of test cases, so unless someone finds one it fails, it's good with me. – Gryphon – 2017-06-16T14:20:25.880

5

05AB1E, 18 bytes

[DR²£O©‚˜³®>‹#]³QZ

Try it online!


Uses: [X,Y], N, A


I feel like some unintended functionality made that harder than it needed to be.

There's no greater-than-or-equal-to, never noticed that before.

And didn't work, and required a ], for +1 bytes #]³.

Magic Octopus Urn

Posted 2017-06-16T13:27:58.067

Reputation: 19 422

4

Python 2, 59 56 bytes

a,n,l=input()
while l[0]<a:l=[sum(l[:n])]+l
print a in l

Try it online!

Takes input as A,N,[X,Y]

ovs

Posted 2017-06-16T13:27:58.067

Reputation: 21 408

Here is a wrapper for all test cases if you like it. – Leaky Nun – 2017-06-16T15:14:47.727

Here are 2 bytes golfed off. – Leaky Nun – 2017-06-16T15:15:43.660

3

Perl 6, 47 bytes

->\A,\N,\X,\Y{A∈(X,Y,{[+] @_.tail(N)}...*>A)}

test it

Expanded:

->
  \A,
  \N,
  \X, \Y
{
    A          # is 「A」

  ∈            # an element of

    (          # this Sequence

      X, Y,        # seed values of sequence

      {            # generate the rest of the Seq using this code block

        [+]        # reduce by addition

          @_       # of all previously generated values
          .tail(N) # only use the last 「N」 of them
      }

      ...          # keep generating values until

      * > A        # it is greater than 「A」

    )
}

Brad Gilbert b2gills

Posted 2017-06-16T13:27:58.067

Reputation: 12 713

2

Python 2, 50 bytes

a,n,l=input()
while[a]>l:l=[sum(l[:n])]+l
a in l>x

Takes input as A,N,[Y,X]. Outputs via exit code.

Try it online!

Lynn

Posted 2017-06-16T13:27:58.067

Reputation: 55 648

1

R, 69 60 bytes

function(a,n,l){while(l<a)l=c(sum(l[1:n],na.rm=T),l)
a%in%l}

Try it online!

Returns an anonymous function, taking a,n and a vector l=c(y,x). Constructs the N-bonacci sequence backwards (i.e., smaller index is further in the sequence), since while(l<a) only checks the first element of l.

Giuseppe

Posted 2017-06-16T13:27:58.067

Reputation: 21 077

1

Common Lisp, 164 bytes

(defun f(a n x y &aux(l(list y x)))(if(= n 1)(or(= a x)(= a y))(loop(if(<= a(car l))(return(member a l))(setf l(cons(reduce'+ l)(if(<(length l)n)l(butlast l))))))))

This function returns NIL for false, non-NIL for true (according to the definition of generalized boolean of Common Lisp).

(defun f(a n x y &aux (l (list y x)))    ; initialize a list l for the N values
  (if (= n 1)                            ; special case for N = 1
      (or (= a x) (= a y))               ;    true only if A = X or A = Y
      (loop
        (if (<= a (car l))               ; when the last number generated is greater than A
            (return (member a l))        ; return true if A is in the list
            (setf l (cons (reduce '+ l)  ; otherwise compute the sum of l
                          (if (< (length l) n)   ; and push it to l (truncating the list at 
                              l                  ; end if it has already size = N)
                              (butlast l))))))))

Renzo

Posted 2017-06-16T13:27:58.067

Reputation: 2 260

Does you special-case handling for N=1 detect an A of, e.g., both 1 and/or 2 when X=1 Y=2? My Lisp-reading skills are not great, but it looks like you might only compare A to one of the two initial values. – apsillers – 2017-06-16T23:16:21.533

@apsillers , when N=1 I compare A only with X and not with Y. should I compare it with both returning true if it is equal to one of them? Maybe the sequence is not well defined for this case? – Renzo – 2017-06-16T23:20:09.993

Ok, now I see the question has been changed, I have updated my answer. – Renzo – 2017-06-16T23:24:03.090

0

k, 29 bytes

{x=*(*x>){(x=#y)_y,+/y}[y]/z}

Try it online! 1 is truthy, 0 is falsey. Input is [A;N;X,Y].

zgrep

Posted 2017-06-16T13:27:58.067

Reputation: 1 291

I ran it on all examples that I saw. 1 is truthy, 0 is falsey. – zgrep – 2017-06-16T14:10:29.980

@Gryphon I moved the input over to the footer instead of the body, but I'm not certain what you want me to change. It both is and was the same function. – zgrep – 2017-06-16T14:21:44.953

Oh, I see now. I thought you weren't taking any input, but you were taking it in the code. Makes much more sense now. I don't know k, so I assumed you'd missinterpreted the question, as all it would do was output 1 0 1 1 – Gryphon – 2017-06-16T14:23:11.997

0

Mathematica, 94 bytes

(s={#3,#4};t=1;While[t<#2-1,s~AppendTo~Tr@s;t++];!LinearRecurrence[1~Table~#2,s,#^2]~FreeQ~#)&


input format

[A,N,X,Y]

J42161217

Posted 2017-06-16T13:27:58.067

Reputation: 15 931

0

PHP>=7.1, 103 Bytes

for([,$n,$d,$s,$e]=$argv,$f=[$s,$e];$x<$n;)$x=$f[]=array_sum(array_slice($f,-$d));echo+in_array($n,$f);

Testcases

Jörg Hülsermann

Posted 2017-06-16T13:27:58.067

Reputation: 13 026