84
\$\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
  • 23
    \$\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

113 Answers 113

1
\$\begingroup\$

Recursiva, 31 22 bytes

{pB9'P+*" "- 9}J""WpB}

Try it online!

Explanation:

{pB9'P+*" "- 9}J""WpB}
{                       - For each
 p                      - palindromize [1,2,..9,..1]
  B9                    - range [1,2...9] 
    '                   - Iteration command begin
     P                  - Print
      +                 - concatenate
       *" "- 9 }        - appropriate number of spaces
                J""WpB} - obtain number string
                J""     - Join with nothing '123454321'
                   W    - map each element as string ['1','2'..'5','2','1']
                    p   - palindromize [1,2,..5,..2,1]
                     B} - range [1,2,3,4,5] 
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Classic), 21 bytes

{⊃⍵/⍕⍵}¨9-+/↑|8-⍳2⍴17

Try it online!

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

Python 2, 72 bytes

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

Try it online!

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

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

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

Stax, 10 bytes

▌┼î▲░ò╝╪.¢

Run and debug it

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

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!

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

Japt -R, 19 18 17 16 11 bytes

9õõ ®¬êÃê û

Test it

\$\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\$
1
  • \$\begingroup\$ @ceilingcat: Nice, Thank you! \$\endgroup\$ – Donat Jun 29 '20 at 7:40
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\$

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

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

Zsh, 91 61 bytes

eval s+={1..9}';<<<${(l:8:)s%?}`rev<<<$s`;'>f;{<f;tac f}|uniq

Try it online!

eval s+={1..9}';<<<${(l:8:)s%?}`rev<<<$s`;'>f;{<f;tac f}|uniq

eval    {1..9}'                          ;'                    # evaluate this 9 times
     s+=       ;                                               #  append the number to s
                <<<                                            #  print
                   ${      s  }                                #   s
                            %?                                 #    with the last character removed
                     (l:8:)                                    #    padded to 8 spaces
                               `rev<<<$s`                      #   then s, reversed
                                          >f;                  # all output to the file f
                                              <f;              # print f
                                                 tac f         # print f in reverse
                                             {        }|uniq   # remove the duplicated line in the middle
```
\$\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\$
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\$ – Martin Thoma 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\$ – Martin Thoma 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\$ – JosiahRyanW 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\$ – JosiahRyanW 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\$

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.