77
\$\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\$
  • 4
    \$\begingroup\$ What is the winning criterion ? And is this a challenge or a golf ? \$\endgroup\$ – Paul R Oct 12 '12 at 15:33
  • 21
    \$\begingroup\$ I read "kolmogorov-complexity" as "code-golf". \$\endgroup\$ – DavidC Oct 12 '12 at 16:44
  • 1
    \$\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
  • \$\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

88 Answers 88

24
\$\begingroup\$

J, 29 26 24 23 22 21 chars

,.(0&<#":)"+9-+/~|i:8

Thanks to FUZxxl for the "+ trick (I don't think I've ever used u"v before, heh).

Explanation

                  i:8  "steps" vector: _8 _7 _6 ... _1 0 1 ... 7 8
                 |     magnitude
              +/~      outer product using +
            9-         inverts the diamond so that 9 is in the center
  (      )"+           for each digit:
      #                  copy
   0&<                   if positive then 1 else 0
       ":                copies of the string representation of the digit
                         (in other words: filter out the strictly positive
                          digits, implicitly padding with spaces)
,.                     ravel each item of the result of the above
                       (necessary because the result after `#` turns each
                        scalar digit into a vector string)
\$\endgroup\$
  • \$\begingroup\$ Instead of "0], write "+. \$\endgroup\$ – FUZxxl Apr 11 '15 at 10:26
  • \$\begingroup\$ For one less character, write ,.0(<#":)"+9-+/~|i:8 \$\endgroup\$ – FUZxxl Apr 11 '15 at 13:24
  • 1
    \$\begingroup\$ Here is your solution translated to 25 characters of APL: ⍪↑{(0<⍵)/⍕⍵}¨9-∘.+⍨|9-⍳17 \$\endgroup\$ – FUZxxl Apr 11 '15 at 13:44
25
\$\begingroup\$

APL (33 31)

A⍪1↓⊖A←A,0 1↓⌽A←⌽↑⌽¨⍴∘(1↓⎕D)¨⍳9

If spaces separating the numbers are allowed (as in the Mathematica entry), it can be shortened to 28 26:

A⍪1↓⊖A←A,0 1↓⌽A←⌽↑⌽∘⍕∘⍳¨⍳9

Explanation:

  • (Long program:)
  • ⍳9: a list of the numbers 1 to 9
  • 1↓⎕D: ⎕D is the string '0123456789', 1↓ removes the first element
  • ⍴∘(1↓⎕D)¨⍳9: for each element N of ⍳9, take the first N elements from 1↓⎕D. This gives a list: ["1", "12", "123", ... "123456789"] as strings
  • ⌽¨: reverse each element of this list. ["1", "21", "321"...]

  • (Short program:)

  • ⍳¨⍳9: the list of 1 to N, for N [1..9]. This gives a list [[1], [1,2], [1,2,3] ... [1,2,3,4,5,6,7,8,9]] as numbers.
  • ⌽∘⍕∘: the reverse of string representation of each of these lists. ["1", "2 1"...]
  • (The same from now on:)
  • A←⌽↑: makes a matrix from the list of lists, padding on the right with spaces, and then reverse that. This gives the upper quadrant of the diamond. It is stored in A.
  • A←A,0 1↑⌽A: A, with the reverse of A minus its first column attached to the right. This gives the upper half of the rectangle. This is then stored in A again.
  • A⍪1↓⊖A: ⊖A is A mirrored vertically (giving the lower half), 1↓ removes the top row of the lower half and A⍪ is the upper half on top of 1↓⊖A.
\$\endgroup\$
  • 5
    \$\begingroup\$ +1 Amazing. Could you translate it for us APL illiterates? \$\endgroup\$ – DavidC Oct 12 '12 at 19:36
  • 3
    \$\begingroup\$ Shouldn't non-ascii code be counted in UTF-8 instead of code-points? This would push APL closer to his earthly relatives. \$\endgroup\$ – John Dvorak Mar 23 '13 at 5:39
  • 5
    \$\begingroup\$ @JanDvorak No, since there is an APL code page, which fits the entire character set into a single byte. But I think you've probably figured this out at some point since 2013. ;) \$\endgroup\$ – Martin Ender Jan 25 '15 at 0:34
23
\$\begingroup\$

Clojure, 191 179 bytes

#(loop[[r & s](range 18)h 1](print(apply str(repeat(if(< r 8)(- 8 r)(- r 8))\ )))(doseq[m(concat(range 1 h)(range h 0 -1))](print m))(println)(if s(recur s((if(< r 8)inc dec)h))))

-12 bytes by changing the outer doseq to a loop, which allowed me to get rid of the atom (yay).

A double "for-loop". The outer loop (loop) goes over each row, while the inner loop (doseq) goes over each number in the row, which is in the range (concat (range 1 n) (range n 0 -1)), where n is the highest number in the row.

(defn diamond []
  (let [spaces #(apply str (repeat % " "))] ; Shortcut function that produces % many spaces
    (loop [[row-n & r-rows] (range 18) ; Deconstruct the row number from the range
           high-n 1] ; Keep track of the highest number that should appear in the row
      (let [top? (< row-n 8) ; Are we on the top of the diamond?
            f (if top? inc dec) ; Decided if we should increment or decrement
            n-spaces (if top? (- 8 row-n) (- row-n 8))] ; Calculate how many prefix-spaces to print
        (print (spaces n-spaces)) ; Print prefix-spaces
        (doseq [m (concat (range 1 high-n) (range high-n 0 -1))] ; Loop over the row of numbers
          (print m)) ; Print the number
        (println)

        (if r-rows
          (recur r-rows (f high-n)))))))

Due to a bug in the logic in my first attempt (accidentally inserting the prefix-spaces between each number instead), I managed to get this:

1
1       2       1
1      2      3      2      1
1     2     3     4     3     2     1
1    2    3    4    5    4    3    2    1
1   2   3   4   5   6   5   4   3   2   1
1  2  3  4  5  6  7  6  5  4  3  2  1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
12345678987654321
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
1  2  3  4  5  6  7  8  9  8  7  6  5  4  3  2  1
1   2   3   4   5   6   7   8   7   6   5   4   3   2   1
1    2    3    4    5    6    7    6    5    4    3    2    1
1     2     3     4     5     6     5     4     3     2     1
1      2      3      4      5      4      3      2      1
1       2       3       4       3       2       1
1        2        3        2        1
1         2         1

Not even correct ignoring the obvious bug, but it looked cool.

\$\endgroup\$
20
\$\begingroup\$

Mathematica 83 49 43 54 51

Print@@#&/@(Sum[k~DiamondMatrix~17,{k,0,8}]/.0->" ")

formatting improved


With 3 bytes saved thanks to Kelly Lowder.

Analysis

The principal part of the code, Sum[DiamondMatrix[k, 17], {k, 0, 8}], can be checked on WolframAlpha.

The following shows the underlying logic of the approach, on a smaller scale.

a = 0~DiamondMatrix~5;
b = 1~DiamondMatrix~5;
c = 2~DiamondMatrix~5;
d = a + b + c;
e = d /. 0 -> "";
Grid /@ {a, b, c, d, e}

grids

\$\endgroup\$
  • 1
    \$\begingroup\$ David, you beat me this time! :-) \$\endgroup\$ – Mr.Wizard Oct 21 '12 at 15:29
  • 1
    \$\begingroup\$ Another try (55 chars): f = Table[# - Abs@k, {k, -8, 8}] &; f[f[9]] /. n_ /; n < 1 -> "" // Grid \$\endgroup\$ – DavidC Mar 22 '13 at 17:56
  • \$\begingroup\$ Still another (71 chars): Table[9 - ManhattanDistance[{9, 10}, {j, k}], {j, 18}, {k, 18}] /. n_ /; n < 1 -> "" // Grid \$\endgroup\$ – DavidC Mar 22 '13 at 17:57
  • 2
    \$\begingroup\$ Grid@#@#@9&[Table[#-Abs@k,{k,-8,8}]&]/.n_/;n<1->"" 50 chars. \$\endgroup\$ – chyanog Mar 23 '13 at 4:26
  • \$\begingroup\$ A visual display of the code: ArrayPlot[Sum[k~DiamondMatrix~17, {k, 0, 8}], AspectRatio -> 2] \$\endgroup\$ – DavidC Nov 23 '13 at 15:53
15
\$\begingroup\$

Python 2, 72 69 67 61

Not clever:

s=str(111111111**2)
for i in map(int,s):print'%8s'%s[:i-1]+s[-i:]
\$\endgroup\$
  • 1
    \$\begingroup\$ doesn't work in Python 3+, which requires parens around the arguments to print :( \$\endgroup\$ – Griffin Oct 12 '12 at 18:27
  • 7
    \$\begingroup\$ @Griffin: In code golf I choose Python 2 or Python 3 depending on whether I need print to be a function. \$\endgroup\$ – Steven Rumbalski Oct 12 '12 at 19:29
  • 3
    \$\begingroup\$ s=`0x2bdc546291f4b1` \$\endgroup\$ – gnibbler Oct 14 '12 at 1:32
  • 1
    \$\begingroup\$ @gnibbler. Very clever suggestion. Unfortunately, the repr of that hexadecimal includes a trailing 'L'. \$\endgroup\$ – Steven Rumbalski Oct 15 '12 at 14:51
  • 1
    \$\begingroup\$ @gnibbler: This works in Python running on 64-bit platforms, but not on 32-bit platforms. \$\endgroup\$ – Konrad Borowski Dec 31 '13 at 8:06
14
\$\begingroup\$

C, 79 chars

v;main(i){for(;i<307;putchar(i++%18?v>8?32:57-v:10))v=abs(i%18-9)+abs(i/18-8);}
\$\endgroup\$
  • 4
    \$\begingroup\$ Explanation please? \$\endgroup\$ – Lucas Henrique Apr 12 '15 at 16:35
  • 1
    \$\begingroup\$ @LucasHenrique 307 characters total. i%18-9 is x value on cartesian plane mirroring itself on the y-axis. i/18-8 is y value on cartesian plane mirroring itself on the x-axis. Sum them together to get 1:1 diagonal (which causes numeric shift to form on 1:1 diamond. (32:57)-v is unichar numeric value for ASCII 0-9. 10 new line. \$\endgroup\$ – Albert Renshaw Feb 14 '17 at 9:13
14
\$\begingroup\$

Python 2, 60 59

for n in`111111111**2`:print`int('1'*int(n))**2`.center(17)

Abuses backticks and repunits.

\$\endgroup\$
  • \$\begingroup\$ The space after in keyword can be removed, just like you did with print keyboard. \$\endgroup\$ – Konrad Borowski Oct 23 '12 at 17:02
  • \$\begingroup\$ @GlitchMr: Thanks! Updated. \$\endgroup\$ – nneonneo Oct 23 '12 at 17:06
  • \$\begingroup\$ I get an extra L in the middle seven lines of output. \$\endgroup\$ – Steven Rumbalski Mar 28 '13 at 14:56
  • \$\begingroup\$ You shouldn't...what version of Python are you using? \$\endgroup\$ – nneonneo Mar 28 '13 at 15:07
12
\$\begingroup\$

GolfScript, 33 31 30 characters

Another GolfScript solution

17,{8-abs." "*10@-,1>.-1%1>n}%

Thank you to @PeterTaylor for another char.

Previos versions:

17,{8-abs" "*9,{)+}/9<.-1%1>+}%n*

(run online)

17,{8-abs" "*9,{)+}/9<.-1%1>n}%
\$\endgroup\$
  • 1
    \$\begingroup\$ You don't need the trailing spaces (the text in the question doesn't have them), so you can skip adding the numbers to the spaces and save one char as 17,{8-abs." "*10@-,1>.-1%1>n}% \$\endgroup\$ – Peter Taylor Mar 28 '13 at 16:21
