# Print this diamond

This question has been spreading like a virus in my office. There are quite a variety of approaches:

Print the following:

        1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
1234321
12321
121
1


Answers are scored in characters with fewer characters being better.

• What is the winning criterion ? And is this a challenge or a golf ? – Paul R Oct 12 '12 at 15:33
• I read "kolmogorov-complexity" as "code-golf". – DavidC Oct 12 '12 at 16:44
• @DavidCarraher "kolmogorov-complexity" was edited in after the question was asked. The original questioner has not specified the winning criteria yet. – Gareth Oct 12 '12 at 20:56
• @Gareth My comment was made after the "kolmogorov-complexity" tag was added but before the "code-golf" tag was added. At that time people were still be asking whether it was a code-golf question. – DavidC Oct 12 '12 at 22:00
• perlmonks.com/?node_id=891559 has perl solutions. – b_jonas Oct 20 '12 at 19:51

# Perl 6, 424138 35 chars

say " "x 9-$_,(1 x$_)²for 1…9…1


Try it online!

• @FrownyFrog Something did change in Perl 6 since this was written (this is a very old answer), fixed. – Konrad Borowski Jul 27 '18 at 12:42

# Python 3, 54 bytes

saved another 3 bytes by letting the int function crash instead of a loop condition. Thanks to dingledooper.

i=8
while[print(f'{int("1"*(9-abs(i)))**2:^17}')]:i-=1


Try it online!

Python 3, 57 bytes

saved 1 byte by using f-Strings thanks to dingledooper.

i=8
while i+9:print(f'{int("1"*(9-abs(i)))**2:^17}');i-=1


Try it online!

Python 3, 58 bytes

Even shorter solution by a different approach (prints one extra space at the beginning of each line).

i=8
while i+9:j=abs(i);i-=1;print(' '*j,int('1'*(9-j))**2)


Try it online!

Python 3, 61 bytes (without extra spaces)

i=8
while i+9:j=9-abs(i);i-=1;print(f'%{8+j}d'%int('1'*j)**2)


Try it online!

These are my previous solutions:

Python 3, 66 bytes

f=lambda c:f'%{8+c}d'%int('1'*c)**2
for c in f(9):print(f(int(c)))


Try it online!

Python 3, 71 bytes:

f=lambda c:str(int('1'*int(c))**2)
for c in f(9):print(f(c).center(17))


Try it online!

• Here's 57 bytes using f-strings. In addition, there's this magical 54 byte solution which works because int() throws an error on an empty string. – dingledooper Feb 26 at 0:00

## Perl 56 54 characters

Added 1 char for the -p switch.

Uses squared repunits to generate the sequence.

s//12345678987654321/;s|(.)|$/.$"x(9-$1).(1x$1)**2|eg


# Groovy 77 75

i=(-8..9);i.each{a->i.each{c=a.abs()+it.abs();print c>8?' ':9-c};println""}


old version:

(-8..9).each{a->(-8..9).each{c=a.abs()+it.abs();print c>8?' ':9-c};println""}

• added a 57 char groovy solution. You can replace both each with any to save two chars. – Matias Bjarland Feb 12 '17 at 10:52

### Scala - 86 characters

val a="543210/.-./012345";for(i<-a){for(j<-a;k=99-i-j)print(if(k<1)" "else k);println}


## Javascript, 137

With recursion:

function p(l,n,s){for(i=l;i;s+=" ",i--);for(i=1;i<=n;s+=i++);for(i-=2;i>0;s+=i--);return(s+="\n")+(l?p(l-1,n+1,"")+s:"")}alert(p(8,1,""))


First time on CG :)

### Or 118

If I can find a JS implementation that executes 111111111**2 with higher precision.
(Here: 12345678987654320).

a="1",o="\n";for(i=0;i<9;i++,o+="         ".substr(i)+a*a+"\n",a+="1");for(i=8;i;i--)o+=o.split("\n")[i]+"\n";alert(o)

