Print number triangle

25

3

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.

Tan WS

Posted 2015-03-12T05:18:47.283

Reputation: 359

Can the output be a nested list of numbers? – seequ – 2015-03-12T05:39:31.920

What should be the behavior for n=0, and for n>9? – freekvd – 2015-03-12T18:55:39.633

@Sieg Sure, as long as the output is correct. – Tan WS – 2015-03-13T00:57:08.980

@freekvd for 0 there is no output, for n>9 no special formatting required – Tan WS – 2015-03-13T01:00:19.673

Ah darn, you broke my submission. Fixing ASAP – seequ – 2015-03-13T05:54:17.417

Can you include an example larger than 10 we can see how two double-digit numbers should look together? – FlipTack – 2017-01-21T08:24:34.543

Answers

17

Joe, 5 3 bytes (+2 or +3 for -t flag)

Well, apparently I didn't utilize the full potential of Joe. This was possible back when I first posted this.

\AR

Here, R gives the range from 0 to n, exclusive. Then \A takes successive prefixes of it (A is the identity function). Examples:

With -t flag (note: this is now the standard output even without the flag):

   (\AR)5
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
   \AR5
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
   \AR2
0
0 1
   \AR1
0
   \AR0

Without it:

   \AR5
[[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4]]
   (\AR)5
[[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4]]
   \AR2
[[0], [0, 1]]
   \AR1
[[0]]
   \AR0
[]

The rules got changed a bit. My old code didn't behave correctly with N =0. Also, now output could be just a nested list, so -t can be dropped.

1R1+R

Now, Rn gives a range from 0 to n, exclusive. If given 0, it returns an empty list. 1+ adds 1 to every element of that range. 1R maps the values to ranges from 1 to x. Empty liats, when mapped, return empty lists.

Example output:

   1R1+R0
[]
   1R1+R5
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]

Update: I just noticed something. The function automatically maps to rank 0 elements. The following example is run with -t flag.

   1R1+R3 5 8
1
1 2
1 2 3

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

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

Old: 5 bytes (with the -t flag)

1R1R

This is an anonymous function which takes in a number, creates a list from 1 to N (1Rn) and maps those values to the preceding range, giving a range from 1 to x for each item of range 1 to N.

The -t flag gives output as a J-like table.

   1R1R5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Note : the language is very new and not complete, but the latest version was released before this challenge.

seequ

Posted 2015-03-12T05:18:47.283

Reputation: 1 714

4So J was not enough for having an advantage on array based challenges ? :D – Optimizer – 2015-03-12T05:51:00.403

4@Optimizer Optimizing is important. – seequ – 2015-03-12T05:57:56.660

4Suddenly, my most voted answer is the one I spent the least time on. The injustice. – seequ – 2015-03-12T18:52:03.213

1I guess Joe is not your average Joe... – Justin – 2015-03-14T18:43:19.423

10

Python 3, 48 45 bytes

f=lambda n:n and[f(n-1),print(*range(1,n+1))]

Hooray for side effects.

Sp3000

Posted 2015-03-12T05:18:47.283

Reputation: 58 729

2Nested nothingness. Now that's twisted. – seequ – 2015-03-12T20:26:54.233

That's a nifty trick: putting the function before the print to execute the prints in reverse order. – xnor – 2015-03-13T21:49:38.410

8

APL, 5

⍪⍳¨⍳⎕

creates a vector 1..n and for each element another such vector.

Then ⍪ makes a column out of all vectors. This avoids the problem with trailing blanks.

Try it on tryapl.org


Older solution:

{⎕←⍳⍵}¨⍳⎕

Creates a vector 1..n

{⎕←⍳⍵} is a function that outputs for each (¨) element a vector 1..n on a separate line

This one can't be tried on tryapl.org unfortunately, because ⎕← doesn't work there.

Moris Zucca

Posted 2015-03-12T05:18:47.283

Reputation: 1 519

There should be no trailing spaces in any line. – randomra – 2015-03-12T09:04:54.720

ah thank you, i missed that one. Will correct soon – Moris Zucca – 2015-03-12T09:14:44.063

I knew APL would be a solution – Conor O'Brien – 2015-09-30T21:33:12.243

Oh God, what are my eyes seeing – Codefun64 – 2015-10-03T18:44:48.620

6

J, 27 bytes

