build a Xbonacci function that takes a signature of X elements ,each next element is the sum of the last X elements ,and returns the first n elements
I think the problem is the slice method but I don't what to use it instead.

It is, but it's not as simple as taking out the slice and using something else in its place, the problem is more fundamental than that.
This algorithm scales poorly to high values of the original length of signature, by the way let's call this value S so I don't have keep spelling it all out. There is an other approach that does not depend on S, by updating the &quot;sum of past S values&quot; in constant time. Instead of explicitly taking the window of S elements and summing it, the sum of the previous window can be updated: one new value enters the window (added to the sum), and one old value leaves the window (subtracted from the sum). The work done for each element of the output is only one addition and one subtraction, not S additions.