26
1
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 n
th item in the Fibonacci sequence, you will get the n
th 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.
Output
The n
th term in the Fibonacci sequence starting with x
, y
.
Test Cases
(0-indexed)
n x y out
5 0 0 0
6 0 1 8
6 1 1 13
2 5 5 10
10 2 2 178
3 3 10 23
13 2308 4261 1325165
0 0 1 0
1 0 1 1
(1-indexed)
n x y out
6 0 0 0
7 0 1 8
7 1 1 13
3 5 5 10
11 2 2 178
4 3 10 23
14 2308 4261 1325165
1 0 1 0
2 0 1 1
Caveats
Assume 0 <= x <= y
.
Please note your input order (must be constant).
Can we take a list as input? – Business Cat – 2017-05-17T14:41:12.567
@BusinessCat you mean like
[1, 2, 3]
? Yes. Whatever you need to accept 3 integers. – Stephen – 2017-05-17T14:42:05.027@StephenS How about taking an input as
n,[x,y]
wheren
is a number andx
andy
are numbers in a list? That's probably being a little too flexible though ;) – Tom – 2017-05-17T14:54:16.620@Tom as long as all you are doing is accepting 3 integers, I don't care how you format your input (so yes, that would be acceptable. You only can't use anything in the input besides the 3 integers as information). – Stephen – 2017-05-17T14:58:09.597
Related (Numberphile), Related (OEIS) (I was hoping the Brady Numbers would be a test case...) – CAD97 – 2017-05-17T17:41:50.950
1@CAD97 I'll add them, I had forgotten about them :) – Stephen – 2017-05-17T17:48:10.650
I honestly don't understand why this isn't being considered as a dupe. In most languages, surely, it's simply going to be a case of replacing the 2 initial values of x & y with input values? I've seen the dupe hammer swung on questions that differ a lot more than these two. – Shaggy – 2017-05-17T22:04:24.197
@Shaggy it was dupe hammered (not 5 votes) then undupe hammered (not 5 votes). Dunno who unduped it. – Stephen – 2017-05-17T22:15:20.403
1Related – xnor – 2017-05-17T22:55:16.710
There are currently no testcases for
n=1
(or0
if indexed as such), do we need to be able to handle this? – JAD – 2017-05-18T07:11:49.333@JarkoDubbeldam yes, editing in, thank you – Stephen – 2017-05-18T16:00:23.140
Quite related. The only difference in that challenge is that you have to first work backwards until you find the "first item" in the custom sequence. – ETHproductions – 2017-07-07T17:32:39.023
Can I
-1
-index? Returny
forn == 0
,x+y
forn == 1
, and so on? – Khuldraeseth na'Barya – 2018-04-21T23:34:20.337@Scrooble As long as you return
x
forn = -1
– Stephen – 2018-04-21T23:48:21.290