J is not good with non-array numeric output. This function creates a properly formatted string from the numbers.

   ;@(<@,&LF@":@:>:@:i.@>:@i.)

   (;@(<@,&LF@":@:>:@:i.@>:@i.)) 4
1
1 2
1 2 3
1 2 3 4

Try it online here.

randomra

Posted 2015-03-12T05:18:47.283

Reputation: 19 909

You could also use ]\@i. to get ;@(<@,&LF@":@:>:@:]\@i.) – seequ – 2015-03-21T11:32:24.660

6

PHP, 53 Bytes

Edit 2: Ismael Miguel suggested reading from input instead of defining a function, so the score is now 53 bytes for PHP:

for($a=1;@$i++<$n=$argv[1];$a.=" ".($i+print"$a\n"));

And once again, it can be improved if PHP is configured to ignore errors (52 bytes):

for($a=1;$i++<$n=$argv[1];$a.=" ".($i+print"$a\n"));
for($a=1;$i++<$n=$_GET[n];$a.=" ".($i+print"$a\n"));

Edit: Austin suggested a 60 bytes version in the comments:

function f($n){for($a=1;@$i++<$n;$a.=" ".($i+print"$a\n"));}

Which can be improved if we doesn't display PHP errors (59 bytes):

function f($n){for($a=1;$i++<$n;$a.=" ".($i+print"$a\n"));}

$a stores the next line that will be printed, and each time it's printed a space and the next number (print always returns 1) are concatened to it.


Recursive functions (65 bytes):

function f($n){$n>1&&f($n-1);echo implode(' ',range(1,$n))."\n";}
function f($n){$n>1&&f($n-1);for(;@$i++<$n;)echo$i,' ';echo"\n";}   // Using @ to hide notices.

Shorter recursive function, with error reporting disabled (64 bytes):

function f($n){$n>1&&f($n-1);for(;$i++<$n;)echo$i,' ';echo"\n";}

Even shorter recursive function, with error reporting disabled and an empty line before real output (62 bytes):

function f($n){$n&&f($n-1);for(;$i++<$n;)echo$i,' ';echo"\n";}

Just for fun, non-recursive fucntions:

function f($n){for($i=0;$i<$n;print implode(' ',range(1,++$i))."\n");}    // 70 bytes
function f($n){for(;@$i<$n;print implode(' ',range(1,@++$i))."\n");}      // 68 bytes, hiding notices.
function f($n){for(;$i<$n;print implode(' ',range(1,++$i))."\n");}        // 66 bytes, error reporting disabled.

Benoit Esnard

Posted 2015-03-12T05:18:47.283

Reputation: 311

245 bytes: for($a=1;@$i<$n;$a.=" ".(@++$i+print"$a\n")); – Austin – 2015-03-13T08:14:54.220

@Austin: I've read in a comment that the code must be either a full program reading from input, or a function. Very nice trick, it can be improved by a bit / byte: for($a=1;@$i++<$n;$a.=" ".($i+print"$a\n")); (44 bytes) – Benoit Esnard – 2015-03-13T09:29:05.580

Ah ok, then I suppose you would do function f($n){for($a=1;@$i++<$n;$a.=" ".($i+print"$a\n"));}, which is 60 bytes. – Austin – 2015-03-13T10:19:37.490

Indeed. Are you OK if I edit my answer to add your solution? – Benoit Esnard – 2015-03-13T10:24:24.750

Sure, go for it. – Austin – 2015-03-13T20:10:37.597

1for($a=1;$i++<$n=$_GET[n];$a.=" ".($i+print"$a\n")); --> try this (full code, using url parameter n with the number) – Ismael Miguel – 2015-03-14T22:32:13.080

@IsmaelMiguel: I've edited the answer with your idea. Using $argv[1] is better for the no-error version. Too bad we can't use $argc directly. – Benoit Esnard – 2015-03-14T22:56:46.840

I have a shorter one for you, but only for PHP 4: for($a=1;@$i++<$n=$N;$a.=" ".($i+print"$a\n"));, pass the URL parameter N=x. This works only on PHP<4.2 because of the directive register_globals (http://php.net/manual/en/ini.core.php#ini.register-globals) which automatically registers the variables in $_POST, $_GET and so on to the current code automatically. $_GET['Z'] will be registed as the variable $Z. This value defaults to on on those old versions. To work for PHP<5.3, you have to create a php.ini file with register_globals=on

– Ismael Miguel – 2015-03-15T01:55:24.130

@BenoitEsnard You have to do $argv[1]*1 or $argv[1]+0, otherwise it will print 1 if you give it 0 when it is supposed to print nothing. At least, that's what happens in PHP 5.5. – Austin – 2015-03-15T06:13:14.807

You can save 3 bytes removing unnecessary $n= in loop condition. – avall – 2015-03-15T21:18:58.263

@Austin this can be done by changing @$i++= to @++$i<= so only one character must be added. – avall – 2015-03-15T21:21:41.023

5

CJam, 13 12 bytes

ri{),:)S*N}/

How it works:

ri{       }/     "Run the block input number of times with iteration index from 0 to N-1";
   )             "Increment the iteration index (making it 1 to N)";
    ,            "Get an array of 0 to iteration index";
     :)          "Increment each of the above array members by 1";
       S*        "Join all above array numbers with space";
         N       "Add a new line. After all iterations, things are automatically printed";

Try it online here

Optimizer

Posted 2015-03-12T05:18:47.283

Reputation: 25 836

4

JavaScript (ES6) 49 52

Such a simple task, I wonder if this can be made shorter in JS (Update: yes, using recursion)

Recursive 49

f=n=>alert((r=w=>n-i++?w+'\n'+r(w+' '+i):w)(i=1))

Iteraive 52

f=n=>{for(o=r=i=1;i++<n;o+='\n'+r)r+=' '+i;alert(o)}

edc65

Posted 2015-03-12T05:18:47.283

Reputation: 31 086

Where can I test this? I cant seem to find any ES6 playgrounds that accepts this – Kristoffer Sall-Storgaard – 2015-03-12T11:53:23.867

@KristofferSall-Storgaard Firefox supports ES6 be default. So Firefox Console. – Optimizer – 2015-03-12T13:42:24.030

4

Pyth, 9 bytes

VQjdr1hhN

Really thought that this can be done shorter, but it doesn't seem so.

Try it online.

            Q = input()
VQ          For N in [0, 1, ..., Q-1]:
    r1hhN       create list [1, ..., N+1+1-1]
  jd            print joined with spaces

Jakube

Posted 2015-03-12T05:18:47.283

Reputation: 21 462

1An alternative 9: VQaYhNjdY. If only a returned the list, then something like VQjdaYhN would be 8. – Sp3000 – 2015-03-12T12:15:29.863

