7
$\begingroup$

Here is my code, which works, but seems a little too long, I think might be a more refined version.

Range[Ceiling[#/2]] ~ Join ~ 
  If[OddQ[#], Rest, Identity] @ Reverse @ Range[Ceiling[#/2]] & /@ Range[9]

Range[Ceiling[#/2]] ~ Join ~ Range[Floor[#/2], 1, -1] & /@ Range[9]
{{1}, {1, 1}, {1, 2, 1}, {1, 2, 2, 1}, {1, 2, 3, 2, 1}, {1, 2, 3, 3,  2, 1}, 
 {1, 2, 3, 4, 3, 2, 1}, {1, 2, 3, 4, 4, 3, 2, 1}, {1, 2, 3, 4, 5, 4, 3, 2, 1}}
$\endgroup$
  • 1
    $\begingroup$ Can I ask downvoters about the reason? I do not claim they have no reason, I just can't see any now. $\endgroup$ – Kuba Sep 14 '13 at 7:47
  • $\begingroup$ @kuba Perhaps it's the Anti-Infix-Police? $\endgroup$ – cormullion Sep 14 '13 at 8:02
  • $\begingroup$ @cormullion so we do not like Infix here? :) I was not aware of that $\endgroup$ – Kuba Sep 14 '13 at 8:05
  • $\begingroup$ Talking of infix ~Join~ - innumerable times I've pondered why Mathematica doesn't have a better infix shorthand for it. There's <> for StringJoin, after all. $\endgroup$ – kirma Sep 14 '13 at 8:31
14
$\begingroup$
Table[Min[i, n + 1 - i], {n, 9}, {i, n}]
| improve this answer | |
$\endgroup$
  • $\begingroup$ This is much more intuitive than mine! :) $\endgroup$ – kirma Sep 14 '13 at 8:58
6
$\begingroup$

The question is in my opinion more about math than Mathematica and so is my answer (unless the goal is to create these lists through specific constructs), but here it is nonetheless:

Table[(1 + x - Abs[1 - 2 n + x]) / 2, {x, 9}, {n, x}]
| improve this answer | |
$\endgroup$
2
$\begingroup$

Although I think Ray Koopman's method is the way to go, I cannot pass on pointing out the similarity to TriangleWave, therefore:

Table[n/2 TriangleWave[i/2/n], {n, 2, 10}, {i, n - 1}] // Simplify
{{1}, {1, 1}, {1, 2, 1}, {1, 2, 2, 1}, {1, 2, 3, 2, 1}, {1, 2, 3, 3, 2, 1},
 {1, 2, 3, 4, 3, 2, 1}, {1, 2, 3, 4, 4, 3, 2, 1}, {1, 2, 3, 4, 5, 4, 3, 2, 1}}

Simplify is needed because TriangleWave does not fully evaluate in some cases without it.

| improve this answer | |
$\endgroup$
1
$\begingroup$
# + 1 - Abs@Range[-#, #] & /@ ((Range@9 - 1)/2)
| improve this answer | |
$\endgroup$
0
$\begingroup$

Just for the heck of it, a recursive solution:

f[1] = {1};
f[n_] := f[n] = With[{list = f[n - 1]},
    If[EvenQ[n - 1],
     Insert[list, 1 + list[[(n - 1)/2]], (n + 1)/2],
     Insert[list, list[[n/2]], 1 + n/2]]
    ];

Table[f[k], {k, 1, 10}]

!!!

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.