8
$\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$
4
  • 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, 2013 at 7:47
  • $\begingroup$ @kuba Perhaps it's the Anti-Infix-Police? $\endgroup$
    – cormullion
    Sep 14, 2013 at 8:02
  • $\begingroup$ @cormullion so we do not like Infix here? :) I was not aware of that $\endgroup$
    – Kuba
    Sep 14, 2013 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, 2013 at 8:31

5 Answers 5

16
$\begingroup$
Table[Min[i, n + 1 - i], {n, 9}, {i, n}]
$\endgroup$
1
  • $\begingroup$ This is much more intuitive than mine! :) $\endgroup$
    – kirma
    Sep 14, 2013 at 8:58
7
$\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}]
$\endgroup$
3
$\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.

$\endgroup$
2
$\begingroup$
# + 1 - Abs@Range[-#, #] & /@ ((Range@9 - 1)/2)
$\endgroup$
1
$\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}]

!!!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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