• 111111111**2 yields 12345678987654320 with my browser too. But you can use one 1 less and get something useful too: '0'+11111111**2+'0' -> "01234567876543210" or use '0'+10*11111111**2 and shave of 1 more byte – Christiaan Westerbeek Jul 8 '20 at 22:09

17,{8-abs' '*19*1$,>~.*n}/  or 17,{8-abs' '*.19*+9<~.*n}/  Both work by building a suitable repunit as a string and then converting to int and squaring to get a Demlo number. PowerShell, 49 48 1..9+8..1|%{" "*(9-$_)+(1..$_+($_-1)..0|?{$_})}  • Nice solution, though it has a lot of spacing not called for in the challenge. Helped me trim mine down to half its original size. – Iszi Nov 24 '13 at 2:24 # APL, 24 chars ⍉⊃{⍕⍵↑⍨⍵>0}¨9-∘.+⍨|9-⍳17  Tested in Nars2000 and Dyalog (requires ⎕ML←3 in the latter.) Explanation  ⍳17 starting with the naturals up to 17 |9- generate the numbers from 8 to 0 and back to 8 ∘.+⍨ make a table of their sum (with 0 in the middle) 9- turn it into a diamond with 9 in the middle { }¨ for each number ⍵↑⍨⍵>0 keep it only if it's positive ⍕ then convert the result, if any, to a string ⍉⊃ disclose the nested array and adjust the dimensions  The last step transposes the result, whose shape is 17 17 1 (because of the disclose ⊃ of nested strings) into 1 17 17, which gets printed like a plain 17 17. Output ⍉⊃{⍕⍵↑⍨⍵>0}¨9-∘.+⍨|9-⍳17 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321 123456787654321 1234567654321 12345654321 123454321 1234321 12321 121 1  • Take the train to save a byte: (⍕>∘0↑⊢) – Adám Jul 11 '16 at 6:03 # Ruby 59 (-8..8).map{|i|puts' '*i.abs+"#{eval [?1*(9-i.abs)]*2*?*}"}  # Ruby, 55 puts (-8..8).map{|i|[?\s*a=i.abs,(?1*(9-a)).to_i**2]*''}  Output: irb(main):342:0> puts (-8..8).map{|i|[?\s*a=i.abs,(?1*(9-a)).to_i**2]*''} 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321 123456787654321 1234567654321 12345654321 123454321 1234321 12321 121 1  ## Groovy, 62, 57 chars ((1..9)+(8..1)).any{println' '*(9-it)+('1'*it as int)**2}  old version: ((1..9)+(8..1)).any{println"${('1'*it as int)**2}".center(17)}


explanation: we create a list [1,2,...,9,8,7,..,1]. Within the closure we create strings '1', '11', '111,..., convert them to numbers, run power of two and center.

# ///, 233 bytes

        1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
1234321
12321
121
1


Try it online!

Yay.

• This is a polyglot. It should work in PHP, and some others also. – NoOneIsHere Feb 24 '17 at 23:13
• @NoOneIsHere Yay. – sporklpony Feb 24 '17 at 23:38

iisiisddddooooooooiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddoddddsddddoooooooiiiiiiiiiiiiiiiiioiododddddddddddddddddddddddddddddddddddddddoddddsddddooooooiiiiiiiiiiiiiiiiioioiodododddddddddddddddddddddddddddddddddddddddoddddsddddoooooiiiiiiiiiiiiiiiiioioioiododododddddddddddddddddddddddddddddddddddddddoddddsddddooooiiiiiiiiiiiiiiiiioioioioiodododododddddddddddddddddddddddddddddddddddddddoddddsddddoooiiiiiiiiiiiiiiiiioioioioioiododododododddddddddddddddddddddddddddddddddddddddoddddsddddooiiiiiiiiiiiiiiiiioioioioioioiodododododododddddddddddddddddddddddddddddddddddddddoddddsddddoiiiiiiiiiiiiiiiiioioioioioioioiododododododododddddddddddddddddddddddddddddddddddddddodddsoioioioioioioioiodododododododododddddddddddddddddddddddddddddddddddddddoddddsddddoiiiiiiiiiiiiiiiiioioioioioioioiododododododododddddddddddddddddddddddddddddddddddddddoddddsddddooiiiiiiiiiiiiiiiiioioioioioioiodododododododddddddddddddddddddddddddddddddddddddddoddddsddddoooiiiiiiiiiiiiiiiiioioioioioiododododododddddddddddddddddddddddddddddddddddddddoddddsddddooooiiiiiiiiiiiiiiiiioioioioiodododododddddddddddddddddddddddddddddddddddddddoddddsddddoooooiiiiiiiiiiiiiiiiioioioiododododddddddddddddddddddddddddddddddddddddddoddddsddddooooooiiiiiiiiiiiiiiiiioioiodododddddddddddddddddddddddddddddddddddddddoddddsddddoooooooiiiiiiiiiiiiiiiiioiododddddddddddddddddddddddddddddddddddddddoddddsddddooooooooiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddo


A more human friendly spaced version:

iisiisdddd oooooooo iiiiiiiiiiiiiiiii o dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooooooo iiiiiiiiiiiiiiiii oiodo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oooooo iiiiiiiiiiiiiiiii oioiododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooooo iiiiiiiiiiiiiiiii oioioiodododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oooo iiiiiiiiiiiiiiiii oioioioiododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooo iiiiiiiiiiiiiiiii oioioioioiodododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oo iiiiiiiiiiiiiiiii oioioioioioiododododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd o iiiiiiiiiiiiiiiii oioioioioioioiodododododododo dddddddddddddddddddddddddddddddddddddddo
ddds oioioioioioioioiododododododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd o iiiiiiiiiiiiiiiii oioioioioioioiodododododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oo iiiiiiiiiiiiiiiii oioioioioioiododododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooo iiiiiiiiiiiiiiiii oioioioioiodododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oooo iiiiiiiiiiiiiiiii oioioioiododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooooo iiiiiiiiiiiiiiiii oioioiodododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oooooo iiiiiiiiiiiiiiiii oioiododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooooooo iiiiiiiiiiiiiiiii oiodo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oooooooo iiiiiiiiiiiiiiiii o dddddddddddddddddddddddddddddddddddddddo

• Any way to run this myself? – Metoniem Feb 13 '17 at 12:26
• esolangs.org/wiki/Deadfish has implementation for most of the common programming languages – Uriel Apr 3 '17 at 21:51
• Deadfish~ version. – user85052 Oct 13 '19 at 14:14

# Matlab 227223221209 208 bytes