2a briefly used to return the appended list. – Optimizer – 2015-03-12T12:47:14.270

I'm not familiar with Pyth, so could you elaborate why N+1+1-1? – seequ – 2015-03-12T20:30:48.307

1@Sieg r is the Python-range function, therefore the -1 (r1N creates the list [1, 2, ..., N-1]). But in the Nth iteration of the loop, I want the list [1, 2, ..., N+1], therefore I need to add 2 to N. r1hhN translates directly to range(1, N+1+1). Another possibility would be r1+N2 (range(1, N+2)). – Jakube – 2015-03-12T21:12:51.203

Or even mhdhN, but that's a complete different approach. – Jakube – 2015-03-12T21:14:38.720

4

Java, 85 84 bytes

This is surprisingly short in Java.

void a(int a){String b="";for(int c=0;c++<a;System.out.println(b+=(c>1?" ":"")+c));}

Indented:

void a(int a){
    String b="";
    for(int c=0;
        c++<a;
        System.out.println(
                b+=(c>1?" ":"")+c
        ));
}

1 byte thanks to Bigtoes/Geobits

TheNumberOne

Posted 2015-03-12T05:18:47.283

Reputation: 10 855

You can save one by moving the b+=... into println(b+=...). – Geobits – 2015-03-12T13:15:03.307

3

><> (Fish), 40 37 + 3 = 40 bytes

&1>:&:&)?;1\
(?v:n" "o1+>}:{:@
ao\~1+

Once again, ><> does decently well at another number printing exercise. Run with the -v flag for input, e.g.

py -3 fish.py -v 4

Explanation

&               Put n in register
1               Push 1 (call this "i")

[outer loop]

:&:&)?          If i > n...
;                 Halt
1                 Else push 1 (call this "j")

[inner loop]

}:{:@(?         If j > i...
~1+ao             Pop j, print newline, increment i and go to start of outer loop
:n" "o1+          Else print j, print a space, increment j and go to start of inner loop

Sp3000

Posted 2015-03-12T05:18:47.283

Reputation: 58 729

3

Prolog - 119

h(N):-setof(X,(between(1,N,K),setof(Y,between(1,K,Y),X)),[L]),k(L),nl,fail.
k([A|B]):-write(A),(B=[];write(" "),k(B)).

Οurous

Posted 2015-03-12T05:18:47.283

Reputation: 7 916

3

Python 2 - 62 54 65 bytes

def f(n):
 for x in range(n):print' '.join(map(str,range(1,x+2)))

pepp

Posted 2015-03-12T05:18:47.283

Reputation: 61

The number n should be given as input to the program, not initialized in a variable. – Zgarb – 2015-03-12T13:16:06.153

Thanks for the hint. Was not sure about that. – pepp – 2015-03-12T13:40:21.300

2

Sorry, I should have been clearer. What I meant is that you must actually define N by doing N=input() or something similar, so that your program can be run as such. Here is a Meta discussion on the topic.

– Zgarb – 2015-03-12T13:45:14.200

So this would be right, right? – pepp – 2015-03-12T21:06:23.810

Looks good now! – Zgarb – 2015-03-12T21:50:36.773

3

C (with no loops, yeah!) - 72 bytes

b(n,c){if(n){b(n-1,32);printf("%d%c",n,c);}}r(n){if(n){r(n-1);b(n,10);}}

This creates a function r(n) that can be used this way:

main(){ r(5); }

See it in action, here on tutorialspoint.com

It requires a very few tricks easily explained. I think it can be greatly improved.

A. Breust

Posted 2015-03-12T05:18:47.283

Reputation: 31

1Actually it's 75 bytes, not 74. However, you can cut it down to 72 bytes by replacing ' ' with 32 and '\n' with 10: b(n,c){if(n){b(n-1,32);printf("%d%c",n,c);}}r(n){if(n){r(n-1);b(n,10);}} – FatalSleep – 2015-03-29T09:47:58.723

1Pretty nice trick, thanks! – A. Breust – 2015-03-30T08:12:08.277

Thanks! I did my best to one up you in the C category, but I couldn't make anything shorter! So I decided to shorten yours instead. – FatalSleep – 2015-03-30T08:40:15.843

64 bytes b(n,c){n&&b(n-1,32)^printf("%d%c",n,c);}r(n){n&&r(n-1)^b(n,10);} Wandbox

– o79y – 2017-01-22T05:29:33.473

3

J, 9 characters

As a tacit, monadic verb.

[:":\1+i.
  • i. y – the numbers from 0 to y - 1.
  • 1 + i. y – the numbers from 1 to y.
  • ": y – the vector y represented as a string.
  • ":\ y – each prefix of y represented as a string.
  • ":\ 1 + i. y – each prefix of the numbers from 1 to y represented as a matrix of characters.

FUZxxl

Posted 2015-03-12T05:18:47.283

Reputation: 9 656

Now that's quite smart. +1 – seequ – 2015-03-21T20:37:17.280

This is more J-esque but doesn't it violate the rule about there being no trailing spaces on each line? – miles – 2017-01-21T09:16:52.840

@miles Indeed it does! Anything else would be very complicated. – FUZxxl – 2017-01-21T14:47:05.690

2

Python 2 - 72

>>> def p(N):print'\n'.join(' '.join(map(str,range(1,i+2)))for i in range(N))
... 
>>> p(5)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Kasramvd

Posted 2015-03-12T05:18:47.283

Reputation: 161

For answers on this site, you should write a complete program or function. And you should print the result to stdout, or return them from a function. N should be read from input or taken as a function parameter, not predefined as a variable. – jimmy23013 – 2015-03-12T15:12:48.307

@user23013 OK,fixed! – Kasramvd – 2015-03-12T15:15:17.013

The function definition needs to be included in the byte count, so I don't think this is 61. It's probably in your best interest to call the function something short, like p. On another note, you can remove two spaces - one between print and '\n' and the other between ))) and for. – Sp3000 – 2015-03-12T16:17:32.570

