Perl6 22 / 30
I'm going to see if Perl6 can deduce the sequence for me.
To do that I used the REPL built into Perl6
$ perl6
> 1,2,4,7...*
Unable to deduce arithmetic or geometric sequence from 2,4,7 (or did you really mean '..'?)
> 1,2,4,7,10...*
1 2 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 ...
Hmm, I see the pattern that Perl deduced. After 4 to get the next value you just add 3.
1,2,4,*+3...*
Which saves one character making the code to get an infinite list of the numbers in the Stöhr sequence 13 characters long.
This code only does something useful in the REPL since it prints the gist of the result for us. To get it to print otherwise you would have to explicitly tell Perl to print the results.
$ perl6 -e 'say 1,2,4,*+3...*'
( * + 3
is simply a way to get a code reference which returns 3 added to it's only argument. Other ways to write it would be { $_ + 3 }
, or -> $i { $i + 3 }
, or { $^i + 3 }
or sub ($i){ $i + 3 }
)
The shortest way to create something Callable to generate the first n elements is to get a slice of the elements.
{(1,2,4,*+3...*)[^$_]} # 22
In void context that would generate the first $_
values, then promptly throw them away.
In anything other than void context it creates an anonymous code block ( a basic subroutine without a name ) which takes one argument.
# store it in a scalar variable
my $sub = {(1,2,4,*+3...*)[^$_]};
say $sub.(5);
# 1 2 4 7 10
# use it immediately
say {(1,2,4,*+3...*)[^$_]}.(5);
# 1 2 4 7 10
# pretend it always had a name
my &Stöhr-first = {(1,2,4,*+3...*)[^$_]};
say Stöhr-first 5;
If you really think it has to have a name to qualify as a valid for this challenge you would probably do this:
sub s(\n){(1,2,4,*+3...*)[^n]} # 30
Though since s
is also used for the substitution operator, to call this the parens are non-optional. ( You could have given it a different name I suppose )
say s(5);
# 1 2 4 7 10