# 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

# Python 2, 72 bytes

z="123456789"
for i in range(9)+range(8)[::-1]:print"% 8s"%z[:i]+z[i::-1]


Try it online!

• Welcome to the site! I've edited in a link to an interpreter, so that others can test your solution – caird coinheringaahing Apr 5 '18 at 16:41

# ///, 124 bytes

/(/# '87#//'/"67//&/123//%/43!//$/ //#/654321 //"/&45//!/21$ /1
$$1! &!&% "%"#'('8987('65%"65% "%&% &!$$1! 1


Try it online!

Shorter than this answer by 109 bytes! Just applies a bunch of substitutions with whitespace and common numbers.

# Stax, 10 bytes

▌┼î▲░ò╝╪.¢


Run and debug it

# R, 62 bytes

for(i in c(1:9,8:1))cat(rep(" ",9-i),1:i,(i:1)[-1],"
",sep="")


Try it online!

# Japt-R, 19181716 11 bytes

9õõ ®¬êÃê û


Test it

# Java, 155 chars

interface A{static void main(String[]a){var o=System.out;for(int i=-9,j,k;++i<9;o.println())for(j=-9;++j<9;)o.print((k=(i<0?-i:i)+(j<0?-j:j))>8?" ":9-k);}}


-7 chars thanks to a kind commenter.

# Perl 5, 50 bytes

s/-//,$==8-$_,say$"x$_,map$=+1-abs,-$=..$=for-8..8  Try it online! # ///, 139 bytes /~/\/\///;/3&~|/*#~*/5432~&/2^~^/#!~%/$56~$/1234~#/1 ~@/ ~!/@@/!!^@ 1&@12;$;$|@ %|@%76| %7876|%789876| %7876|@%76|@ %54;$54; $;@12;@ 1&!#  Try it online! ## REXX, 86 chars x=' 12345678987654321 ' do n=1 to 17 m=9-abs(9-n) say substr(x,m,10)right(x,m) end  Complete program. No real tricks used except some smooshing and keeping to a single loop. I'm assuming EOL doesn't count. ## Java, 156 chars interface A{static void main(String[]v){for(int x,a=-9;++a<9;System.out.printf("%9s%s%n","123456789".substring(0,9-x),"87654321".substring(x)))x=a>0?a:-a;}}  Formatted version of this solution: interface A { static void main(String[] v) { for (int x, a = -9; ++a < 9; System.out.printf("%9s%s%n", "123456789".substring(0, 9 - x), "87654321".substring(x))) x = a > 0 ? a : -a; } }  Java code is generally longer. This is my shortest solution so far. It does not produce additional whitespace in the right side as another solution does, but it is a little bit longer. • @ceilingcat: Nice, Thank you! – Donat Jun 29 at 7:40 # T-SQL, 104 bytes Inspired by @BradC's answer DECLARE @ INT=8a:PRINT space(abs(@))+stuff('12345678987654321',9-abs(@),abs(@)*2,'')SET @-=1IF~@<8GOTO a  Try it online # Java (JDK), 132 bytes interface A{static void main(String[]v){for(long m=0,a=0;++a<18;System.out.printf("%"+(a>9?26-a:8+a)+"d%n",m*m))m=a>9?m/10:m*10+1;}}  Try it online! This is a different way to do it than my first answer for Java. It is using integer arithmetics. It is even shorter, maybe the shortest possible. Formatted version of this code: interface A { static void main(String[] v) { for (long m = 0, a = 0; ++a < 18; System.out.printf("%" + (a > 9 ? 26 - a : 8 + a) + "d%n", m * m)) m = a > 9 ? m / 10 : m * 10 + 1; } }  Or more readable: class A { public static void main(String[] v) { for (long m = 0, a = 0; ++a < 18; ) { long width = a > 9 ? 26 - a : 8 + a; m = a > 9 ? m / 10 : m * 10 + 1; System.out.printf("%" + width + "d%n", m * m); } } }  # JavaScript (Node.js), 75 bytes s=z=>"".padEnd(9-z)+BigInt((10**z-1)/9)**2n for(z of s(9))console.log(s(z))  Try it online! Shortest solution for JavaScript until now. # 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 ## C, 98 chars l=9,n;p(i){for(i=18;i;putchar(i?n>l?48+n-l:32:10))n=9<--i?18-i:i;} main(){p(l--);l&&p(main());l++;}  ## VBA, 197 for i=1 to 9:?string(27-i*3,32)l;:for j=1 to i:?j;:next:for j=i-1 to 1 step -1:?j;:next:?:next:for i=8 to 1 step -1:?string(27-i*3,32)l;:for j=1 to i:?j;:next:for j=i-1 to 1 step -1:?j;:next:?:next  if vba didn't automatically add a space for the + sign it doesn't print, perhaps I could avoid the 27-i*3 construct for making it look right ## Java, 177 chars public class A{public static void main(String[]v){ for(int a=-8,b,c;a<9;a++){ for(b=-8;b<9;){c=Math.abs(a)+Math.abs(b++); System.out.print(c>8?" ":9-c);}System.out.println();}}}  correct formatting of this solution (352 chars): public class A { public void main(String[] args) { for (int a = -8; a < 9; a++) { for (int b = -8; b < 9;) { int c = Math.abs(a) + Math.abs(b++); System.out.print(c > 8 ? " " : 9 - c); } System.out.println(""); } } }  • You can trim off a few characters here and there. For example, String[] args can be String[]v, and System.out.println("") can be System.out.println(). Similarly, int b= -8; b < 9; can be int b=-8;b<9;. – arshajii Oct 24 '12 at 0:11 • @A.R.S.: Thanks. Now it has 9 characters less (and is still by far the longest solution) – Martin Thoma Oct 24 '12 at 5:59 • You can spare 2 more characters by moving variable c's declaration into the for: for(int b=-8,c;b<9;){c=Math.…. – manatwork Oct 24 '12 at 6:41 • @manatwork: Thanks. I could remove 5 characters with this :-) And I've just seen that a VBA-solution is longer – Martin Thoma Oct 24 '12 at 7:34 • You can remove all the newlines in your code – Poke Dec 5 '16 at 15:48 # Excel, 24 chars (cheating?) 1. In cell B2 put =MAX(A2,B1,B3,C2)-1 (19 chars) 2. Fill down to B18 and right to R18. 3. Select B2 through B18 and Format:Cells:Number:Custom: 0; (2 chars) 4. Enter a 8.9 in J10 (3 char) 5. Ignore all complaints about circular references throughout the process. Use Preferences:Calculation to allow iteration if it is not already allowed. 6. Select columns B through R and resize their width to make square boxes. 7. Profit? Note: Some might call step 2 cheating as it creates 19 to 23 characters in every cell for a total of over 6000 chars. If you really want to count it that way, you would be better off not putting the formula into those squares that are to remain blank. In that case, you can use a 9 in J10 and you don't need the custom formatting. The total character count would then be just over 3000. # Haskell, 77 bytes r=[-8..8] f n|n<1=" "|1>0=show n mapM putStrLn[[9-abs x-abs y|x<-r]>>=f|y<-r]  # SmileBASIC, 65 bytes FOR I=-8TO 8A=ABS(I)Q=VAL("1"*(9-A))?" "*A;Q*Q NEXT LOCATE 16,8?1  I used the fact that 11*11=121, 111*111=12321, etc. Unfortunately, 12345678987654321 can't be stored as a 64 bit float, so I had to add the last 1 separately, adding 14 bytes. # Common Lisp SBCL, 145 bytes (do((b 1)(l'(1 2 3 4 5 6 7 8 9))(i 1(+ i b)))((= i 0))(format t"~17:@<~v{~a~}~v{~a~}~>~%"i l i(reverse(subseq l 0(1- i))))(if(> i 8)(setf b -1)))  It is worse than the other Common Lisp solution, but it works and I think it is different enough to be posted. ### Explanation i is incremented up to 9 and then decremented to 0 (when i=9 sign of b is changed effectively changing addition to subtraction - this avoids second loop. In each line I print numbers: 123...i(i-1)...1 using loops of format function (for first loop I use list '(1 2 3 4 5 6 7 8 9) and for decrementing loop I use reversed subsequence of this list. Text is then centered. I didn't notice that numbers are squares of 11..11. As far as this solution is concerned the diamond could be made out of letters or (! @ # ...) (for that you would need to change ~a to ~c in format function. # tcl, 122 110 proc P i {puts [format %[expr 8+$i]s [expr [string repe 1 $i]**2]]} time {P [incr i]} 9 time {P [incr i -1]} 8  demo # Python 2, 59 bytes i=8;exec'print" "*abs(i)+int("1"*(9-abs(i)))**2;i-=1;'*17  Try it online! • Alternate 59-byte solution: i=8;exec'printint("1"*(9-abs(i)))**2.center(18);i-=1;'*17 – JosiahRyanW Sep 20 '18 at 18:10 • Also, 58-bytes: i=8;exec'j=abs(i);print" "*j+int("1"*(9-j))**2;i-=1;'*17 – JosiahRyanW Sep 20 '18 at 18:13 # Excel VBA, 81 76 Bytes Anonymous VBE immediate window function that takes no input and outputs to the VBE Immediate Window For i=-8To 8:j=Abs(i):r=Mid(987654321,j+1):?Spc(j)StrReverse(r)Mid(r,2):Next  ## Old Version, 81 Bytes For i=-9To 8:For j=-8To 9:k=Abs(i)+Abs(j):l=l &IIf(k>8," ",9-k):Next:?l:l="":Next  # Yabasic, 89 bytes Anonymous function that takes no input and outputs to STDOUT. For i=-8To 8 For j=-8To 9 k=Abs(i)+Abs(j) If k>8Then?" "; Else?Chr$(57-k);
Fi
Next
?
Next


Try it online!

# ///, 132 bytes

/T/123//F/T45//f/4321//S/  //s/S //N/
s//a/F678//b/765f//A/1Ns 121Ns/ssSAT21NSTfN FfNF65f
SF6b
ab
a98b
ab
SF6bNF65fN FfNSTfNsT2AS1


Try it online!

## Canvas, 14 bytes

９｛：９∔ ×；Ｒ］∑９ｎ┼


Try it here!

# Attache, 49 bytes

Print@Join=>Table[{If[_%9=_,9-_,sp]}@+,Abs!-8:8]


Try it online!

Simply tables (that is, applies a function like a multiplication table) the function which adds two numbers (from [8, 7, 6, 5, ..., 5, 6, 7, 8]), checks if they are less than 9, then yields 9 - that number if so, otherwise a space. Then, prints each row accordingly.

# Kotlin, 125 bytes

fun d(){for(i in 0..307){val v=Math.abs(i%18-9)+Math.abs(i/18-8)
print(if(i%18!=0)if(v>8)' '
else(57-v).toChar()
else '\n')}}


Try it online!

# Japt-R, 11 bytes

9õ_õ ¬êÃê û


Try it online!

### Unpacked & How it works

9õZ{Zõ q ê} ê û

9õZ{  [1..9].map(Z=>...)
Zõ      [1..Z]
q       Join with nothing; ["1", "12", ..., "123...9"]
ê       Make palindrome;   ["1", "121", ..., "123...9...321"]
}
ê     Make palindrome on the array
û     Center-pad each element to the longest one
-R joins with newline
implicit output


I like the õ_õ emoji.