@Sp3000 OK,thanks for attention! fixed!;) – Kasramvd – 2015-03-12T16:30:52.947

72: def p(N):print'\n'.join(' '.join(map(str,range(1,i+2)))for i in range(N)) – seequ – 2015-03-12T16:37:03.480

You forgot the other two changes. – seequ – 2015-03-12T16:42:17.967

@Sieg Yes! nice, as im new here i don't attention to this tiny stuffs ;) – Kasramvd – 2015-03-12T16:46:16.867

2

Perl, 28

Reads the parameter from stdin.

@x=1..$_,print"@x
"for 1..<>

From the command line:

perl -E'$,=$";say 1..$_ for 1..<>'

but I don't now how to count that (probably between 25 and 29).

nutki

Posted 2015-03-12T05:18:47.283

Reputation: 3 634

1

tcl, 76

time {set p "";set j 1;while \$j<=[incr i] {set p $p\ $j;incr j};puts $p} $n

Testable on http://rextester.com/FPJXC1708

Is the first line accountable? It is not part of the algorithm, only serves for acquiring input.

sergiol

Posted 2015-03-12T05:18:47.283

Reputation: 3 055

1

Perl 6, 24 bytes

{say ~$_ for [\,] 1..$_}

Sean

Posted 2015-03-12T05:18:47.283

Reputation: 4 136

1

Jelly, 3 bytes

RRG

How it works:

RRG
R     Inclusive Range, 3 -> [1, 2, 3]
 R    Inclusive Range for all elements, [1, 2, 3] -> [[1], [1, 2], [1, 2, 3]]
  G   Attempt to format as grid

Try it online!

Jeffmagma

Posted 2015-03-12T05:18:47.283

Reputation: 145

1

J, 14 bytes

":@>@<@:>:\@i.

Usage:

   f =. ":@>@<@:>:\@i.
   f 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Explanation:

   i.4                      N.B.  i.n = list of numbers up to n
0 1 2 3

   >:@i.4                   N.B.  >:n = increment n
1 2 3 4

   >:\@i.4                  N.B.  >:\n = increment prefixes of n
1 0 0 0
1 2 0 0
1 2 3 0
1 2 3 4

   <@:>:\@i.4               N.B.   < = Box, <@:>:\ = Box the incremented prefixes of n
+-+---+-----+-------+
|1|1 2|1 2 3|1 2 3 4|
+-+---+-----+-------+

   ":@>@<@:>:\@i.4          N.B.   ":@> = String format each opened box (ensures no zeros or spaces appear at the end of each line). 
1
1 2
1 2 3
1 2 3 4

Gareth

Posted 2015-03-12T05:18:47.283

Reputation: 11 678

1

Noether, 39 bytes

Non-competing: Noether is a new language

