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

75 Answers 75

1 2
3
0
\$\begingroup\$

C# - 92 bytes

void f(int n){for(int i=0;i++<n;)Console.WriteLine(string.Join(" ",Enumerable.Range(1,i)));}

Nothing fancy, just a packed function.

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

JavaScript, 73 chars

Not even trying to compare with the others here, just your typical first post:

for(n=prompt(),i=1;n>=i;i++){var l="";for(j=1;j<=i;j++)l+=" "+j;alert(l)}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'm not sure this qualifies because it's alerting n times, which is not the same as outputting a 'right angle triangle' of the entire thing. \$\endgroup\$
    – BadHorsie
    Mar 30, 2015 at 16:22
0
\$\begingroup\$

Python 2 - 57 61 bytes

Had another idea. Only bad thing is the leading space.

def f(n):
 s=''
 for x in range(n):s+=' %s'%(x+1);print s[1:]

example output:

>>> def f(n):
...  s=''
...  for x in range(n):s+=' %s'%(x+1);print s[1:]
... 
>>> f(8)
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
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Change print s to print s[1:]. This gets rid of the first char in the string \$\endgroup\$
    – HEGX64
    Mar 30, 2015 at 7:42
0
\$\begingroup\$

Element, 20

_'[1+2:'.2:`\ ."\
`]

The main idea is that the stacks contain a string and a number. We repeatedly increment the number and append it to the string, printing the result. Given that Element has zero support for arrays, I think this is a very decent score.

Since most people are probably unfamiliar with this language, Element is a golfing language I created in 2012. You can see the most updated interpreter, written in Perl, here.

To make the explanation easier, I'll replace the newline with an L.

_'[1+2:'.2:`\ ."\L`]
_'                    input a number and put it onto the control stack
  [                ]  this is a FOR loop
   1+                 increment the number.  An empty stack is zero.
     2:               make a second copy of it
       '              put one copy on the control stack to save it
        .             append the other copy to the end of the string
         2:           make a second copy of the new string
           `          output one of them
            \ .       append a space to the other
               "      put the number from earlier pack onto the main stack
                \L`   output a newline, but the L should be an actual newline
\$\endgroup\$
0
\$\begingroup\$

Python 2, 60 bytes

One byte less than current best in Python 2, so I've decided to post it.

def f(x):
 if x:f(x-1);print" ".join(map(str,range(1,x+1)))

Usage:

f(5)

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
\$\endgroup\$
0
\$\begingroup\$

Three slightly different approaches

Ruby, 37 bytes

f=->n{n.times{|i|puts [*1..i+1]*" "}}

Ruby, 38 bytes

f=->n{puts (1..n).map{|i|[*1..i]*" "}}

Ruby, 40 bytes

f=->n{([*1..n+1]*" ").scan(/ /){puts$`}}
\$\endgroup\$
0
\$\begingroup\$

UniBasic 50 Bytes

S='';INPUT N;FOR I=1 TO N;S:=I;CRT S;S:=' ';NEXT I

Ungolfed:

S=''
INPUT N
FOR I=1 TO N
  S:=I
  CRT S
  S:=' '
NEXT I
\$\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\$

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\$
2
  • \$\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\$ Jan 20, 2017 at 21:10
  • \$\begingroup\$ @AdmBorkBork I do indeed still lurk around here, thanks for the tips! \$\endgroup\$ Jan 20, 2017 at 22:33
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\$
2
  • \$\begingroup\$ Doesn't this generate a leading new line and a leading space? \$\endgroup\$
    – dwana
    Mar 13, 2015 at 8:46
  • \$\begingroup\$ oh leading space isn't allowed? I thought only trailing space is not allowed \$\endgroup\$
    – Alex
    Mar 13, 2015 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\$
0
\$\begingroup\$

Nibbles, 2 bytes (4 nibbles)

.,$,
.,$,        # full program
.,$,$       # with implicit arg added
.           # map over
 ,          # range from 1..
  $         # input
   ,        # getting the range from 1..
    $       # each number

Fortunately, Nibbles' default formatting for lists-of-lists is a happy match to the required output.

enter image description here

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

Rockstar, 97 93 bytes

listen to N
X's 0
while N-X
build X up
O's 1
Y's 1
while X-Y
let Y be+1
let O be+" "+Y

say O

Try it (Code will need to be pasted in)

Original, 97 bytes

listen to N
X's 0
while N-X
build X up
O's 1
cut O
while X-O
rock O+1 in O

join O with " "
say O
\$\endgroup\$
1 2
3

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.