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!
[8, 13, 10]
)? \$\endgroup\$