The Fibonacci sequence is a fairly well known thing around here. Heck, it even has its own tag. However, for all that, we sure like to stick to our roots of 1, 1, ... (or is it 0, 1, ...? We may never know...). In this challenge, the rules are the same, but instead of getting the nth item in the Fibonacci sequence, you will get the nth item in the Fibonacci-esque sequence starting with x, y, ....

Input

Three integers, in whatever order you want. n is the index (0 or 1 indexed) of term in the sequence for your output. x and y are the first two items in your current program run's Fibonacci sequence.

\$\begingroup\$@StephenS How about taking an input as n,[x,y] where n is a number and x and y are numbers in a list? That's probably being a little too flexible though ;)\$\endgroup\$
– TomMay 17 '17 at 14:54

1

\$\begingroup\$@CAD97 I'll add them, I had forgotten about them :)\$\endgroup\$
– StephenMay 17 '17 at 17:48

42 Answers
42

How it works

+¡ạ Main link. Left argument: x. Right argument: y. Third argument: n
ạ Yield abs(x - y) = y - x, the (-1)-th value of the Lucas sequence.
+¡ Add the quicklink's left and right argument (initially x and y-x), replacing
the right argument with the left one and the left argument with the result.
Do this n times and return the final value of the left argument.

Input format is "x y n".
I'm still a noob at this, so I'm 100% sure there are better ways to do this, but please instead of telling me "do this" try to only give me hints so that I can find the answer myself and get better. Thanks!

Try it online! 0-indexed. Use as (x#y)n, e.g. (0#1)5 for the fifth element of the original sequence.

The most likely shortest way to get the Fibonacci sequence in Haskell is f=0:scanl(+)1f, which defines an infinite list f=[0,1,1,2,3,5,8,...] containing the sequence. Replacing 0 and 1 with arguments x and y yields the custom sequence. (f!!) is then a function returning the nth element of f.

µ¡ - repeat n times (computes the n+1th and n+2th element):
S - take the sum of the elements of the previous iteration (starting at (x,y))
; - append to the end of the previous iteration
Ḋ - remove the first element
I - Take the difference of the n+1th and n+2th to get the n-th.

Explanation

Let the inputs be denoted n (index), a, b (initial terms).

:" % Implicitly input n. Do this n times
% At this point in each iteration, the stack contains the two most
% recently computed terms of the sequence, say s, t. In the first
% iteration the stack is empty, but a, b will be implicitly input
% by the next statement
w % Swap. The stack contains t, s
y % Duplicate from below. The stack contains t, s, t
+ % Add. The stack contains t, s+t. These are now the new two most
% recently comnputed terms
] % End
x % Delete (we have computed one term too many). Implicitly display

R, 39 bytes

f=function(x,y,n)'if'(n,f(y,x+y,n-1),x)

A simple recursive function. Funnily enough this is shorter than anything I can come up with for the regular Fibonacci sequence (without built-ins), because this doesn't have to assign 1 to both x and y =P

Calculates n+1 numbers of the sequence, including the initial values. Each recursion is calculates with n-1 and stopped when n==0. The lowest of the two numbers is then returned, giving back the n-th value.

Your Answer

If this is an answer to a challenge…

…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.

…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

…Please make sure to answer the question and provide sufficient detail.

…Avoid asking for help, clarification or responding to other answers (use comments instead).

Code Golf Stack Exchange is a site for recreational programming competitions, not general programming questions. Challenges must have an objective scoring criterion, and it is highly recommended to first post proposed challenges in the Sandbox.