z=@cellfun;
C=z(@(a,b,c)[a b c fliplr([a b])],[mat2cell(repelem(' ',36),1,8:-1:1),{''}],[{''},mat2cell(nonzeros(tril(repmat(49:56,8,1))')',1,1:8)],num2cell(49:57),'un',0)';
z(@(c)disp(c),[C;flipud(C(1:end-1))])


# PHP, 9991 81 byte

for($y=17;$y--;print"
")for($x=17;$x--;)echo($d=9-abs($x-8)-abs($y-8))>0?$d:" ";


run in command line

php -r "CODE_ABOVE"


18 bytes saved by Jörg Hülsermann

• you must not use open tags if you run PHP from the commndline with the -r option , replacing while with for loops and removing unnessaary spaces ends in Try it online! – Jörg Hülsermann Jul 4 '17 at 11:07
• @JörgHülsermann It works in ubuntu, but not in tio.run Do you know why? – Евгений Новиков Jul 4 '17 at 11:44
• I understand not really the question cause I have linked a working tio.run example. tio.run is not the same as the command line so you need not to use options – Jörg Hülsermann Jul 4 '17 at 11:56
• @JörgHülsermann first i seen about r option. Second about for, new line and other hacks. In fact in tio don't support two line arguments – Евгений Новиков Jul 4 '17 at 12:01
• You need no arguments or input in this case and the bytecount is 80 – Jörg Hülsermann Jul 4 '17 at 12:04

# T-SQL, 116 115 bytes

DECLARE @ INT=8,@d INT=-1a:PRINT SPACE(@)+STUFF('12345678987654321',9-@,2*@,'')IF @=0SET @d=1SET @+=@d If @<9GOTO a


Pure procedural counter and loop, not very "SQL"-like, but the best I could come up with using what SQL offers. Formatted:

DECLARE @ INT=8, @d INT=-1
a:
PRINT SPACE(@)+STUFF('12345678987654321',9-@,2*@,'')
IF @=0 SET @d=1
SET @+=@d
If @<9 GOTO a


Cuts a length out of a hard-coded string using STUFF. Would work just as easily with any other set of characters.

• You can replace 10 with 9 and we get the same result (but saving 1 byte). – Razvan Socol Apr 6 '18 at 19:58

# Canvas, 7 bytes

９｛Ｒ⇵］↶┼


Try it here!

Explanation:

9{R⇵]↶┼
9{  ]    map over i = 1..9
R        push a range 1..i
⇵       and reverse that array
in Canvas, an array of arrays is a list of lines,
and the inner arrays are joined together
↶   rotate anti-clockwise
┼  and quad-palindromize with 1 overlap


I've just fixed a couple built-ins to make a 6 byte version possible:

９｛Ｒ］／┼


Try it here!

9{R]/┼
9{ ]    map over 1..9
R       create a range 1..i
/   pad with a diagonal of spaces


# 05AB1E, 1715 10 bytes

9LJ.pû€û.c


Try it online!

# MATL, 18 bytes

9Zv-9Zvq!-t0>48*+c


Try it on MATL Online

9Zv - symmetric range from [1:9 8:-1:1]

-9Zv - reverse symmteric range, [9:-1:1 2:9]

q! - decrement that to [8:-1:0 1:8] and transpose to vertical

- - broadcast subtract - matrix of each value in the second range subtracted from each value in the first range

t0> - duplicate that and get a logical matrix of 1s where it's > 0, 0s elsewhere

48* - multiply by 48 to change 1s to 48s (ASCII '0')

+ - add that to the original matrix

c - convert to char and implicitly display

# Clojure, 160 bytes

(run! #(println(reduce str "" %))(map (fn[i](map #(let[x(- (if(> % 9)(- 9(- % 9))%)(- 9 i))](if(pos? x)x " "))(range 1 18)))(concat(range 1 10)(range 8 0 -1))))

• Welcome to this platform. Nice solution! Better than the other solution for Clojure! I suggest, you link your code with TIO as most of the other contributors have done: <a href="tio.run/##RYxBCoMwFESvMo0I/…" title="Clojure – Try It Online">Try it online!</a> – Donat Jul 12 '20 at 20:06
• Have also a look at my solution: Clojure, 127 – Donat Jul 12 '20 at 23:41

# StupidStackLanguage, 468 bytes

avdqvdmffffffffqvvviifavvfblffffffflfifdflavvfbfffffflfififdfdflavvfbffffflfifififdfdfdflavvfbfffflfififififdfdfdfdflavvfbffflfifififififdfdfdfdfdflavvfbfflfififififififdfdfdfdfdfdflavvfbflfifififififififdfdfdfdfdfdfdfavvfbfififififififififdfdfdfdfdfdfdfdflavvfbflfifififififififdfdfdfdfdfdfdflavvfbfflfififififififdfdfdfdfdfdflavvfbffflfifififififdfdfdfdfdflavvfbfffflfififififdfdfdfdflavvfbffffflfifififdfdfdflavvfbfffffflfififdfdflavvfbffffffflfifdflavvfbfffffffflf


Try it online!

## Explanation

Super simple, we just create a space and the number 1, then every line just increment and print then decrement and print

• Wait, you're telling me this is a completely manual solution?! – Razetime Oct 3 '20 at 12:04
• Yeah lol, not very efficient at all but I didn't have much time writing this answer, it can probably be compacted hugely. – Lebster Oct 4 '20 at 3:32

# Jelly,  22 18 12  11 bytes

9ŒḄr1z⁶ṚŒḄY


Try it online!

Done alongside caird coinheringaahing in chat.

## How it works

9ŒḄr1z⁶ṚŒḄY - Full program.

9ŒḄ         - The list [1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1].
r1       - Generate [N, N-1, ..., 1] for each N in ^.
z⁶     - Zip; transpose ^ with filler spaces.
Ṛ    - Reverse.
ŒḄ  - Palindromize without vectorization.
Y - Join by newlines.


Saved 6 bytes thanks to Leaky Nun!

# K, 59

-1'(-:'9+k,1_|k:!9)$,/'$b,1_||:'b:(-1_'a),'|:'a:1_1+!:'!10;


## Javascript 88

for(i=9,a=Math.abs;--i>-9;console.log(o))for(j=9,o='';j-->-9;)o+=(n=9-a(i)-a(j))>0?n:' '


## Javascript, 129* 126

for(i=1;i<18;i++){s="";a=Math.abs(9-i);for(j=0;j<a;j++)s+=" ";for(k=a+1;k<=9;k++)s+=k-a;for(l=8;l>a;l--)s+=l-a;console.log(s)}


Includes suggestion from Shmiddty in comments. Original preserved below:

for(i=1;i<18;i++){s="";a=Math.abs(9-i);for(j=0;j<a;j++){s+=" "}for(k=a+1;k<=9;k++){s+=k-a}for(l=8;l>a;l--){s+=l-a}console.log(s)}


I'm sure this could be condensed further, but darned if I know how. :P

• This gets the JS nod. It multiplies correctly. – Christian Mar 22 '13 at 4:37
• Instead of wrapping your for loops in brackets, use a semicolon. eg: for(j=0;j<a;j++)s+=" ";for(k... – Shmiddty Mar 22 '13 at 18:59
• Thank you for pointing that out, @Shmiddty. I've adjusted the snippet. – joequincy Mar 22 '13 at 21:56

## APL (40)

r←{⍵,1↓⌽⍵}
{⎕←⍵,⍨' '⍴⍨(2×10-⌈/⍵)}¨r¨r⍳¨⍳9


I guess I'm not beating marinus. :p

# C++ 223 Byte

#include <iostream>
using std::cout;using std::size_t;int main(){for(int a=0;a<2;++a)for(size_t b=1+a*7;b<10-a;((a!=1)?++b:--b)){size_t c=9-b;for(;c-->0;)cout<<" ";for(c=1;c<b;)cout<<c++;for(c=b;0<c;)cout<<c--;cout<<'\n';}}


Ungolfed:

#include <iostream>
using std::cout; //for not having to type std::cout over and over again
using std::size_t; //for not having to type std::size_t over and over again

int main()
{
for(int a = 0; a < 2; ++a)
for(size_t b=1+a*7; b<10-a; ((a!=1)?++b:--b))
{     //either count up to nine or down from nine
size_t c = 9-b; //space count we need
for(; c-- > 0;)
cout << " ";
for(c = 1; c < b;) //set c to the counter that will be print
cout << c++; //post-crement :)
for(c = b; 0 < c;) //count backwards
cout << c--; //post-decrement :)
cout << '\n'; //line is done
}
}

• Explanation please? – Lucas Henrique Apr 12 '15 at 16:38
• @LucasHenrique updated – NaCl Apr 12 '15 at 17:20

r=[-8..8]
f n|n<1=" "|1>0=show n
mapM putStrLn[[9-abs x-abs y|x<-r]>>=f|y<-r]

• This is not a complete runnable program. I think, you should add main= in the third line. Try it online. Would be 82 bytes then. – Donat Mar 2 at 22:16
• You can shorten this by 2 bytes; Try it online, 80 bytes – Donat Mar 4 at 16:38

# k, 37 bytes

r:{x,1_|x};0:c\$r@r'|8{32,-1_x}\49+!9


Try it online.