12
3
I am learning Ruby and wrote my first nontrivial code to solve this problem.
The challenge is to generate the first n elements of the Stöhr sequence, S, which is defined as follows:
S[0] = 1
S[n] is the smallest number that cannot be expressed as the sum of two distinct previous elements in the sequence.
Thus the sequence begins with 1, 2, 4, 7, and 10. The next element is 13, because 11 (=1+10) and 12 (=2+10) are sums of previous elements, but 13 is not.
I am looking for the shortest code. My own, in Ruby, is 108 characters long, but maybe I'll wait to see what others come up with before posting it?
I like the answers so far. Now, it's perhaps too late to go back and change the requirements, but I suppose I should have mentioned that I'm particularly interested in solutions that use the definition of the sequence itself (i.e., the code doesn't know in advance that eventually the numbers go up by 3). So: moral bonus points if you can do that. – Théophile – 2015-01-26T13:09:20.300
Such is the problem with mathematical sequences. If you know the pattern, it will usually be shorter. – None – 2015-01-27T04:41:37.010
This sequence is arithmetical without any use(?). – user75200 – 2018-01-06T19:39:54.267
@user75200 The sequence is not arithmetical, as you can see from the differences in the first three elements, but the subsequence starting at the third element is indeed arithmetical. It is used in connection to the Postage Stamp problem. – Théophile – 2018-01-06T22:12:25.437