28
5
Monday Mini-Golf: A series of short code-golf challenges, posted (hopefully!) every Monday.
A Fibonacci-like sequence is obtained using the same method as the famous Fibonacci sequence; that is, each number F(n) is found by adding the previous two numbers in the sequence (F(n) = F(n-1) + F(n-2)), or by subtracting the next two numbers (F(n) = F(n+2) - F(n+1)). The main difference is that these sequences can start with any two numbers. The zero-indexing of these sequences is disputable, but for now, we're going to use this rule:
- The 0th number in a Fibonacci-like sequence is the last number which is smaller than the preceding number.
As an example, the Fibonacci sequence could be written as 1, 0, 1, 1, 2, 3, 5...
, so the 0th number in the sequence is the lone 0
.
Challenge
The goal of the challenge is to write a program or function that takes in three integers, in any format:
- A and B, the two numbers with which to start generating a sequence.
- N, the length of the resulting sequence to output.
And outputs the first N numbers of the sequence, starting at the 0th.
Details
- A, B, and N may be taken in any order and format, as long as they are visibly separated. If you use a different order/format, please specify what it is.
- You may assume that A, B, and N are always positive integers.
- You may assume that N is no more than 100, and the resulting sequence will not contain
x >= 2^31
. - If A is larger than B, then B is the 0th number in the sequence.
- The output must be separated by spaces, commas, and/or newlines.
- A trailing space or newline is allowed, but not a trailing comma.
Test-cases
Example 1:
8 13 10
Working backward from 8 13
until we find a number larger than the previous, we get 13 8 5 3 2 1 1 0 1
. Thus, 0
is the 0th number in this sequence. Working forward from this, we print out 0
and the next 9 members:
0 1 1 2 3 5 8 13 21 34
Example 2:
23 37 5
Again working backward to find the 0th number, we find 37 23 14 9 5 4 1 3
. The 0th number this time is 1
, so we print it out, along with the next 4 members:
1 4 5 9 14
Example 3:
4 3 8
With this one, we don't have to work backward to find the 0th number, because 3
is smaller than 4
:
3 7 10 17 27 44 71 115
Example 4:
29 47 11
Result:
1 3 4 7 11 18 29 47 76 123 199
Scoring
This is code-golf, so shortest valid code in bytes wins. Tiebreaker goes to earlier posted submission. The winner will be chosen next Monday, Sep 28. Good luck!
Edit: Congrats to your winner, @Jakube, using Pyth for an amazing 23 bytes!
10I've removed the [monday-mini-golf] tag you created. I don't think we should create tags for more or less arbitrary groups of challenges. The tag doesn't really tell you anything about the challenge, and if you want to find all of these, one could just search for the phrase in search bar. Alternatively, if you include a link to this first challenge in every future instalment, they will all be linked under "Linked questions" in the sidebar. – Martin Ender – 2015-09-21T20:48:56.257
@MartinBüttner OK, thanks; I'll keep that in mind. – ETHproductions – 2015-09-21T20:49:38.380
Can I take the input how I want it (A python list literal
[8, 13, 10]
)? – Blue – 2015-09-21T20:57:04.963@muddyfish Sure, as long as you specify in your answer the format used. – ETHproductions – 2015-09-21T20:59:37.007
Is the solution
3 7 10 17 27 44 71 115
correct? – Luis Mendo – 2015-09-21T21:36:49.650@LuisMendo I believe so;
4+3=7
,3+7=10
, etc. and the4
is not printed, since it has an index of-1
. Are you getting a different result? – ETHproductions – 2015-09-21T21:40:39.850No, I think I had got the question wrong. Thanks! – Luis Mendo – 2015-09-21T21:52:42.550
Can the input be an array of the two numbers and then the third number? That is,
[8 3], 10
– Luis Mendo – 2015-09-21T22:10:02.7873The challenge currently says write a program. Does that mean that functions are not allowed? (CC @LuisMendo) – Dennis – 2015-09-21T22:11:47.220
@Dennis Hm, good point. OP please clarify! – Luis Mendo – 2015-09-21T22:13:57.757
3@Dennis Sorry, slipped my mind. Functions are also allowed. Thanks for pointing that out! – ETHproductions – 2015-09-21T23:59:01.430
@LuisMendo That's alright; just be sure to specify it in your answer. – ETHproductions – 2015-09-21T23:59:28.057