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

140 Answers 140

1
\$\begingroup\$

R, 62 bytes

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

Try it online!

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

Powershell, 45 bytes

1..9+8..1|%{' '*(9-$_)+-join(1..$_+$_..1|gu)}

2 solutions with 44 bytes were proposed in the comments to the Iszi's post

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

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.

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

Perl 5, 50 bytes

s/-//,$==8-$_,say$"x$_,map$=+1-abs,-$=..$=for-8..8

Try it online!

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

///, 139 bytes

/~/\/\///;/3&~|/*#~*/5432~&/2^~^/#!~%/$56~$/1234~#/1
~@/  ~!/@@/!!^@ 1&@12; $;$|@ %|@%76| %7876|%789876| %7876|@%76|@ %54;$54; $;@12;@ 1&!#

Try it online!

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

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.

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

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.

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

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

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

Clojure, 115 bytes

(defn s[n](concat(range 1 n)(range n 0 -1)))(print(apply str(mapcat #(concat(repeat(- 9 %)" ")(s %)'("\n"))(s 9))))

Try it online!

Shortened from 127 to 121 to 115.

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

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);
    }
  }
}
\$\endgroup\$
1
\$\begingroup\$

Python 3, 123 bytes

k=[]
for t in range(10):
    l=str(111111111**2)[:t]
    k.append(l.rjust(9)+l[:-1][::-1])
print("\n".join(k+k[:-1][::-1]))

Try it online!

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

Java 10 IntStream, 230 chars

interface A{static void main(String[]a){f(i->{var o=System.out;f(j->o.print(i+j>8?" ":""+(9-i-j)));o.println();});}static void f(java.util.function.IntConsumer c){java.util.stream.IntStream.range(-8,9).map(Math::abs).forEach(c);}}

Try it online!

Readable:

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

    static void f(java.util.function.IntConsumer c) {
        java.util.stream.IntStream.range(-8, 9).map(Math::abs).forEach(c);
    }
}
\$\endgroup\$
1
\$\begingroup\$

MathGolf, 11 bytes

9╒ñmÆ╒ñyFΩn

Try it online.

Explanation:

9╒           # Push a list in the range [1,9]
  ñ          # Palindromize it: [1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1]
   m         # Map over each inner integer,
    Æ        # using the following five character as inner code-block:
     ╒       #  Convert the integer to a list in the range [1,n]
      ñ      #  Palindromize it similar as before
       y     #  Join the list together to a string
         Ω   #  Prepend/append potential leading/trailing spaces (centralize)
        F    #  to make the string length 17
          n  # After the map: join the strings in the list by newlines
             # (after which the entire stack is output implicitly as result)
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 171 163 147 142 bytes

-5 bytes thanks to @ceilingcat

#define f(x,y);for(j=0;j++<(i<9?x:y);)printf(
j;D(i){for(i=0;++i<19;puts("")){f(9-i,i-9)" ")f(i,18-i)"%d",j)f(i-1,17-i)"%d",i<9?i-j:18-i-j);}}

Try it online!

This code first prints a certain amount of spaces and then the numbers.

166 bytes

i,j;D(){for(;++i<19;){for(j=0;++j<19;i<10?(i+j>9&&j-i<9?printf("%d",j<9?i+j-9:9-j+i):printf(" ")):i-j<9&&i+j<27?printf("%d",j<9?j-i+9:27-i-j):printf(" "));puts("");}}

Try it online!

This code always chooses whether to print a space or a number, depending on the values of i and j.

\$\endgroup\$
1
  • \$\begingroup\$ @ceilingcat thank you. The semicolon at the start of the macro is a nice and original idea \$\endgroup\$
    – anotherOne
    Commented Jan 18, 2021 at 15:30
1
\$\begingroup\$

Lua, 80 bytes

for i=-8,8 do x=9-math.abs(i)n=math.ceil(10^x-1)//9print((" "):rep(9-x)..n*n)end

Try it online!

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

Vyxal, C, 9 bytes

9ƛɾømĴ;ḆJ

Try it Online!

\$\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\$ printf("%*ld\n",i,1l*j*j) \$\endgroup\$ Commented Jul 22, 2023 at 23:13
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\$

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

Perl 5, 33 bytes

say$"x abs.(1x(9-abs))**2for-8..8

Try it online!

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

3 bytes saved thanks to naffetS.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 33: say$"x abs.(1x(9-abs))**2for-8..8 \$\endgroup\$
    – naffetS
    Commented Mar 15, 2023 at 2:53
1
\$\begingroup\$

Java 8 (OpenJDK 8), 157 161 bytes

-4 bytes thanks to @ceilingcat

Tried to use only 1 for instead of 2 for two-dimensional printing, but couldn't beat the best Java answers.

interface M{static void main(String[]b){for(int r,s,a=0;a<289;System.out.print((r<1?s<1?" ":"":r)+(s>7?"\n":"")))r=9-Math.abs(s=a%17-8)-Math.abs(8-a++/17);}}

Try it online!

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

JavaScript (V8), 77 78 bytes

