I need to create a list that holds, for given integer d
, the elements
$1,\ldots,d,d+2,\ldots,2d,2d+3,\ldots,3d,3d+4,\ldots,d^2$ where $\ldots$ is just denoting increment by 1 until the next written value is reached (as usual).
My naive attempt is this:
ClearAll[list];
list[d_] := Module[{tmp = {}, n = 0}, While[(n + 1)*d <= d^2, AppendTo[tmp, Range[n*(d + 1) + 1, (n + 1)*d]]; n++]; Flatten@tmp];
which produces what I want, e.g.
list[3]
(* {1,2,3,5,6,9} *)
However, I would be very interested how (or maybe if) this can be achieved without using things like While
and For
. I guess there is a nice approach for this...
Update
Here are some timings for the current approaches on my machine:
At least for larger d
, the second solution by @ciao scales best (although only marginally faster than the one by @gpap). Since ciao's approach is also faster than gpap's for small d
, I decided to accept his solution. But all approaches are very nice, so it was a bit difficult to choose the "one" that will be accepted.
Table
:) So, yes, this would be an option (+1) $\endgroup$