102
\$\begingroup\$

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.

\$\endgroup\$
8
  • 5
    \$\begingroup\$ What is the winning criterion ? And is this a challenge or a golf ? \$\endgroup\$
    – Paul R
    Commented Oct 12, 2012 at 15:33
  • 26
    \$\begingroup\$ I read "kolmogorov-complexity" as "code-golf". \$\endgroup\$
    – DavidC
    Commented Oct 12, 2012 at 16:44
  • 2
    \$\begingroup\$ @DavidCarraher "kolmogorov-complexity" was edited in after the question was asked. The original questioner has not specified the winning criteria yet. \$\endgroup\$
    – Gareth
    Commented Oct 12, 2012 at 20:56
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – DavidC
    Commented Oct 12, 2012 at 22:00
  • 3
    \$\begingroup\$ perlmonks.com/?node_id=891559 has perl solutions. \$\endgroup\$
    – b_jonas
    Commented Oct 20, 2012 at 19:51

141 Answers 141

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

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("");
        }
    }
}
\$\endgroup\$
8
  • 1
    \$\begingroup\$ 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;. \$\endgroup\$
    – arshajii
    Commented Oct 24, 2012 at 0:11
  • \$\begingroup\$ @A.R.S.: Thanks. Now it has 9 characters less (and is still by far the longest solution) \$\endgroup\$ Commented Oct 24, 2012 at 5:59
  • 1
    \$\begingroup\$ You can spare 2 more characters by moving variable c's declaration into the for: for(int b=-8,c;b<9;){c=Math.…. \$\endgroup\$
    – manatwork
    Commented Oct 24, 2012 at 6:41
  • 1
    \$\begingroup\$ @manatwork: Thanks. I could remove 5 characters with this :-) And I've just seen that a VBA-solution is longer \$\endgroup\$ Commented Oct 24, 2012 at 7:34
  • 1
    \$\begingroup\$ You can remove all the newlines in your code \$\endgroup\$
    – Poke
    Commented Dec 5, 2016 at 15:48
0
\$\begingroup\$

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.

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

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.

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

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.

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

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

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

Python 2, 59 bytes

i=8;exec'print" "*abs(i)+`int("1"*(9-abs(i)))**2`;i-=1;'*17

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Alternate 59-byte solution: i=8;exec'print`int("1"*(9-abs(i)))**2`.center(18);i-=1;'*17 \$\endgroup\$ Commented Sep 20, 2018 at 18:10
  • \$\begingroup\$ Also, 58-bytes: i=8;exec'j=abs(i);print" "*j+`int("1"*(9-j))**2`;i-=1;'*17 \$\endgroup\$ Commented Sep 20, 2018 at 18:13
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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!

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

///, 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!

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

Canvas, 14 bytes

9{:9∔ ×;R]∑9n┼

Try it here!

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

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.

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

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.

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

C - 118 characters

x,c,i=1,j;main(){for(;i<20;){x=10;for(j=1;j<20;){j++<10?x--:x++;printf(x>c?" ":"%d",x);}i++<10?c++:c--;printf("\n");}}

My very first code golf! I still need to get into the golfing mindset, I know there is probably a lot I can do differently.

Edit: Try it online!

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

JavaScript, 84 bytes

Saved many bytes thanks to ETHProductions

[...s="12345678987654321"].map(x=>"".padEnd(9-x)+s.slice(0,x-1)+s.slice(-x)).join`
`

Try it online!

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

Tcl, 99 bytes

time {puts [string repe { } [expr abs([incr i]-9)]][expr [string repe 1 [expr 9-abs($i-9)]]**2]} 17

Try it online!

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

C# (Visual C# Interactive Compiler), 103 bytes

for(int i=1,j=1;i>0;i+=j=i>8?-1:j)Write($"{"123456789".Substring(0,i),9}"+"87654321\n".Substring(9-i));

Try it online!

Definitely not the shortest answer, and this interpreter was published after the question was posted. But I did not see a C# answer. When using the regular C# compiler, the print statement is much longer: System.Console.Write. Also, string interpolation was not a thing when this question was posted.

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

Javascript, 103 96 93 89 88 87

f=(i=[...Array(17)].map((x,i)=>i<8?i:i-2*(i-8)))=>i.map(x=>i.map(y=>x+y>7?(x+y-7):' ').join``).join`\n`

I haven't looked at the other answers up until this point, and just started out with this spreadsheet. Just saw that this answer isn't too bad of a golf compared to the other Javascript answers.

I tried to be clever coming up with something that generates a particular array (i) that I use for the x and y-axis. But it turns out, spelling out that array is shorter.

f=(i=[0,1,2,3,4,5,6,7,8,7,6,5,4,3,2,1,0])=>i.map(x=>i.map(y=>x+y>7?(x+y-7):' ').join``).join`\n`

Update. Spelling out the array differently, but I'm sure that I can golf this further

f=(i=[...'01234567876543210'])=>i.map(x=>i.map(y=>1*x+1*y>7?(1*x+1*y-7):' ').join``).join`\n`

Last update for tonight:

f=(i=[...'01234567876543210'])=>i.map(x=>i.map(y=>+x+1*y>7?+x+1*y-7:' ').join``).join`\n`

Getting rid of one more byte:

f=(i=[...'0'+10*11111111**2])=>i.map(x=>i.map(y=>+x+1*y>7?+x+1*y-7:' ').join``).join`\n`

And one more:

f=(i=[0,...11111111**2+'0'])=>i.map(x=>i.map(y=>+x+1*y>7?+x+1*y-7:' ').join``).join`\n`
\$\endgroup\$
0
\$\begingroup\$

Duocentehexaquinquagesimal, 58 bytes

(Úv×H.F°ǝ=ÉÈNŸ=~ʒ€
₁˨Dûl`N₆rÇX³ΣJd‘×∞*Åv€rØ(0՜֤ΔNā—St€ô

Try it online!

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

FreeBASIC, 100 characters

Dim As Byte i,j,k
for i=-8 to 8 : for j=-8 to 9
k=abs(i)+abs(j)
if k > 8 then print " "; : else : ? chr(57-k);
next
? 
next 
Sleep
End
\$\endgroup\$
1
0
\$\begingroup\$

Uiua, 33 bytes

⊂:↘1⇌.≡(⊂⇌↘1.)⬚@ ≡°□+@1≡(□⇌⇡)+1⇡9

Try it

I’ll come back to this later and see if I can reduce it, it’s likely that I can shave off like 10 bytes

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

Zsh, 57 bytes

for c ({1..9} {8..1}){printf %$[8+c]s\\n $[(10**c/9)**2]}

Try it online!

\$\endgroup\$
1 2 3 4
5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.