-1 byte thanks to Donat!

Not the shortest JS solution, but not the longest either!

for(i=9;--i+9;)print(("".padEnd(a=i<0?-i:i)+"1".repeat(9-a)**2).replace(0,1))

Try it online!


i goes from 8 to -8.
We generate the correct number of spaces for each line by using the absolute value of i.

We take advantage of the fact that:

1 * 1 = 1
11 * 11 = 121
111 * 111 = 12321
1111 * 1111 = 1234321
(and so on)

So we generate the correct number of 1 by repeating it 9 minus the absolute value of i.

But unfortunately, JS can't handle properly the biggest calculation: 111111111 at power 2 gives 12345678987654320, so we also have to replace this single 0 with a 1, which costs 14 bytes :(

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can substitute " ".repeat by "".padEnd and save one byte. \$\endgroup\$
    – Donat
    Commented Jul 22, 2023 at 15:23
  • \$\begingroup\$ @Donat Thanks for your tip! I've updated my solution. \$\endgroup\$
    – Fhuvi
    Commented Jul 24, 2023 at 7:59
1
\$\begingroup\$

FMSLogo, 107 bytes

to a
show map[(word map["\ ]r 9-? reverse r ?-1 r ? "\n)]word "12345678 r 9
end
to r :
op member : "987654321
  • The power 111111111 2 trick doesn't work because FMSLogo renders this number as 1.23456789876543e+16.
  • member a b outputs the segment of b from the first occurrence of a in it to the end, and so r : outputs a word with descending digits from : to 1.
  • Apparently, the empty string is a valid variable name in Logo. (variables are always prefixed with : to get their value or " to express their name)
  • "Templates", the special anonymous functions which map takes, have ? stand for their parameter by default.
  • For each line, word concatenates together:
    • map["\ ]r 9-?, where every character in r 9-? is replaced with a space
    • reverse r ?-1,
    • r ?,
    • "\n
\$\endgroup\$
1
\$\begingroup\$

YASEPL, 77 bytes

=a$`1!d$10-a;" ",d~!e$`6~+}2,a,6!@8!e`7~-]8,7`8>" "!c@2!++`2!-}2,9,5!c$`5}1,0

explained:

=a$`1!d$10-a;" ",d~!e$`6~+}2,a,6!@8!e`7~-]8,7`8>" "!c@2!++`2!-}2,9,5!c$`5}1,0     packed
=a$                                                                               set variable a to 1
   `1                                                                    }1,0     while a > 0 || c > 0
     !d$10-a;" ",d~                                                               print (10-a) space characters
                   !e$                                                            set variable e to 1
                      `6  }2,a,6                                                  while e < a...
                        ~+                                                        print e and add one to e
                                !@8          `8                                   if a != 1...
                                   !e`7  ]8,7                                     while e != 0...
                                       ~-                                         print e and subtract e by one.
                                               >" "                               print a space
                                                   !c@2!++`2!-                    add 1 to a if c != 1
                                                              }2,9,5!c$`5         if a < 9 then set c to one
\$\endgroup\$
1
\$\begingroup\$

Vyxal 3 jR, 8 bytes

9ᵖэ“9«ṅṅ

Try it Online!

There's no way to center stuff in vyxal 3 so I used some palindrome tricks

9ᵖэ“9«ṅṅ­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌⁣​‎⁠‎⁡⁠⁤‏⁠⁠⁠‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌­
9         # ‎⁡R flag treats numbers as ranges when used as iterable
 ᵖэ       # ‎⁢Map the lambda over the prefixes of the range [1,9]
   “      # ‎⁣join by nothing
    9«    # ‎⁤prepend spaces to length 9
      ṅ   # ‎⁢⁡palindromise horizontally
       ṅ  # ‎⁢⁢palindromise vertically
💎

Created with the help of Luminespire.

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

Easyfuck, 61 54 bytes

Σ6lÙ³fÍSGCINe␏R>kfýµ¯SGCI.yu]ëwSTS«½vò␕«m§[uSHYMWÒPU2>RSHY#åyó<Zµ#ìà

due to lack of unicode representations for c1 control characters, they have been replaced by their superscripted abbreviations

Decompressed:

g(ffffffff)f(=$>5Y_[->2.<]+^[^'+^]^!--['-]5Y.J$>)+$>+gf<--$>g

+$>+gf set first cell to 1 and copy it to storage cell, then set 2nd cell to 1 and invoke functions g and f

g(ffffffff) declare function g that invokes function f 8 times

f( ) declare function f

=$>5Y_ add storage cell to 2nd cell, copy it to the storage cell, and set 3rd cell to 10 - storage

[->2.<]+ ) print an amount of spaces specified by value of 3rd cell, then set it to 1

^[^'+^]^!-- print and increment the 3rd cell until its value is equal to the storage cell, then set it to the value of storage cell - 2

['-]5Y.J$> print and decrement until 0, then print a new line, copy the first cell to the storage cell and goto the second cell

<--$>g set the first cell to -1 (255), copy it to the storage cell and run g again

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

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.