| Bytes | Lang | Time | Link |
|---|---|---|---|
| 003 | Uiua | 250322T153547Z | raka boy |
| 038 | Tcl | 170121T001239Z | sergiol |
| 005 | Japt mR | 250320T144157Z | Shaggy |
| 026 | Raku Perl 6 rakudo | 250320T141606Z | xrs |
| 093 | Rockstar | 230614T094226Z | Shaggy |
| 024 | Nibbles | 230614T111427Z | Dominic |
| 030 | Arturo | 230614T025730Z | chunes |
| 034 | Julia 1.0 | 230613T142208Z | Ashlin H |
| 005 | Thunno 2 N | 230611T135405Z | The Thon |
| 004 | Vyxal j | 221012T024400Z | DialFros |
| 004 | Pushy | 170121T081955Z | FlipTack |
| 039 | Noether | 170531T205201Z | Beta Dec |
| 040 | Forth gforth | 201110T133629Z | Razetime |
| 003 | 05AB1E | 180917T125721Z | Kevin Cr |
| 003 | Jelly | 170121T010600Z | Jeffmagm |
| 053 | PHP | 150312T144956Z | Benoit E |
| 130 | C++ | 150313T184340Z | Matthew |
| 116 | VBA | 150312T163012Z | Alex |
| 014 | J | 170121T094459Z | Gareth |
| 024 | Perl 6 | 170121T005056Z | Sean |
| nan | PowerShell 32bytes | 150324T192549Z | VisualMe |
| 077 | JavaScript | 170120T210643Z | TrojanBy |
| 063 | MATLAB | 151004T150511Z | Tom Carp |
| 018 | q | 151003T094047Z | Alexande |
| 027 | awk | 150930T161931Z | Cabbie40 |
| 092 | JavaScript | 150930T142311Z | Cristian |
| 088 | C | 150930T180213Z | user1270 |
| 050 | UniBasic 50 Bytes | 150930T171955Z | Ken Greg |
| 005 | APL | 150312T085027Z | Moris Zu |
| nan | 150706T181237Z | daniero | |
| 323 | Joe | 150312T054736Z | seequ |
| 060 | Python 2 | 150625T124051Z | heo |
| 097 | Excel VBA | 150625T095758Z | user3464 |
| 020 | Element | 150407T234802Z | PhiNotPi |
| 061 | Python 2 57 | 150313T084001Z | pepp |
| 073 | JavaScript | 150330T170238Z | BadHorsi |
| 072 | C with no loops | 150312T162539Z | A. Breus |
| 062 | Scala | 150315T143200Z | Dave Swa |
| 073 | JavaScript | 150324T190916Z | Sadra |
| 092 | C# | 150324T175830Z | tia |
| 032 | Mathematica | 150321T144900Z | alephalp |
| nan | ><> Fish | 150312T073057Z | Sp3000 |
| nan | Powershell v2 number of Characters 567 | 150317T231837Z | NattyDre |
| 011 | Dyalog APL | 150314T231102Z | FUZxxl |
| 017 | K | 150315T225214Z | JohnE |
| 009 | J | 150314T225422Z | FUZxxl |
| 065 | PHP | 150314T135122Z | Ismael M |
| 293 | Java 8 | 150314T015007Z | Michael |
| 057 | Haskell | 150312T234844Z | nimi |
| 026 | Bash+coreutils | 150312T154254Z | Digital |
| 485 | ARM assembly | 150313T233145Z | kyle k |
| 094 | C# | 150313T231505Z | LegionMa |
| 041 | Golfscript | 150313T184520Z | xenia |
| 034 | Perl | 150312T130456Z | Felix By |
| 028 | Perl | 150313T101914Z | nutki |
| 008 | k | 150312T135915Z | tmartin |
| 065 | Python 2 62 54 | 150312T125050Z | pepp |
| 089 | C | 150312T131433Z | Felix By |
| 148 | And the loser is... Pascal! | 150312T171206Z | jmm |
| 028 | TIBasic | 150312T165936Z | Timtech |
| 028 | R | 150312T164444Z | freekvd |
| 072 | Python 2 | 150312T110100Z | Kasravnd |
| 039 | ZX / Sinclair BASIC | 150312T160925Z | Brian |
| 053 | Python 2 | 150312T114326Z | SuchANov |
| nan | Go | 150312T113522Z | Kristoff |
| 051 | Ruby | 150312T151709Z | Devon Pa |
| 083 | Javascript | 150312T135412Z | dwana |
| 084 | Java | 150312T123239Z | TheNumbe |
| 016 | Clip | 150312T133112Z | Ypnypn |
| 012 | CJam | 150312T052513Z | Optimize |
| 014 | Golfscript | 150312T121431Z | Cristian |
| 045 | Python 3 | 150312T111556Z | Sp3000 |
| nan | 150312T112937Z | Getachew | |
| nan | 150312T110830Z | bacchusb | |
| nan | 150312T082201Z | edc65 | |
| 009 | Pyth | 150312T084613Z | Jakube |
| 027 | J | 150312T083914Z | randomra |
| 119 | Prolog | 150312T080107Z | Οurous |
Uiua, 4 chars, 3 tokens
⧅&p⇡
Tacit solution.
Explanation:
⇡ #Creates range from 0 to n-1
⧅&p #Monadic print modified by tuples modifier, which in monadic cases gets prefixes of the array.
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
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.
Thunno 2 N, 5 bytes
RRıðj
Explanation
RRıðj # Implicit input -> 4
R # Range [1..n] -> [1,2,3,4]
R # Range of each -> [[1],[1,2],[1,2,3],[1,2,3,4]]
ı # To each list:
ðj # Join by spaces -> ["1","1 2","1 2 3","1 2 3 4"]
# Join by newlines -> "1\n1 2\n1 2 3\n1 2 3 4"
# Implicit output
Vyxal j, 4 bytes
ɾɾvṄ
Explained
ɾɾvṄ
rr # range over range over input
v # vectorise
Ṅ # join list by spaces
j flag joins by newlines
Pushy, 4 bytes
:Lh_
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.
Noether, 39 bytes
I~A{A0>}{A(i1+~i(j1+~jP" "P£)"
"P0~ji)}
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
05AB1E, 3 bytes
LL»
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)
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
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.
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");}}
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
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
Perl 6, 24 bytes
{say ~$_ for [\,] 1..$_}
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 >
JavaScript, 77 Bytes
function(n){for(i=1;i<=n;i++){a='';for(j=1;j<=i;j++)a+=j+' ';console.log(a)}}
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.
q, 18 bytes
1+til each 1+til n
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
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)
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
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
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.
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$`}}
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.
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
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:

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
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
JavaScript, 73
n=5;for(i=1,s='';i<=n;i++)for(j=1;j<=i;j++)s+=j+(j==i?"\n":' ');alert(s);
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.
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"))
}
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)}
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.
Mathematica, 32
Print@Row[Range@i," "]~Do~{i,#}&
><> (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
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
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 from1to⍵.(⍕↑)⍤0 1⍨⍳⎕– The previous expression with input taken from the user.
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
J, 9 characters
As a tacit, monadic verb.
[:":\1+i.
i. y– the numbers from0toy - 1.1 + i. y– the numbers from1toy.": y– the vectoryrepresented as a string.":\ y– each prefix ofyrepresented as a string.":\ 1 + i. y– each prefix of the numbers from1toyrepresented as a matrix of characters.
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.
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
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
Bash+coreutils, 26 bytes
seq $1|sed "x;G;s/\n/ /;h"
seqsimply generates the numbers 1 to nsedsaves the entire output for a given line in the hold space, and then appends the next line to it.
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
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, ...
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
Perl: 34 characters
print"@$_\n"for map[1..$_],1..$_;
This code gets the input number supplied through the special variable $_.
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).
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
Python 2 - 62 54 65 bytes
def f(n):
for x in range(n):print' '.join(map(str,range(1,x+2)))
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)););}
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.
TI-Basic, 28 bytes
Input N
For(I,1,N
randIntNoRep(1,N->L1
SortA(L1
Disp L1
End
R, 28
for(i in 1:scan())print(1:i)
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
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

Python 2 - 60 61 53 bytes
k='1'
for i in range(2,input()+2):print k;k+=' '+`i`
With thanks to Sp3000
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
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
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
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
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 -.
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";
Golfscript 14
,{2+,1>' '*n}/
Expects the input number to be present on the stack.
Online example: link
Python 3, 48 45 bytes
f=lambda n:n and[f(n-1),print(*range(1,n+1))]
Hooray for side effects.
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;
}
Python
import string
N,s=int(input()),list(string.digits)
for i in range(1,N+1):
print(' '.join(s[1:i+1]))
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)}
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
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
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)).
