89
\$\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
  • 4
    \$\begingroup\$ What is the winning criterion ? And is this a challenge or a golf ? \$\endgroup\$
    – Paul R
    Oct 12 '12 at 15:33
  • 24
    \$\begingroup\$ I read "kolmogorov-complexity" as "code-golf". \$\endgroup\$
    – DavidC
    Oct 12 '12 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
    Oct 12 '12 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
    Oct 12 '12 at 22:00
  • 3
    \$\begingroup\$ perlmonks.com/?node_id=891559 has perl solutions. \$\endgroup\$
    – b_jonas
    Oct 20 '12 at 19:51

121 Answers 121

1
\$\begingroup\$

Vyxal, C, 9 bytes

9ƛɾømĴ;ḆJ

Try it Online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 73 bytes

for(z of(s=z=>"".padEnd(9-z)+(BigInt(10**z)/9n)**2n)(9))console.log(s(z))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Templates Considered Harmful, 267 255 bytes

Ap<Fun<If<A<1>,Cat<Ap<Fun<Cat<Ap<Fun<If<A<1>,Cat<Ap<A<0>,Sub<A<1>,T>>,SP>,LF>>,Sub<I<9>,A<1>>>,Ap<Fun<If<lt<A<1>,A<2>>,Cat<Cat<A<1>,Ap<A<0>,Add<A<1>,T>,A<2>>>,A<1>>,A<1>>>,T,A<1>>>>,If<lt<A<1>,I<9>>,A<1>,Sub<I<18>,A<1>>>>,Ap<A<0>,Sub<A<1>,T>>>,LF>>,I<17>>

Try it online!

Has leading and trailing whitespace -- sorry! tch has no concept of "empty string" (at least not that I've found)

I doubt the ungolfed version would answer any questions (probably the opposite). I like to think of TCH as a lisp-like language with no functions, loops, or variables, so all the logic is done through recursive lambdas.

The basic structure:

lambda:  (run lower lambdas with int 1,2,3...,17,18, join output)
   lambda:  (convert input, run lower lambdas, join output)
     lambda:  (int -> string of spaces & newline)
     lambda:  (int -> string of numbers)
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 107 bytes

b=Math.abs,e="";for(i=0;i<17;i++){for(j=0;j<17;j++){z=9-(b(i-8)+b(j-8));e+=z>0?z:" "}e+="\n"}console.log(e)

Try it online!

\$\endgroup\$
1
1
\$\begingroup\$

C (gcc), 82 78 bytes

#define W printf("%*lld\n",i,1ll*j*j)
g(i,j){i<17&&g(W,j*10+1);W;}D(){g(9,1);}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Haskell, (complete program) 79 bytes

main=mapM putStrLn[(' '<$[n..8])++(s n>>=show)|n<-s 9]
s n=[1..n-1]++[n,n-1..1]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Vyxal C, 17 bytes

»÷rṡF»S¦øṁ½ƛ÷⋎;øm

Explanation

»÷rṡF»S                - 123456789 as string
       ¦               - Take all the prefixes
        øṁ             - Vertical mirror
          ½ƛ÷⋎;        - Halve and split, then join while removing the duplicate ends.
               øm      - Horizontal palindromize.
<C flag>               - Center and join list by newlines
<implicit output>      - Print

Try it Online!

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

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

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

\$\endgroup\$
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\$
7
  • 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
    Oct 24 '12 at 0:11
  • \$\begingroup\$ @A.R.S.: Thanks. Now it has 9 characters less (and is still by far the longest solution) \$\endgroup\$ Oct 24 '12 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
    Oct 24 '12 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\$ Oct 24 '12 at 7:34
  • 1
    \$\begingroup\$ You can remove all the newlines in your code \$\endgroup\$
    – Poke
    Dec 5 '16 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\$ Sep 20 '18 at 18:10
  • \$\begingroup\$ Also, 58-bytes: i=8;exec'j=abs(i);print" "*j+`int("1"*(9-j))**2`;i-=1;'*17 \$\endgroup\$ Sep 20 '18 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\$

Perl 5, 36 bytes

s/-//,say$"x$_.(1x(9-$_))**2for-8..8

Try it online!

Using 11 * 11 = 121, 111 * 111 = 12321, 1111 * 1111 = 1234321, ...

\$\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\$

Excel, 80 bytes

=CONCAT(LET(x,ABS(9-ROW(1:17)),y,9-ABS(9-COLUMN(A:R)),IFS(y>x,y-x,y," ",1,"
")))

Link to Spreadsheet

\$\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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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