9
1
Sequences
You are given four number sequences, numbered 1
through 4
.
OEIS The location of
0
's when the natural numbers are listed in binary. Here's an example of how to calculate the sequence:0,1,10,11,100,101,110,111 ^ ^ ^^ ^ ^ 0 3 78 10 14
The start of the sequence goes like this:
0, 3, 7, 8, 10, 14, 19, 20, 21, 23, 24, 27, 29, 31, 36, 37, 40, 45, 51, ...
OEIS This sequence includes the first natural number, skips the next two, then includes the next three, then skips the next four, and continues.
0, 3, 4, 5, 10, 11, 12, 13, 14, 21, 22, 23, 24, 25, 26, 27, 36, ...
OEIS Positive integers where both the number of
0
's and the number of1
's in the binary representation of the number are powers of2
.2, 4, 5, 6, 9, 10, 12, 16, 23, 27, 29, 30, 33, 34, 36, 39,
OEIS The Hofstadter Q sequence.
a(1) = a(2) = 1;
a(n) = a(n-a(n-1)) + a(n-a(n-2)) for n > 2.1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6, 8, 8, 8, 10, 9, 10, 11, 11, 12, 12, 12, 12, 16, 14, ...
Little is proven rigorously about this sequence, but many empirical results exist. One is particularly important, and you may assume it's valid for the entire series:
This paper observed that the elements of the series can be grouped into generations. If we number them starting at 1, then the kth generation contains exactly 2k elements. The relevant property is that all numbers in generation k are obtained by summing two numbers from generations k-1 and/or k-2, but never from earlier generations. You may use this (and only this) observation to put a lower bound on the remaining elements in the sequence.
Challenge
Your challenge is to print the first x
numbers in the intersection of the given input sequences.
Input: Two numbers separated by a space on STDIN
. The first number is an integer from 1
to 15
inclusive where each bit corresponds to a sequence. The lowest bit corresponds to sequence 1
, and the highest corresponds to sequence 4
. The second is amount of numbers, x
, to output on STDIN
.
Output: The first x
numbers that intersect with the given input sequences. Print the numbers on STDOUT
with any clear whitespace or punctuation as a delimiter (spaces, tabs, newlines, commas, colons, periods, etc).
Examples
1. Print the first 3
numbers that are in every sequence.
Input: 15 3
Output: 10,23,40
2. Print the first 12
numbers in sequences number 1
and 4
.
Input: 9 12
Output: 3,8,10,14,19,20,21,23,24,31,37,40
3. Print the first 10
numbers in sequence 2
.
Input: 2 10
Output: 0,3,4,5,10,11,12,13,14,21
4. Print the first 6
numbers in sequences 3
and 4
.
Input: 12 6
Output: 2,4,5,6,9,10
Details
- You may print the output as you go or all at once at the end.
Huge thanks to everyone who helped out with this in chat! This question benefited greatly from being in the sandbox.
@chilemagic: Actually how do you define "first X numbers" in an intersection? If you take both sequences in the
12 5
example up to the same index, then10
does indeed come before9
in the intersection... like, how would you, while going through the sequences, decide whether to skip the9
in #3 as a possible intersection? Like if #3 had7
in it then you would be required to skip over it since that doesn't appear in #4 – Claudiu – 2014-11-17T20:15:33.613@Claudiu Your outputted numbers should always be increasing, and each number would only appear once in your output. – hmatt1 – 2014-11-17T20:21:53.777
Is there a maximum limit to
x
? – Ypnypn – 2014-11-18T01:18:06.817@ypnypn don't hard code a limit, but if your algorithm is very slow or won't finish for very large inputs that's ok. This is code golf so you can be inefficient to save bytes. – hmatt1 – 2014-11-18T02:33:48.890