25
\$\begingroup\$

Given a number N, output a NxN right angled triangle, where each row i is filled with numbers up to i.

Example

n = 0

(no output)

n = 4

1
1 2
1 2 3
1 2 3 4

n = 10

1
1 2
1 2 3
.
.
.
1 2 3 4 5 6 7 8 9 10

(no alignment needed)

n = N

1
1 2
1 2 3
.
.
.
1 2 3 4 .... N

There is no trailing space at the end of each line.

Least number of bytes wins, and standard loopholes are not allowed.

\$\endgroup\$
  • \$\begingroup\$ Can the output be a nested list of numbers? \$\endgroup\$ – seequ Mar 12 '15 at 5:39
  • \$\begingroup\$ What should be the behavior for n=0, and for n>9? \$\endgroup\$ – freekvd Mar 12 '15 at 18:55
  • \$\begingroup\$ @Sieg Sure, as long as the output is correct. \$\endgroup\$ – Tan WS Mar 13 '15 at 0:57
  • \$\begingroup\$ @freekvd for 0 there is no output, for n>9 no special formatting required \$\endgroup\$ – Tan WS Mar 13 '15 at 1:00
  • \$\begingroup\$ Ah darn, you broke my submission. Fixing ASAP \$\endgroup\$ – seequ Mar 13 '15 at 5:54

69 Answers 69

0
\$\begingroup\$

awk, 27 bytes

{for(n=$0;n>=$i=++i;)print}

Got it working with some tweaks.

Example usage and output

me@home:~$ echo 0 | awk '{for(n=$0;n>=$i=++i;)print}'
me@home:~$ echo 1 | awk '{for(n=$0;n>=$i=++i;)print}'
1
me@home:~$ echo 2 | awk '{for(n=$0;n>=$i=++i;)print}'
1
1 2
me@home:~$ echo 12 | awk '{for(n=$0;n>=$i=++i;)print}'
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12
\$\endgroup\$
0
\$\begingroup\$

q, 18 bytes

1+til each 1+til n
\$\endgroup\$
0
\$\begingroup\$

MATLAB, 63

for m=1:input('');fprintf(['1' repmat(' %d',1,m-1) 10],2:m);end

Not the best, but because of the requirements on spaces, functions like num2str and disp can't be used as they produce too many.

Run the code and enter the number when prompted (~=STDIN). It will then print the number triangle.

\$\endgroup\$
0
\$\begingroup\$

MATLAB, 38

@(n)spy(sparse(tril(repmat(1:n,n,1))))

This one is a bit of a wildcard answer. It doesn't print the output, but rather makes a pretty graph. I know this is probably outside the rules, if so refer to my other answer, but I thought I would post this one anyway as it looks quite cool :)

enter image description here

\$\endgroup\$
0
\$\begingroup\$

JavaScript, 77 Bytes

function(n){for(i=1;i<=n;i++){a='';for(j=1;j<=i;j++)a+=j+' ';console.log(a)}}
\$\endgroup\$
0
\$\begingroup\$

PowerShell -  42 32bytes

Edit: AdmBorkBork reports that we can save many bytes by using a complete script, and assuming a default delimiter!

 param($n)0..$n-gt0|%{"$(1..$_)"}

Never done any serious PowerShell, just felt I might learn something from this, and indeed I did. Old 42byte code:

function k($n){0..$n-gt0|%{1..$_-join" "}}

Defines a function k which takes an input $n, creates a range from 0 through to $n, filters for entires greater than 0, then creates a range from 1 to each remaining value, and joins these with a space. Example out:

PS > k 3
1
1 2
1 2 3
PS > k 0
PS >

Alternatively, you can define a filter, which will print as many triangles as you want.

filter z{0..$_-gt0|%{1..$_-join" "}}

PS > 3,0,2|z
1
1 2
1 2 3
1
1 2
PS >
\$\endgroup\$
  • \$\begingroup\$ Hiya! Not sure if you're still around, but you can use a full script instead of a function and use the default $OutputFieldSeparator to get down to 32 bytes with param($n)0..$n-gt0|%{"$(1..$_)"} -- Try it online! \$\endgroup\$ – AdmBorkBork Jan 20 '17 at 21:10
  • \$\begingroup\$ @AdmBorkBork I do indeed still lurk around here, thanks for the tips! \$\endgroup\$ – VisualMelon Jan 20 '17 at 22:33
0
\$\begingroup\$

Pushy, 4 bytes

Non-competing as the language postdates the challenge.

:Lh_

Try it online!

This works by building the stack up to the final line, step-by-step, and printing the stages:

:    \ Input times do (this consumes input):
Lh   \  Push stack length + 1
_    \  Print stack items, space separated.
\$\endgroup\$
0
\$\begingroup\$

VBA, 116 bytes

Function n(m As Integer)
If m > 0 Then 
For a = 1 To m 
n = n & " " & a 
Next 
Debug.Print n(m - 1) 
End If 
End Function
\$\endgroup\$
  • \$\begingroup\$ Doesn't this generate a leading new line and a leading space? \$\endgroup\$ – dwana Mar 13 '15 at 8:46
  • \$\begingroup\$ oh leading space isn't allowed? I thought only trailing space is not allowed \$\endgroup\$ – Alex Mar 13 '15 at 14:22
0
\$\begingroup\$

05AB1E, 3 bytes

LL»

Try it online.

Explanation:

L      # Create a list in the range [1, (implicit) input]
 L     # Map each integer `i` to a list in the range [1, `i`]
  »    # Join the list of lists by newlines,
       # which implicitly also joins the inner lists by a single space
       # (and output it implicitly)
\$\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.