12
\$\begingroup\$

Mathematica 55 50 45 41 38

(10^{9-Abs@Range[-8,8]}-1)^2/81//Grid

Grid[(10^Array[{9}-Abs[#-9]&,17]-1)^2/81]

Mathematica graphics

\$\endgroup\$
  • 1
    \$\begingroup\$ Very nice work. \$\endgroup\$ – DavidC Mar 22 '13 at 17:17
  • \$\begingroup\$ @DavidCarraher Thank you :D \$\endgroup\$ – chyanog Mar 23 '13 at 4:05
  • \$\begingroup\$ I echo David's remark. How did you come up with this? \$\endgroup\$ – Mr.Wizard Mar 27 '13 at 7:02
  • \$\begingroup\$ May I update your answer with the shorter modification I wrote? \$\endgroup\$ – Mr.Wizard Mar 27 '13 at 21:49
  • \$\begingroup\$ @Mr.Wizard Certainly. \$\endgroup\$ – chyanog Mar 28 '13 at 4:16
10
\$\begingroup\$

Javascript, 114

My first entry on Codegolf!

for(l=n=1;l<18;n-=2*(++l>9)-1,console.log(s+z)){for(x=n,s="";x<9;x++)z=s+=" ";for(x=v=1;x<2*n;v-=2*(++x>n)-1)s+=v}

If this can be shortened any further, please comment :)

\$\endgroup\$
  • \$\begingroup\$ damn it!! I missed the space and made half diamond. I must sleep now \$\endgroup\$ – Joomler Nov 23 '16 at 12:41
9
\$\begingroup\$

PHP, 92 90 characters

<?for($a=-8;$a<9;$a++){for($b=-8;$b<9;){$c=abs($a)+abs($b++);echo$c>8?" ":9-$c;}echo"\n";}

Calculates and prints the Manhattan distance of the position from the centre. Prints a space if it's less than 1.

An anonymous user suggested the following improvement (84 characters):

<?for($a=-8;$a<9;$a++,print~õ)for($b=-8;$b<9;print$c>8?~ß:9-$c)$c=abs($a)+abs($b++);
\$\endgroup\$
  • \$\begingroup\$ 2nd one doesn't work. \$\endgroup\$ – Christian Mar 22 '13 at 4:30
  • \$\begingroup\$ I know it's very late, but I always have a need to golf when I see PHP scripts. 83 bytes with <? skipped per meta. Also, you seem to have some encoding problems in the second code. \$\endgroup\$ – RedClover May 26 '18 at 18:56
  • \$\begingroup\$ @Soaku The second one is not mine. It was suggested as an edit to my answer by an anonymous user. I just added it without checking - not really sure why the user didn't just post it as their own attempt really. The meta question post-dates this answer by almost 3 years. \$\endgroup\$ – Gareth May 26 '18 at 19:43
  • \$\begingroup\$ I meant, that I don't include <? in the bytecount. I've made some other improvements too. \$\endgroup\$ – RedClover May 26 '18 at 19:58
8
\$\begingroup\$

Charcoal (non-competing), 13 bytes

Not competing because the language is (much) newer than the question.

F⁹«GX⁻⁹ιI⁺ι¹→

Try it online!

How?

Draws nine, successively smaller, concentric number-diamonds on top of each other:

F⁹«   Loop ι from 0 to 8:
GX     Draw a (filled) polygon with four equilateral diagonal sides
⁻⁹ι      of length 9-ι
I⁺ι¹    using str(ι+1) as the character
→       Move right one space before drawing the next one
\$\endgroup\$
  • 4
    \$\begingroup\$ This should be now competing as per new consensus in meta. \$\endgroup\$ – officialaimm Sep 13 '17 at 8:16
7
\$\begingroup\$

Common Lisp, 113 characters

(defun x(n)(if(= n 0)1(+(expt 10 n)(x(1- n)))))(dotimes(n 17)(format t"~17:@<~d~>~%"(expt(x(- 8(abs(- n 8))))2)))

First I noticed that the elements of the diamond could be expressed like so:

  1   =   1 ^ 2
 121  =  11 ^ 2
12321 = 111 ^ 2

etc.

x recursively calculates the base (1, 11, 111, etc), which is squared, and then printed centered by format. To make the numbers go up to the highest term and back down again I used (- 8 (abs (- n 8))) to avoid a second loop

\$\endgroup\$
7
\$\begingroup\$

JavaScript, 81

for(i=9;--i+9;console.log(s))for(j=9;j;s=j--^9?k>0?k+s+k:" "+s:k+"")k=i<0?j+i:j-i
\$\endgroup\$
6
\$\begingroup\$

PowerShell (2 options): 92 84 45 bytes

1..8+9..1|%{' '*(9-$_)+[int64]($x='1'*$_)*$x}
1..9+8..1|%{' '*(9-$_)+[int64]($x='1'*$_)*$x}

Thanks to Strigoides for the hint to use 1^2,11^2,111^2...


Shaved some characters by:

  • Eliminating $w.
  • Nested the definition of $x in place of its first use.
  • Took some clues from Rynant's solution:
    • Combined the integer arrays with + instead of , which allows elimination of the parenthesis around the arrays and a layer of nesting in the loops.
    • Used 9-$_ to calculate the length of spaces needed, instead of more complicated maths and object methods. This also eliminated the need for $y.

Explanation:

1..8+9..1 or 1..9+8..1 generates an array of integers ascending from 1 to 9 then descending back to 1.

|%{...} pipes the integer array into a ForEach-Object loop via the built-in alias %.

' '*(9-$_)+ subtracts the current integer from 9, then creates a string of that many spaces at the start of the output for this line.

[int64]($x='1'*$_)*$x defines $x as a string of 1s as long as the current integer is large. Then it's converted to int64 (required to properly output 1111111112 without using E notation) and squared.

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ You can save a byte by casting to a long instead of int64 \$\endgroup\$ – Veskah Jul 26 '18 at 20:50
  • \$\begingroup\$ Another way to save a byte 1..8+9..1|%{' '*(9-$_)+ +($x='1'*$_+'L')*$x} \$\endgroup\$ – mazzy Sep 19 '18 at 12:12
5
\$\begingroup\$

Vim, 62 39 38 keystrokes

Thanks to @DJMcMayhem for saving a ton of bytes!

My first Vim answer, so exciting!

i12345678987654321<ESC>qqYP9|xxI <ESC>YGpHq7@q

I tried to write the numbers via a recording, but it is much longer

Try it online!

Explanation:

i123 ... 321<ESC>                   Write this in insert mode and enter normal mode
qq                                  Start recording into register q
  YP                                Yank this entire line and Paste above
    9|                              Go to the 9th column
      xx                            Delete character under cursor twice
        I <ESC>                     Go to the beginning of the line and insert a space and enter normal mode
               Y                    Yank this entire line
                G                   Go to the last line
                 p                  Paste in the line below
                  H                 Go to the first line
                   q                End recording
                    7@q             Repeat this 7 times

EDIT:

I used H instead of gg and saved 1 byte

\$\endgroup\$
  • \$\begingroup\$ You can delete the ma and change `ai<space> to I<space>. \$\endgroup\$ – DJMcMayhem Dec 4 '16 at 20:20
  • \$\begingroup\$ Also, you could probably delete stage 3 if you change stage 1 to pasting above and below. \$\endgroup\$ – DJMcMayhem Dec 4 '16 at 20:21
  • \$\begingroup\$ @DJMcMayhem Thank you for the suggestion! I initially was thinking about introducing a new register for the copied bits, but this is much shorter! \$\endgroup\$ – Kritixi Lithos Dec 5 '16 at 8:37
5
\$\begingroup\$

APL (Dyalog Classic), 20 19 bytes

(⍉⊢⍪1↓⊖)⍣2⌽↑,⍨\1↓⎕d

Try it online!

⎕d are the digits '0123456789'

1↓ drop the first ('0')

,⍨\ swapped catenate scan, i.e. the reversed prefixes '1' '21' '321' ... '987654321'

mix into a matrix padded with spaces:

1
21
321
...
987654321

reverse the matrix horizontally

(...)⍣2 do this twice:

⍉⊢⍪1↓⊖ the transposition () of the matrix itself () concatenated vertically () with the vertically inverted matrix () without its first row (1↓)

\$\endgroup\$
4
\$\begingroup\$

R, 71 characters

For the records:

s=c(1:9,8:1);for(i in s)cat(rep(" ",9-i),s[0:i],s[(i-1):0],"\n",sep="")
\$\endgroup\$
  • \$\begingroup\$ +1 - can save a few with message(rep(" ",9-i),s[c(1:i,i:1-1)]) \$\endgroup\$ – flodel Apr 11 '15 at 17:14
  • \$\begingroup\$ @flodel you'd have to note that that prints to stderr, and you could also do for(i in s<-c(1:9,8:1))... to save a byte \$\endgroup\$ – Giuseppe Dec 14 '17 at 17:10
  • \$\begingroup\$ 64 bytes \$\endgroup\$ – Giuseppe Apr 6 '18 at 21:05
4
\$\begingroup\$

k (64 50 chars)

-1'(::;1_|:)@\:((|!9)#'" "),'$i*i:"J"$(1+!9)#'"1";

Old method:

-1',/(::;1_|:)@\:((|!9)#\:" "),',/'+(::;1_'|:')@\:i#\:,/$i:1+!9;

\$\endgroup\$
  • \$\begingroup\$ (1+!9)#'"1" is ,\9#"1" \$\endgroup\$ – ngn Dec 5 '16 at 22:42
4
\$\begingroup\$

CJam, 31 27 bytes

CJam is a lot newer than this challenge, so this answer is not eligible for being accepted. This was a neat little Saturday evening challenge, though. ;)

8S*9,:)+9*9/2%{_W%1>+z}2*N*

Test it here.

The idea is to form the upper left quadrant first. Here is how that works:

First, form the string " 123456789", using 8S*9,:)+. This string is 17 characters long. Now we repeat the string 9 times, and then split it into substrings of length 9 with 9/. The mismatch between 9 and 17 will offset every other row one character to the left. Printing each substring on its own line we get:

        1
23456789 
       12
3456789  
      123
456789   
     1234
56789    
    12345
6789     
   123456
789      
  1234567
89       
 12345678
9        
123456789

So if we just drop every other row (which conveniently works by doing 2%), we obtain one quadrant as desired:

        1
       12
      123
     1234
    12345
   123456
  1234567
 12345678
123456789

Finally, we mirror this twice, transposing the grid in between to ensure that the two mirroring operations go along different axes. The mirroring itself is just

_      "Duplicate all rows.";
 W%    "Reverse their order.";
   1>  "Discard the first row (the centre row).";
     + "Add the other rows.";

Lastly, we just join all lines with newlines, with N*.

\$\endgroup\$
3
\$\begingroup\$

GolfScript, 36 chars

Assuming that this is meant as a challenge, here's a basic GolfScript solution:

9,.);\-1%+:a{a{1$+7-.0>\" "if}%\;n}%
\$\endgroup\$
3
\$\begingroup\$

Ruby, 76 characters

def f(a)a+a.reverse[1..-1]end;puts f [*1..9].map{|i|f([*1..i]*'').center 17}

Improvements welcome. :)

\$\endgroup\$
  • 1
    \$\begingroup\$ 69 chars: f=->x{[*1..x]+[*1...x].reverse};puts f[9].map{|i|(f[i]*'').center 17} \$\endgroup\$ – Patrick Oscity Nov 5 '12 at 21:11
  • \$\begingroup\$ Great comment, I didn't know the '...' and didn't understand how this could work. \$\endgroup\$ – G B Nov 25 '16 at 7:10
  • \$\begingroup\$ 60 chars: [*-8..8].map{|i|puts' '*i.abs+"#{eval [?1*(9-i.abs)]*2*?*}"} \$\endgroup\$ – G B Nov 25 '16 at 8:05
3
\$\begingroup\$

Befunge-93, 155 chars

9:v:<,+55<v5*88<v-\9:$_68v
> v>     ^>3p2vpv  -1<!  *
, 1^  2p45*3+9<4:    ,:  +
g -^_75g94+4pg7^!    +^ ,<
1 : ^ `0    :-1$_:68*^$
^1_$:55+\-0\>:#$1-#$:_^

Try it online!

It could definitely be golfed more, but it's my first Funge program and my head already hurts. Had a lot of fun, though

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 170 bytes

My first code golf :)

Golfed

a="";function b(c){a+=" ".repeat(10-c);for(i=1;i<c;i++)a+=i;for(i=2;i<c;i++)a+=c-i;a+="\n";}for(i=2;i<11;i++)b(i);for(i=9;i>1;i--)b(i);document.write("<pre>"+a+"</pre>");

Ungolfed

var str = "";
function row(line) {
    str += " ".repeat(10 - line);
    for (var i = 1; i < line; i++) {
        str += i;
    }
    for (var i = 2; i < line; i++) {
        str += line - i;
    }
    str += "\n";
}
for (var line = 2; line < 11; line++) {
    row(line);
}
for (var line = 9; line > 1; line--) {
    row(line);
}
document.write("<pre>" + str + "</pre>");
\$\endgroup\$
2
\$\begingroup\$

Perl 56 54 characters

Added 1 char for the -p switch.

Uses squared repunits to generate the sequence.

s//12345678987654321/;s|(.)|$/.$"x(9-$1).(1x$1)**2|eg
\$\endgroup\$
2
\$\begingroup\$

Perl, 43+1

adding +1 for -E which is required for say

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

edit: shortened a bit

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

Python, 65

for i in map(int,str(int('1'*9)**2)):print' '*(9-i),int('1'*i)**2
\$\endgroup\$
  • \$\begingroup\$ Try prepending I=int; to your code and replacing all subsequent instances of int with I \$\endgroup\$ – Cyoce Nov 27 '16 at 2:51
  • \$\begingroup\$ @Cyoce I had thought of that. It would save 2 chars each time int is used, and it's used 3 times, so it saves 6 chars at a cost of 6 chars. \$\endgroup\$ – cardboard_box Nov 27 '16 at 4:05
2
\$\begingroup\$

Groovy 77 75

i=(-8..9);i.each{a->i.each{c=a.abs()+it.abs();print c>8?' ':9-c};println""}

old version:

(-8..9).each{a->(-8..9).each{c=a.abs()+it.abs();print c>8?' ':9-c};println""}
\$\endgroup\$
  • \$\begingroup\$ added a 57 char groovy solution. You can replace both each with any to save two chars. \$\endgroup\$ – Matias Bjarland Feb 12 '17 at 10:52
2
\$\begingroup\$

Scala - 86 characters

val a="543210/.-./012345";for(i<-a){for(j<-a;k=99-i-j)print(if(k<1)" "else k);println}
\$\endgroup\$
2
\$\begingroup\$

Javascript, 137

With recursion:

function p(l,n,s){for(i=l;i;s+=" ",i--);for(i=1;i<=n;s+=i++);for(i-=2;i>0;s+=i--);return(s+="\n")+(l?p(l-1,n+1,"")+s:"")}alert(p(8,1,""))

First time on CG :)

Or 118

If I can find a JS implementation that executes 111111111**2 with higher precision.
(Here: 12345678987654320).

a="1",o="\n";for(i=0;i<9;i++,o+="         ".substr(i)+a*a+"\n",a+="1");for(i=8;i;i--)o+=o.split("\n")[i]+"\n";alert(o)
\$\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.