I~A{A0>}{A(i1+~i(j1+~jP" "P£)"
"P0~ji)}

Try it here!

Explanation:

I~A   - Push User Input onto the stack and store in variable A
{A0>} - If A>0
{     - Then do
A(    - loop until top of stack equals A
i1+~i - Increment i
(     - loop until top of stack equals i
j1+~j - Increment j
P     - Print top of stack (j)
" "   - Push the string " " onto stack
P     - Print top of stack (" ")
£     - Pop top of stack
)     - End loop
"\n"  - Push the string "\n" onto stack
P     - Print top of stack
0~j   - Set variable j to zero
i     - Push i onto stack
)     - End loop

Beta Decay

Posted 2015-03-12T05:18:47.283

Reputation: 21 478

1

Python

import string
N,s=int(input()),list(string.digits)
for i in range(1,N+1):
    print(' '.join(s[1:i+1]))

bacchusbeale

Posted 2015-03-12T05:18:47.283

Reputation: 1 235

1Doesn't this fail if N >= 10? – seequ – 2015-03-12T13:52:11.767

@Sieg Yes you're right. I just learning Python, was looking for way to convert list of int to list of strings. – bacchusbeale – 2015-03-12T17:20:20.243

63 bytes: for i in range(int(input())):print(' '.join("123456789"[:i+1])) - Note that strings are treated as lists. – seequ – 2015-03-12T17:24:31.247

1

Go, 93 81 78 93 90 bytes

func r(n int)(s string){s=string(n+48);if n!=1{s=r(n-1)+" "+s};println(s);return}

Current ungolfed

func r(n int) (s string) {
    // Convert n to a string, we do not have to initialize s since
    // we hijacked the return value.
    // Numbers in the ascii table starts at 48
    s = string(n | 48)
    // Unless we are on our last iteration, we need previous iterations,
    // a space and our current iteration
    if n != 1 {
        // Collect the result of previous iteration for output
        s = r(n-1) + " " + s
    }
    println(s)
    // We can use a naked return since we specified the
    // name of our return value in the function signature
    return
}

If we need to handle N > 9 we can use the following at 78 bytes, however it requires importing the fmt package.

func r(n int)(s string){s=Sprint(n);if n!=1{s=r(n-1)+" "+s};Println(s);return}

If we include the import statement I'm now back at my initial 93 92 90 bytes

import."fmt";func r(n int)(s string){s=Sprint(n);if n>1{s=r(n-1)+" "+s};Println(s);return}

Test it online here: http://play.golang.org/p/BWLQ9R6ilw

The version with fmt is here: http://play.golang.org/p/hQEkLvpiqt

Kristoffer Sall-Storgaard

Posted 2015-03-12T05:18:47.283

Reputation: 489

I'm not sure how I feel about the string cast, but any attempts to turn it into a byte array just makes it longer – Kristoffer Sall-Storgaard – 2015-03-12T14:15:59.870

The main problem I see is that it doesn't work for n>9. You can save a byte by changing != to >, though. – Geobits – 2015-03-12T14:49:08.620

@Bigtoes, fixed now, I dont know if I'm supposed to count the import statement though – Kristoffer Sall-Storgaard – 2015-03-12T15:04:18.540

I know they're counted for the languages I'm more familiar with, so most likely yes. Sucks, I know :) – Geobits – 2015-03-12T15:05:56.197

1

Golfscript 14

,{2+,1>' '*n}/

Expects the input number to be present on the stack.

Online example: link

Cristian Lupascu

Posted 2015-03-12T05:18:47.283

Reputation: 8 369

1

Perl: 34 characters

print"@$_\n"for map[1..$_],1..$_;

This code gets the input number supplied through the special variable $_.

Felix Bytow

Posted 2015-03-12T05:18:47.283

Reputation: 311

1Most brackets are redundant here: print"@$_\n"for map[1..$_],1..$_ also works. – nutki – 2015-03-13T10:26:36.220

I adjusted the code. – Felix Bytow – 2015-03-13T11:06:19.780

1

C, 89 characters

// 90 characters
f(int n){int a=1,b;for(;n--;++a){for(b=0;b<a;++b)printf("%c%d",(!!b)*' ',b+1);puts("");}}

To eliminate confusion about puts("");. This simply prints a newline character (as seen here):

Notice that puts not only differs from fputs in that it uses stdout as destination, but it also appends a newline character at the end automatically (which fputs does not).

I got it slightly shorter with @TheBestOne's java algorithm:

// 89 characters
f(int a){char b[999]="",*p=b+1;int c=0;for(;a--&&(sprintf(b,"%s %d",b,++c)&&puts(p)););}

Felix Bytow

Posted 2015-03-12T05:18:47.283

Reputation: 311

puts(""); does nothing. You can use char b[999]="" instead of char b[999]={0} to save 1 character. – mch – 2015-03-12T16:14:39.977

2puts(""); prints a newline character. – Felix Bytow – 2015-03-12T20:56:05.603

1

Clip, 16

Jm[ijkw,1iwS},1n

Explanation

J                   .- join with newlines                           -.
 m[i        },1n    .- map numbers from 1 to numeric value of input -.
    jkw   wS        .- join with spaces                             -.
       ,1i          .- numbers from 1 to index                      -.

Ypnypn

Posted 2015-03-12T05:18:47.283

Reputation: 10 485

1

Bash+coreutils, 26 bytes

seq $1|sed "x;G;s/\n/ /;h"
  • seq simply generates the numbers 1 to n
  • sed saves the entire output for a given line in the hold space, and then appends the next line to it.

Digital Trauma

Posted 2015-03-12T05:18:47.283

Reputation: 64 644

1

ZX / Sinclair BASIC - 39 bytes

ZX Basic uses 1 byte per keyword (all the uppercase words), so helps to keep the byte size down a bit...

1 INPUT n:FOR i=1 TO n:FOR j=1 TO i:PRINT j;" ";:NEXT j:PRINT:NEXT i

Using n = 8

enter image description here

Brian

Posted 2015-03-12T05:18:47.283

Reputation: 1 209

1Nice. But ZX basic uses 6 more hidden bytes for each numeric literal (a common trick was VAL("1") (6 bytes as VAL is 1) insted of 1 (7 bytes)) – edc65 – 2015-03-13T00:29:17.380

1

R, 28

for(i in 1:scan())print(1:i)

freekvd

Posted 2015-03-12T05:18:47.283

Reputation: 909

The output is incorrect for an input value of 0. Also, it's unclear whether the leading [1] on each line is in violation of the spec. – Alex A. – 2015-03-13T01:28:25.133

@AlexA. if you look closely at the question you'll see my comment asking what behavior should be for n=0. But thanks for pointing me in the right direction! – freekvd – 2015-03-13T20:10:26.420

I saw the comment. The thing is that this doesn't print nothing for 0, it prints 1; 1 0. (Pretend ; is a line break.) – Alex A. – 2015-03-13T20:13:44.060

You might want to also consider using cat(1:i,"\n"). Even though it's slightly longer than print(1:i), it doesn't include a leading [1] on each line. – Alex A. – 2015-03-13T21:28:12.443

1

TI-Basic, 28 bytes

Input N
For(I,1,N
randIntNoRep(1,N->L1
SortA(L1
Disp L1
End

Timtech

Posted 2015-03-12T05:18:47.283

Reputation: 12 038

1This does not output as the format indicates; rather, the array is displayed, brackets and all, on the homescreen. – lirtosiast – 2015-05-20T01:03:17.503

1

Haskell, 62 57 bytes

e=enumFromTo 1
f=putStr.unlines.map(unwords.map show.e).e

Point-free style. Usage example:

Prelude> f 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

nimi

Posted 2015-03-12T05:18:47.283

Reputation: 34 639

Doing e=enumFromTo 1 saves 7 bytes. – Zgarb – 2015-03-13T08:40:21.627

@Zgarb: Thanks, but if I swap out enumFromTo 1, I have to give the main function a name, too, so it's 5 bytes. Without the name it would be a let construct: let e=enumFromTo 1 in (putStr.unlines.map(unwords.map show.e).e) 5 – nimi – 2015-03-14T01:47:12.380

1

C++, 130 bytes

To provide N, provide that many arguments on the command-line (the arguments don't matter, just the count of arguments).

#include <stdio.h>
#define printf P
int main(int c, char**){if(int x=c-1){main(x--,0);P("1");for(;x;--x){P(" %d",c-x);}P("\n");}}

Matthew Moss

Posted 2015-03-12T05:18:47.283

Reputation: 141

1

C# - 94 bytes

Written as an anonymous function that returns a string, which doesn't seem to be disallowed by the spec.

n=>String.Join("\n\n",Enumerable.Range(1,n).Select(l=>String.Join(" ",Enumerable.Range(1,l))))

Here's an ungolfed version (comments are read in BDCA order):

n =>
    String.Join("\n\n",                    //...then join it together with newlines.
        Enumerable.Range(1, n).Select(l => //For each l from 1 to n, ...
                String.Join(" ",              //...and join it with spaces, ...
                    Enumerable.Range(1, l)    //...get the range from 1 to l, ...

LegionMammal978

Posted 2015-03-12T05:18:47.283

Reputation: 15 731

1

Scala, 73 65 62 bytes

(n:Int)=>print(1 to n map(1 to _ mkString " ") mkString "\n")

Ungolfed

def printNumberTriangle(n: Int): Unit = {
  def rowString(m: Int): String = 1.to(m).mkString(" ")
  print(1.to(n).map(rowString).mkString("\n"))
}

Dave Swartz

Posted 2015-03-12T05:18:47.283

Reputation: 185

1

Mathematica, 32

Print@Row[Range@i," "]~Do~{i,#}&

alephalpha

Posted 2015-03-12T05:18:47.283

Reputation: 23 988

1How about TableForm[Range/@Range@#]&? – Martin Ender – 2015-03-21T15:13:53.117

1Shorter: Grid[Range/@Range@#]& – alephalpha – 2015-03-21T15:22:07.243

And it even looks better. :) (I keep forgetting about Grid.) – Martin Ender – 2015-03-21T15:23:25.703

But I'm not sure if there is no trailing space at the end of each line. – alephalpha – 2015-03-21T15:24:10.080

Oh good point. :( – Martin Ender – 2015-03-21T15:25:17.037

1

JavaScript, 73

n=5;for(i=1,s='';i<=n;i++)for(j=1;j<=i;j++)s+=j+(j==i?"\n":' ');alert(s);

BadHorsie

Posted 2015-03-12T05:18:47.283

Reputation: 111

1

Excel VBA, 97 bytes

n = InputBox("n")
For i = n To 1 Step -1
For j = n To 1 Step -1
Cells(i, j) = j
Next j
n = n - 1
Next i

When executed you are prompted for n and the resulting triangles are displayed on the spread sheet: output

Wightboy

Posted 2015-03-12T05:18:47.283

Reputation: 339

Optimized version to taking autoformatting into account, 74 Bytes Sub f(n) For i=n To 1 Step -1 For j=1To n Cells(i, j)=j Next j n=n-1 Next i End Sub – Taylor Scott – 2017-03-25T20:12:38.233

1

JavaScript, 92 bytes

var x=function(o){for(var r=1;o>=r;r++){for(var a="",f=1;r>=f;f++)a+=f+" ";console.log(a)}};

usage: x(5)

Cristian Gutu

Posted 2015-03-12T05:18:47.283

Reputation: 111

Welcome to PPCG! I think you accidentally put a stray \`` in there. Also, [unnamed functions are generally allowed](http://meta.codegolf.stackexchange.com/questions/1501/should-function-literals-be-allowed-when-a-function-is-asked-for/1503#1503), so you can omit thevar x. Even if not, you could shorten it tofunction x(o).... If you'd use the ES6 standard, you could shorten it even further by using [arrow notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions). You can also leave out all thevar`s ... we don't care about good style here. ;) – Martin Ender – 2015-09-30T15:01:26.387

@MartinBüttner Thank you for the good pointers :) – Cristian Gutu – 2015-09-30T21:15:32.980

1

C,88 bytes

f(n,i,j,t){i=j=1;for(;i<=n;){t=j;printf("%d%c",t,j==i?(++i&&(j=1))*'\n':(++j||1)*' ');}}

Usage:

f(10);

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

user12707

Posted 2015-03-12T05:18:47.283

Reputation: 41

0

JavaScript, 77 Bytes

function(n){for(i=1;i<=n;i++){a='';for(j=1;j<=i;j++)a+=j+' ';console.log(a)}}

TrojanByAccident

Posted 2015-03-12T05:18:47.283

Reputation: 300

0

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.

FlipTack

Posted 2015-03-12T05:18:47.283

Reputation: 13 242

0

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)

Kevin Cruijssen

Posted 2015-03-12T05:18:47.283

Reputation: 67 575

0

C++

int main()
{
   cout << "Enter N:";
   int N;
   cin >> N;

    for(int i=1; i <= N; i++)
    {
         for(int j=1; j<=i; j++)
            {
              cout << j;
            }
       cout << endl;
    }

  return 0;
}

Getachew Mulat

Posted 2015-03-12T05:18:47.283

Reputation: 9

7Btw, this is code-golf (writing a program in the least amount of characters). Therefore something like cout << "Enter N:"; is not necessary. Also you can save a lot of chars, if you remove the whitespace, remove return 0;, ... – Jakube – 2015-03-12T11:33:25.517

0

Python 2 - 60 61 53 bytes

k='1'
for i in range(2,input()+2):print k;k+=' '+`i`

With thanks to Sp3000

SuchANovice

Posted 2015-03-12T05:18:47.283

Reputation: 11

Had the indents in my code, lost them when I copied it to the post. And thanks for the suggestion! – SuchANovice – 2015-03-12T13:26:42.343

1

Also one more thing: by default, code golf implies full program or function, so you can't assume N is already defined (maybe i<=input()?). On a side note though, you can save a few bytes by replacing str(i) with <backtick>i<backtick> :)

– Sp3000 – 2015-03-12T13:40:44.803

1@Sp3000 \\`i\``` seems to work: \i``. – jimmy23013 – 2015-03-12T14:51:00.560

@user23013 \i``. Ah thanks, so it does. – Sp3000 – 2015-03-12T14:53:28.593

0

Javascript, 83

Not to happpy about this one

n=9;r='';for(i=1;i<n+2;i++){for(a=1;a<i;a++){r+=a+' '}r=r.slice(0,-1)+'\n'}alert(r)

Pretty straight forwards

r=''; so javascript doesn't add the value as numbers

r.slice(0,-1)+'\n'remove last space and add a line end

Edit: info

First I was trying to use A030512(int seq) but that failed n>10

dwana

Posted 2015-03-12T05:18:47.283

Reputation: 531

0

k, 8

1+!:'1+!

I need to add 27 characters so I'm just going to type some nonsense here.

k)!5
0 1 2 3 4

k)1+!5
1 2 3 4 5

k)!:'1+!5
,0
0 1
0 1 2
0 1 2 3
0 1 2 3 4

k)1+!:'1+!5
,1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

tmartin

Posted 2015-03-12T05:18:47.283

Reputation: 3 917

4Wouldn't a brief explanation be a lot more fun than some nonsense? – seequ – 2015-03-12T21:41:44.897

0

Ruby - 51

r=->(n){(1..n).each{|i|puts (1..i).to_a.join(" ")}}

Sample output:

r[5] # 1
     # 1 2
     # 1 2 3
     # 1 2 3 4
     # 1 2 3 4 5

r[7] # 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

Devon Parsons

Posted 2015-03-12T05:18:47.283

Reputation: 173

If you aren't pedantic enough to care, trim 3 bytes by changing puts to p - but this surrounds every line in the output with " – Devon Parsons – 2015-03-12T15:21:08.970

0

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

Alex

Posted 2015-03-12T05:18:47.283

Reputation: 369

Doesn't this generate a leading new line and a leading space? – dwana – 2015-03-13T08:46:41.247

oh leading space isn't allowed? I thought only trailing space is not allowed – Alex – 2015-03-13T14:22:46.653

0

And the loser is... Pascal! 178 148 bytes

Sidenote: this isn't golfed at all, besides newlines and spaces. I haven't coded in Pascal for more than 10 years, so I used it just for the memories.

Golfed:

program p;procedure p(n:integer);var i,j:integer;begin for i:=1 to n do begin for j:=1 to i do begin write(j); end; writeln; end end;begin p(5);end.

Ungolfed:

program p;
procedure p(n:integer);
var i,j:integer;
begin
    for i:=1 to n do begin
        for j:=1 to i do begin
            write(j);
        end;
        writeln;
    end
end;
begin
    p(5);
end.

jmm

Posted 2015-03-12T05:18:47.283

Reputation: 241

1It doesn't seem to output the spaces. Also there is no need for semicolons before end. – jimmy23013 – 2015-03-13T09:34:14.090

Right... the spaces. I didn't remember that semicolons before end were optional, two extra bytes could be golfed there – jmm – 2015-03-13T12:38:33.670

0

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

pepp

Posted 2015-03-12T05:18:47.283

Reputation: 61

1Change print s to print s[1:]. This gets rid of the first char in the string – HEGX64 – 2015-03-30T07:42:42.137

0

Golfscript - 41 chars

"#{STDIN.gets}"~.0={}{)}if,{,{)}%" "*}%n*

How it works:

"#{STDIN.gets}" -> Read a string from input

"~" -> Evaluate the input

".0=" -> Create a clone of the input and check if it's zero

"{}{)}if" -> If it's zero, do nothing, else add one to the number

",{,{)}%" "*}%" -> Create a list from 0 to N, loop through it and create new lists and do other stuff that I don't have time to explain.

"n*" -> Display it nicely

Loovjo

Posted 2015-03-12T05:18:47.283

Reputation: 7 357

0

ARM assembly 485 bytes

.global main
main:
    mov r0, #10
    mov r1, #1
    bl triangle

    mov r0, #10
    bl putchar

    mov r7, #1
    swi 0

triangle:
    cmp r1, r0
    movgt pc, lr
    push {r0,r1,lr}
    mov r0, r1
    mov r1, #1
    bl row
    mov r0, #10
    bl putchar
    pop {r0,r1,lr}
    add r1, r1, #1
    b triangle

row:
    cmp r1, r0
    movgt pc, lr
    push {r0,r1,lr}
    ldr r0, =numFormat
    bl printf
    pop {r0,r1,lr}
    add r1, r1, #1
    b row

numFormat:
    .asciz "%d "

called using the subroutine triangle, register r0 holds the limit and r1 holds the starting value.

tested on Raspberry Pi and Android

kyle k

Posted 2015-03-12T05:18:47.283

Reputation: 409

0

Java 8 - 293 chars

Golfed:

import java.util.stream.*;
import static java.util.stream.Collectors.joining;
public class T{public static void main(String[] a){int n=10;System.out.println(IntStream.range(1,n+1).boxed().map(i->IntStream.range(1,i+1).boxed().map(j->""+j).collect(joining(" "))).collect(joining("\n")));}}

Ungolfed:

    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;

    public class T {
        public static void main(String[] a){
            int n=10;

            System.out.println(
                 IntStream.range(1,n+1)
                          .boxed()
                          .map(i -> IntStream.range(1,i+1)
                                             .boxed()
                                             .map(j -> ""+j)
                                             .collect(joining(" ")))
                          .collect(joining("\n")));
        }
    }

Example output, n = 10:

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

Michael Easter

Posted 2015-03-12T05:18:47.283

Reputation: 585

0

PHP (65 bytes):

This is pretty simple to do in php:

for(;$i++<$_GET[n];$_[]=join(' ',range(1,$i)));echo@join('
',$_);

This probably may output an empty string or the '\0' char or worst if you use 0 (there is no control on it).

This is because $_ will be nullwhen you use 0 and PHP will output null (which may output any of the said before).


There is another aproach (61 bytes):

foreach(range(1,$_GET[n])as$v)echo join(' ',range(1,$v))."
";

But it can't handle the 0 in the expected way.

Ismael Miguel

Posted 2015-03-12T05:18:47.283

Reputation: 6 797

0

Dyalog APL, 11 characters

Assumes ⎕IO←1.

(⍕↑)⍤0 1⍨⍳⎕
  • ⍕⍵ represented as a string.
  • ⍺↑⍵ items taken from .
  • ⍺(⍕↑)⍵ items taken from , the result represented as a string.
  • ⍺(⍕↑)⍤0 1⍵ – The previous expression, but executed for each item in and each vector in .
  • (⍕↑)⍤0 1⍨⍵ – The previous expression with both arguments taken from .
  • (⍕↑)⍤0 1⍨⍳⍵ – The previous expression applied to the integers from 1 to .
  • (⍕↑)⍤0 1⍨⍳⎕ – The previous expression with input taken from the user.

FUZxxl

Posted 2015-03-12T05:18:47.283

Reputation: 9 656

0

K, 17 bytes

(`0:" "/$1+!)'1+!

Doing this sort of formatted output isn't very convenient in K. I used the "join" form of / from k5 to join elements of each vector with spaces. Try it with oK.

A nice thing about this solution is that it is in "tacit" form, so there's no need to create a function wrapper. Just append the argument N and suppress the result with ;:

  (`0:" "/$1+!)'1+!5;
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

JohnE

Posted 2015-03-12T05:18:47.283

Reputation: 4 632

0

Powershell v2 number of Characters : 567

   function Build-Triangle 
    {
         [CmdletBinding()]
         param(
            [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
            [int]$n
        )

        begin {}
        process 
        {
            [int]$cnt = 1
            if ( $n -gt 0 )
            {
                while ( $cnt -le $n )
                {
                    $string += $cnt.ToString() + " "
                    Write-Output -InputObject $string
                    $cnt++
                }                
            }
        }
        end {}
    }


    Build-Triangle -n 10 | Measure-Object 
    0 | Build-Triangle 
    11 | Build-Triangle  
    # just for kicks make the triangle upside down
    11 | Build-Triangle  | Sort-Object -Descending
    # find out how big the function is
    Get-Content Function:\Build-Triangle | Measure-Object -Word -Character -Line 

NattyDread2009

Posted 2015-03-12T05:18:47.283

Reputation: 1

2Welcome to PPCG! This is code golf. Please show some effort to reduce the number of bytes in your code (single-letter variable names, no comments, no unnecessary whitespace would be a start). – Martin Ender – 2015-03-18T03:26:22.403

0

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.

tia

Posted 2015-03-12T05:18:47.283

Reputation: 745

0

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)}

Sadra

Posted 2015-03-12T05:18:47.283

Reputation: 9

1I'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. – BadHorsie – 2015-03-30T16:22:14.710

0

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 >

VisualMelon

Posted 2015-03-12T05:18:47.283

Reputation: 3 810

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!

– AdmBorkBork – 2017-01-20T21:10:43.327

@AdmBorkBork I do indeed still lurk around here, thanks for the tips! – VisualMelon – 2017-01-20T22:33:57.403

0

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

PhiNotPi

Posted 2015-03-12T05:18:47.283

Reputation: 26 739

0

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

heo

Posted 2015-03-12T05:18:47.283

Reputation: 91

0

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$`}}

daniero

Posted 2015-03-12T05:18:47.283

Reputation: 17 193

0

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

Cabbie407

Posted 2015-03-12T05:18:47.283

Reputation: 1 158

0

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

Ken Gregory

Posted 2015-03-12T05:18:47.283

Reputation: 151

0

q, 18 bytes

1+til each 1+til n

Alexander Belopolsky

Posted 2015-03-12T05:18:47.283

Reputation: 191

0

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.

Tom Carpenter

Posted 2015-03-12T05:18:47.283

Reputation: 3 990

0

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

Tom Carpenter

Posted 2015-03-12T05:18:47.283

Reputation: 3 990