| Bytes | Lang | Time | Link |
|---|---|---|---|
| 071 | AWK | 250728T201102Z | xrs |
| 105 | Swift | 230723T202200Z | macOSist |
| 095 | Tcl | 181220T204916Z | david |
| 019 | Vyxal 3 | 250415T092928Z | Themooni |
| 096 | Tcl | 170204T150119Z | sergiol |
| 057 | Zsh | 240809T081012Z | Donat |
| 054 | Easyfuck | 240325T010452Z | Quadrupl |
| 025 | Jelly | 240320T223924Z | Aaron |
| 017 | Uiua | 240320T170704Z | lynn |
| 008 | Vyxal 3 jR | 240308T140713Z | pacman25 |
| 033 | Uiua | 240308T133723Z | noodle p |
| 020 | J | 240307T212720Z | algorith |
| 021 | K ngn/k | 240307T202611Z | algorith |
| 077 | YASEPL | 240208T145555Z | madeforl |
| 051 | Julia 1.0 | 210226T103553Z | MarcMush |
| 071 | R | 121014T104023Z | Paolo |
| 036 | GolfScript | 121012T162253Z | Ilmari K |
| 030 | GolfScript | 121012T171510Z | Howard |
| 113 | Common Lisp | 121014T023114Z | Strigoid |
| 079 | C gcc | 121019T025321Z | baby-rab |
| 081 | JavaScript V8 | 130323T010222Z | copy |
| 059 | Python 2 | 121022T184155Z | nneonneo |
| 107 | FMSLogo | 231231T220857Z | jan |
| 158 | owl lisp | 230826T070141Z | krzych |
| 067 | JavaScript V8 | 230722T214701Z | Donat |
| 077 | JavaScript V8 | 230720T154110Z | Fhuvi |
| 167 | Go | 230316T184400Z | The Thon |
| 065 | Bash | 230719T202729Z | Donat |
| 157 | Java 8 OpenJDK 8 | 230317T182504Z | Fhuvi |
| 033 | Perl 5 | 200630T200734Z | Donat |
| 108 | Regenerate | 210629T034952Z | DLosc |
| 018 | MATL | 180726T151928Z | Sundar R |
| 007 | Vyxal C | 220622T153950Z | naffetS |
| 014 | Vyxal | 220520T192753Z | math sca |
| 024 | K ngn/k | 220118T191439Z | coltim |
| 131 | PICO8 | 211006T192340Z | weatherm |
| 100 | FreeBASIC | 211005T061840Z | user3234 |
| 034 | Vim | 161204T191752Z | user4180 |
| nan | Raku | 121021T173616Z | null |
| 054 | Ruby | 121014T140458Z | Mark Ree |
| 017 | Vyxal C | 210908T081443Z | SjoerdPe |
| 059 | jq rn | 210904T202223Z | DLosc |
| 055 | jq nr | 210905T200119Z | lynn |
| 013 | Charcoal | 161123T043809Z | DLosc |
| 080 | Excel | 210904T144851Z | Axuary |
| 079 | Haskell | 210302T222357Z | Donat |
| 078 | C gcc | 210415T035855Z | l4m2 |
| 058 | Duocentehexaquinquagesimal | 210415T021909Z | Makonede |
| 107 | JavaScript Node.js | 210328T070039Z | mekb |
| 255 | Templates Considered Harmful | 210304T070404Z | Zack C. |
| 073 | JavaScript Node.js | 200722T155040Z | Donat |
| 009 | Vyxal | 210226T014104Z | lyxal |
| 054 | Python 3 | 200930T222401Z | Donat |
| 075 | JavaScript V8 | 200711T185348Z | Donat |
| 011 | Jelly | 171021T194424Z | Mr. Xcod |
| 075 | JavaScript V8 | 200712T105737Z | Donat |
| 080 | Lua | 210219T161637Z | Donat |
| 061 | Zsh | 210118T183719Z | pxeger |
| 142 | C gcc | 210117T220209Z | anotherO |
| 011 | MathGolf | 210118T105958Z | Kevin Cr |
| 230 | Java 10 IntStream | 210117T153643Z | user1004 |
| 468 | StupidStackLanguage | 200706T100640Z | Lebster |
| 123 | Python 3 | 200912T200631Z | Lebster |
| 132 | Java JDK | 200707T210946Z | Donat |
| 115 | Clojure | 200712T233046Z | Donat |
| 104 | TSQL | 200712T213535Z | t-clause |
| 160 | Clojure | 200712T195221Z | colinkah |
| 087 | Javascript | 200708T205510Z | Christia |
| 156 | Java | 200628T214213Z | Donat |
| 086 | REXX | 200629T053441Z | Turophil |
| 139 | /// | 200628T161218Z | nph |
| 050 | Perl 5 | 191018T212439Z | Denis Ib |
| 155 | Java | 191017T235239Z | Matthew |
| 011 | Japt R | 170704T132140Z | Shaggy |
| 103 | C# Visual C# Interactive Compiler | 181221T104750Z | dana |
| 084 | JavaScript | 181220T004015Z | Oliver |
| 019 | APL Dyalog Classic | 161204T184406Z | ngn |
| 045 | Powershell | 180919T122516Z | mazzy |
| 118 | C | 180726T201011Z | J.Barber |
| 062 | R | 180726T173122Z | Giuseppe |
| 154 | brainfuck | 180726T105131Z | Dorian |
| 010 | 05AB1E | 161123T045302Z | Oliver N |
| 007 | Canvas | 180406T214308Z | dzaima |
| 011 | Japt R | 180525T080259Z | Bubbler |
| 125 | Kotlin | 180406T212349Z | JohnWell |
| 010 | Stax | 180406T205714Z | recursiv |
| 115 | TSQL | 180405T150427Z | BradC |
| 114 | Javascript | 121020T171002Z | tomsmedi |
| 049 | Attache | 180406T053404Z | Conor O& |
| 124 | /// | 180405T185519Z | Conor O& |
| 014 | Canvas | 180405T171105Z | hakr14 |
| 072 | Python 2 | 180405T162019Z | ShadowCa |
| 009 | 05AB1E | 180405T124420Z | Erik the |
| 132 | /// | 180405T124105Z | Erik the |
| 089 | Yabasic | 180216T141854Z | Taylor R |
| 021 | APL Dyalog Classic | 180112T033254Z | ngn |
| 076 | Excel VBA | 161205T150518Z | Taylor R |
| 022 | Recursiva | 170913T081534Z | 0xffcour |
| 009 | Gaia | 170726T155254Z | Business |
| 059 | Python 2 | 170720T213839Z | Koishore |
| 045 | PowerShell 2 options | 131120T175259Z | Iszi |
| 081 | PHP | 170704T105056Z | Евгений |
| 037 | k | 170614T004929Z | zgrep |
| 038 | Octave | 170228T224927Z | flawr |
| 208 | Matlab | 170228T215755Z | EBH |
| 1446 | Deadfish | 170212T232330Z | Uriel |
| 233 | /// | 170224T230925Z | sporkl |
| 170 | JavaScript | 161125T093339Z | Cr4xy |
| 057 | Groovy | 170212T102641Z | Matias B |
| 061 | Python 2 | 121012T175423Z | Steven R |
| 179 | Clojure | 170205T205259Z | Carcigen |
| 145 | Common Lisp SBCL | 170211T180514Z | user6516 |
| 065 | SmileBASIC | 170201T200815Z | 12Me21 |
| 055 | Ruby | 161204T131750Z | Vasu Ada |
| 051 | Mathematica | 121012T165338Z | DavidC |
| 059 | Ruby | 161125T084201Z | G B |
AWK, 71 bytes
END{for(;j++<17;)print j<10?z[j]=sprintf("%*s",9-j,X)(x=x 1)^2:z[18-j]}
Swift, 132 131 130 105 bytes
let r={(1..<$0)+(1...$0).reversed()}
r(9).map{print({String.init}()(" ",9-$0),""+r($0).flatMap{"\($0)"})}
Tcl, 99 95 bytes
thx to @sergiol
time {puts [format %[expr abs([incr i]-9)]s ""][expr [string repe 1 [expr 9-abs($i-9)]]**2]} 17
Vyxal 3, 19 bytes
9ɾPæᐕJ⍾æᐕJƓ9$-␣×p“,
9ɾPæᐕJ⍾æᐕJƓ9$-␣×p“,
9ɾ # range from 1 to 9
P # prefixes
æᐕJ # duplicate, reverse, drop the duplicated middle, and join
⍾ # map over each:
æᐕJ # duplicate, reverse, drop the duplicated max, and join
␣×p # prepend x spaces where x is:
Ɠ9$- # 9 - max
“, # join on empty string, and print each line with newline
💎
Created with the help of Luminespire.
<script type="vyxal3">
␣,
9ɾPæᐕJ⍾æᐕJƓ9$-␣×p“,
</script>
<script>
args=[]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
Tcl, 122 110 96 bytes
time {puts [format %[expr 8+[set x [expr 9-abs([incr i]-9)]]]s [expr [string repe 1 $x]**2]]} 17
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
Jelly, 25 bytes
L_8Cx@32;
9RR€+48Ç€ŒBŒḄỌY
Definitely have some extra bytes because I get messed up on when and how things are converted to strings in jelly
L_8Cx@32; # Replicate a space (ascii 32) 9-length amount of times
# and prepend that to the original input
9RR€+48Ç€ŒBŒḄỌY
9R # [1..9]
R€ # Each of those as a range
# [[1], [1,2], [1,2,3] ... ]
+48 # Turn the number to ascii
Ç€ # Call the space prepender (previous link) on each
ŒB # Mirror each element of the list
ŒḄ # Mirror the entire list (bottom half of diamond)
ỌY # Convert to characters and join with linefeeds
Uiua, 17 bytes
+@!÷⊃≥+8⊞+.↧⇌.⇡17
We use ⇡17 to make a range from 0 to 16, and then min it by its reverse ↧⇌. to get
[0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0]
Then ⊞+. makes an addition table of this list with itself:
╭─
╷ 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2
3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3
4 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 4
5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5
6 7 8 9 10 11 12 13 14 13 12 11 10 9 8 7 6
7 8 9 10 11 12 13 14 15 14 13 12 11 10 9 8 7
8 9 10 11 12 13 14 15 16 15 14 13 12 11 10 9 8
7 8 9 10 11 12 13 14 15 14 13 12 11 10 9 8 7
6 7 8 9 10 11 12 13 14 13 12 11 10 9 8 7 6
5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5
4 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 4
3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3
2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0
╯
Now we do something clever. Let's call this array x. Then ⊃≥+8 pushes +8x and ≥8x, and ÷ divides the two.
╭─
╷ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 16 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
∞ ∞ ∞ ∞ ∞ ∞ ∞ 16 17 16 ∞ ∞ ∞ ∞ ∞ ∞ ∞
∞ ∞ ∞ ∞ ∞ ∞ 16 17 18 17 16 ∞ ∞ ∞ ∞ ∞ ∞
∞ ∞ ∞ ∞ ∞ 16 17 18 19 18 17 16 ∞ ∞ ∞ ∞ ∞
∞ ∞ ∞ ∞ 16 17 18 19 20 19 18 17 16 ∞ ∞ ∞ ∞
∞ ∞ ∞ 16 17 18 19 20 21 20 19 18 17 16 ∞ ∞ ∞
∞ ∞ 16 17 18 19 20 21 22 21 20 19 18 17 16 ∞ ∞
∞ 16 17 18 19 20 21 22 23 22 21 20 19 18 17 16 ∞
16 17 18 19 20 21 22 23 24 23 22 21 20 19 18 17 16
∞ 16 17 18 19 20 21 22 23 22 21 20 19 18 17 16 ∞
∞ ∞ 16 17 18 19 20 21 22 21 20 19 18 17 16 ∞ ∞
∞ ∞ ∞ 16 17 18 19 20 21 20 19 18 17 16 ∞ ∞ ∞
∞ ∞ ∞ ∞ 16 17 18 19 20 19 18 17 16 ∞ ∞ ∞ ∞
∞ ∞ ∞ ∞ ∞ 16 17 18 19 18 17 16 ∞ ∞ ∞ ∞ ∞
∞ ∞ ∞ ∞ ∞ ∞ 16 17 18 17 16 ∞ ∞ ∞ ∞ ∞ ∞
∞ ∞ ∞ ∞ ∞ ∞ ∞ 16 17 16 ∞ ∞ ∞ ∞ ∞ ∞ ∞
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 16 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
╯
And now +@! adds these as offsets to the char ! (ASCII 33), which maps 16…24 to ASCII digits 1…9 but miraculously treats ∞ as -1 to produce a space (ASCII 32).
╭─
╷ " 1 "
" 121 "
" 12321 "
" 1234321 "
" 123454321 "
" 12345654321 "
" 1234567654321 "
" 123456787654321 "
"12345678987654321"
" 123456787654321 "
" 1234567654321 "
" 12345654321 "
" 123454321 "
" 1234321 "
" 12321 "
" 121 "
" 1 "
╯
Vyxal 3 jR, 8 bytes
9ᵖэ“9«ṅṅ
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.
Uiua, 33 bytes
⊂:↘1⇌.≡(⊂⇌↘1.)⬚@ ≡°□+@1≡(□⇌⇡)+1⇡9
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
J, 20 bytes
(1{2{.":)@-9-+/~|i:8
Try it online! Exploits a few silly features of J, some of which are already present in other solutions.
(1{2{.":)@-9-+/~|i:8
i:8 NB. -8 -7 ... 7 8
| NB. abs
+/~ NB. addition table: 16 in corner down to 0 in center
9- NB. subtract from 9: -7 in corner up to 9 in center
( )@- NB. for each number, negate and...
2{.": NB. convert to string and right pad to width 2
1{ NB. take second character
": prints negative signs so 2{.": gives a 2 character string which ends in a space for 0 through 9 and in a digit for -1 or less. Also of note is the symmetrical-integers-around-zero primitive i: which combines with absolute value | to give a terse palindrome of ints.
K (ngn/k), 21 bytes
Hard to do better than directly translating from ngn's APL.
2{+x,1_|x}/-9$,\$1+!9
2{+x,1_|x}/-9$,\$1+!9
1+!9 / 1 2...9
,\$ / string prefixes
-9$ / left pad all to width 9
{+x,1_|x} / subroutine with argument x
|x / reverse list x
1_ / drop first
x, / append to x
+ / transpose as matrix of chars
2 / / do that twice
Here's a fun different approach but I can only squeeze it down to 22.
($7-a+/:a&:|a:!17)@''1
a:!17 / a is assigned 0 1...16
|a / reverse a -> 16 15...0
a& / min with a -> 0 1...7 8 7...0
a : / reassign to a
a+/:a / addition table of a with itself
/ -> 0 in corners up to 16 in middle
7- / subtract from 7
/ -> 7 in corners down to -9 in middle
$ / convert to strings
( )@''1 / 2nd character of each string
/ -> space if 0...9, digit if negative
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
R, 71 bytes
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="")
GolfScript, 36 bytes
Assuming that this is meant as a code-golf challenge, here's a basic GolfScript solution:
9,.);\-1%+:a{a{1$+7-.0>\" "if}%\;n}%
GolfScript, 33 31 30 bytes
Another GolfScript solution
17,{8-abs." "*10@-,1>.-1%1>n}%
Thank you to @PeterTaylor for another char.
Previous versions:
17,{8-abs" "*9,{)+}/9<.-1%1>+}%n*
17,{8-abs" "*9,{)+}/9<.-1%1>n}%
Common Lisp, 113 bytes
(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
C (gcc), 79 bytes
v;main(i){for(;i<307;putchar(i++%18?v>8?32:57-v:10))v=abs(i%18-9)+abs(i/18-8);}
JavaScript (V8), 81 bytes
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
Python 2, 60 59 bytes
for n in`111111111**2`:print`int('1'*int(n))**2`.center(17)
Abuses backticks and repunits.
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 2trick doesn't work because FMSLogo renders this number as1.23456789876543e+16. member a boutputs the segment of b from the first occurrence of a in it to the end, and sor :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
maptakes, have?stand for their parameter by default. - For each line,
wordconcatenates together:map["\ ]r 9-?, where every character inr 9-?is replaced with a spacereverse r ?-1,r ?,"\n
owl lisp, 158 bytes
(map (λ (x) (map display (append (map (λ (_) " ") (iota 1 1 (- 10 x))) (iota 1 1 (+ 1 x)) (iota (- x 1) -1 0) `("\n")))) (append (iota 1 1 9) (iota 9 -1 0)))
un-golfed
(map (λ (x)
(map display
(append
(map (λ (_) " ") (iota 1 1 (- 10 x)))
(iota 1 1 (+ 1 x))
(iota (- x 1) -1 0)
`("\n"))))
(append (iota 1 1 9)
(iota 9 -1 0)))
JavaScript (V8), 67 bytes
z="9"
for(z of(s=n=>n<z?n+s(n+1)+n:z)(1))print("".padEnd(9-z)+s(1))
One byte less thanks to Fhuvi
JavaScript (V8), 68 bytes
s=n=>n<z?n+s(n+1)+n:z
z="9"
for(z of s(1))print("".padEnd(9-z)+s(1))
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))
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 :(
Go, 239 196 187 185 167 bytes
import(."fmt";."strings")
func g(i int){P:=Print
P(Repeat(" ",9-i))
j:=1
for;j<i;j++{P(j)}
for;j>0;j--{P(j)}
P(`
`)}
func f(){i:=1
for;i<9;i++{g(i)}
for;i>0;i--{g(i)}}
My first attempt at golfing in Go.
-18 thanks to @c--
Commented (Old)
import (."fmt"; ."strings") // Import modules for later
func g(i int) { // Helper function g taking an int i
P := Print // Useful function for later
P(Repeat(" ", (9 - i))) // Repeat a space 9-i times and print
for j := 1; j <= i; j++ { P(j) } // Print each j for j in [1 .. i]
for k := i-1; k > 0; k-- { P(k) } // Print each k for k in [i .. 1]
P("\n") } // Print a newline
func f(){ // Function boilerplate
for i := 1; i < 10; i++ { g(i) } // Call g for each i in [1 .. 9]
for i := 8; i > 0; i-- { g(i) }} // Call g for each i in [8 .. 1]
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);}}
Perl 5, 33 bytes
say$"x abs.(1x(9-abs))**2for-8..8
Using 11 * 11 = 121, 111 * 111 = 12321, 1111 * 1111 = 1234321, ...
3 bytes saved thanks to naffetS.
Regenerate, 109 108 bytes
(( {#2-1}| {8})($3${#3+1}|1)(${#4+1}$4|)\n){9}(( $6| )(${$7/10}|${$3/10})(${$8-1+1/$8*(#7-1)}|7){#7-1}\n){8}
Explanation
The solution is divided into two parts, which generate the upper half of the diamond (including the center line) and the lower half, respectively. The upper half consists of group 1, repeated nine times (...\n){9}; the lower half consists of group 5, repeated eight times (...\n){8}.
The first part of each group handles the spaces. (I've replaced space with underscore in the explanations for better visibility.)
Upper-half spaces:
( {#2-1}| {8})
( ) Group 2:
_{#2-1} Space, repeated one less time than the previous length of group 2
| Or, if this is the first row and there is no previous value of
group 2...
_{8} Space, repeated eight times
Lower-half spaces:
( $6| )
( ) Group 6:
_$6 Space, concatenated to the previous value of group 6
| Or, if this is the first row and there is no previous value of
group 6...
_ A single space
Now for the diamond itself. It can be divided into four quadrants, and I used three different approaches to generate them:
Top left & top right: concatenation
We generate the left half (including the center) of each row with group 3, and the right half with group 4. For both halves, observe that we can generate the next row by concatenating a digit to the previous row:
1234 321
| |
v v
12345 4321
So we can use a similar approach to what we did for the bottom-half spaces. The only difference is that the character we're concatenating at each step needs to be calculated.
Top left quadrant:
($3${#3+1}|1)
( ) Group 3:
$3 Previous value of group 3
${ } concatenated to the result of...
#3+1 Previous length of group 3 plus 1
| Or, if this is the first row and there is no previous value of
group 3...
1 The digit 1
Top right quadrant:
(${#4+1}$4|)
( ) Group 4:
${ } The result of...
#4+1 Previous length of group 4 plus 1
$4 concatenated to the previous value of group 4
| Or, if this is the first row and there is no previous value of
group 4...
Empty string
Bottom left: integer division
We generate the left half (including the center) of each row with group 7. Observe that each row is obtained by removing the rightmost digit from the previous row:
12345
|
v
1234
If we treat the entire thing as an integer, we can perform this transformation easily by dividing it by ten:
(${$7/10}|${$3/10})
( ) Group 7:
${ } The result of...
$7/10 Previous value of group 7, divided by 10
| Or, if there is no previous value of group 7...
${ } The result of...
$3/10 Group 3 (the final upper-left quadrant row) divided by 10
Bottom right: digit-by-digit
There isn't an easy way to remove a digit from the left side of a previous match, so for the right half of each bottom row, we resort to generating one digit at a time with group 8. Here are the first three rows that we need to generate:
7654321
654321
54321
Each digit is one less than the previous digit, except that we have to "reset" when we hit 1. The reset value is always one less than the length of the left half of the row, which we have access to as group 7.
How can we tell when we've hit 1? I used an integer-division trick I first developed for programming in Acc!!: if N is a positive integer, then 1/N will be one iff N = 1 and zero if N > 1. Multiplying this quantity by (length of group 7) - 1 gives us the reset we need.
(${$8-1+1/$8*(#7-1)}|7){#7-1}
( ) Group 8 (generates one digit at a time):
${ } The result of...
$8-1 Previous value of group 8, minus 1
+ Plus
#7-1 The length of group 7 minus 1
1/$8*( ) if the previous value of group 8 equals 1
| Or, if there is no previous value of group 8...
7 The digit 7
{ } Generate this many digits on each row:
#7-1 One less than the length of group 7
MATL, 18 bytes
9Zv-9Zvq!-t0>48*+c
9Zv - symmetric range from [1:9 8:-1:1]
-9Zv - reverse symmteric range, [9:-1:1 2:9]
q! - decrement that to [8:-1:0 1:8] and transpose to vertical
- - broadcast subtract - matrix of each value in the second range subtracted from each value in the first range
t0> - duplicate that and get a logical matrix of 1s where it's > 0, 0s elsewhere
48* - multiply by 48 to change 1s to 48s (ASCII '0')
+ - add that to the original matrix
c - convert to char and implicitly display
Vyxal, 14 bytes
9ɾ∞(9n-Inɾ∞Jṅ,
Explanation
9ɾ∞(9n-Inɾ∞Jṅ,
9ɾ∞ Palindromise range from 1-9 incl.
( Start loop
9n-I Push n-9 spaces
nɾ∞ Palindromise range from 1-n
J Join spaces with range
ṅ, Join everything by nothing and print it out
K (ngn/k), 24 bytes
`0:2{+x,1_|x}/-9$,\$1+!9
Takes a lot of inspiration from @zgrep's k answer.
$1+!9generate"123456789",\take the prefixes, i.e.(,"1";"12";"123";...)-9$left-pad each element to nine characters (e.g.(" 1";...))2{...}/set up a do-reduce, run twice on the above list1_|xreverse the input, and drop the first character (e.g." 123"=>"21 ")x,append this to the input+transpose the result, so that the next iteration works on the opposite axis
`0:print to stdout (suppressing implicit result)
PICO-8, 131 bytes
cls()function _draw()for i=1,9do for j=i-9,0do for k=-1,1,2do for l=0,1do print(i,72*l-(l-0.5)*(i-j)*8,48+k*j*6)end end end end end
Output:
Ungolfed version:
cls() //clear screen
function _draw() //keeps commandline off
for i=1,9 do //numbers
for j=i-9,0 do //position control
for k=-1,1,2 do //y axis switch
for l=0,1 do //x axis switch
print(i,72*l-(l-0.5)*(i-j)*8,48+k*j*6)
end
end
end
end
end
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
Vim, 62 39 38 34 keystrokes
Thanks to @DJMcMayhem for saving a ton of bytes
Thanks to @AaronMiller for saving 4 bytes by generating 12345678987654321 in a different way
9i1<ESC>|C<C-r>=<C-r>"*<C-r>"
<ESC>qqYP9|xxI <ESC>YGpHq7@q
Explanation:
9i1<ESC> Write 9 '1's, leaving the cursor at the end of the line
|C Go to the first column and cut all that's to the right of the cursor (implictly into register "), entering insert mode
<C-r>= Enter an expression:
<C-r>"*<C-r>" The contents of register " multiplied with itself
<CR> Evaluate it, yielding 12345678987654321
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
Ruby, 76 69 60 54 characters
(-8..8).map{i=_1.abs;puts' '*i+"#{eval(?1*(9-i))**2}"}
(Thanks, Patrick, G B, and Slim Liser!)
Improvements welcome. :)
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
jq -rn, 76 65 60 59 bytes
-6 bytes thanks to ovs and another -6 (plus -5 more indirectly) thanks to Michael Chatiskatzi
def r:range(9),7-range(8);r|[8-.-r|"\(" "*.//1-.)"[:1]]|add
Explanation
The -n flag specifies that there is no input; the -r flag outputs the result strings, each on its own line, without quotes.
def r: Define a helper function r
range(9), which generates the numbers 0 through 8, followed by
7 - range(8); the numbers 7 through 0
Main program:
r | Start with the results of r
[ For each of those numbers, put the following in a list:
8 - . - r | 8, minus the current number, minus the results of calling r again
"\( For each of those numbers, interpolate the following into a string:
" " * . Try to repeat a space that many times
// and if the result is null (i.e. the number was less than 1),
1 - . subtract the number from 1 instead
)"
[:1] Take the first character of each resulting string
] | Take each resulting list of strings and
add concatenate it together
To help visualize what's going on, here's what just the r|[8-.-r] part does:
[8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8]
[7,6,5,4,3,2,1,0,-1,0,1,2,3,4,5,6,7]
[6,5,4,3,2,1,0,-1,-2,-1,0,1,2,3,4,5,6]
[5,4,3,2,1,0,-1,-2,-3,-2,-1,0,1,2,3,4,5]
[4,3,2,1,0,-1,-2,-3,-4,-3,-2,-1,0,1,2,3,4]
[3,2,1,0,-1,-2,-3,-4,-5,-4,-3,-2,-1,0,1,2,3]
[2,1,0,-1,-2,-3,-4,-5,-6,-5,-4,-3,-2,-1,0,1,2]
[1,0,-1,-2,-3,-4,-5,-6,-7,-6,-5,-4,-3,-2,-1,0,1]
[0,-1,-2,-3,-4,-5,-6,-7,-8,-7,-6,-5,-4,-3,-2,-1,0]
[1,0,-1,-2,-3,-4,-5,-6,-7,-6,-5,-4,-3,-2,-1,0,1]
[2,1,0,-1,-2,-3,-4,-5,-6,-5,-4,-3,-2,-1,0,1,2]
[3,2,1,0,-1,-2,-3,-4,-5,-4,-3,-2,-1,0,1,2,3]
[4,3,2,1,0,-1,-2,-3,-4,-3,-2,-1,0,1,2,3,4]
[5,4,3,2,1,0,-1,-2,-3,-2,-1,0,1,2,3,4,5]
[6,5,4,3,2,1,0,-1,-2,-1,0,1,2,3,4,5,6]
[7,6,5,4,3,2,1,0,-1,0,1,2,3,4,5,6,7]
[8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8]
Any number that's greater than 0 gets turned into that many spaces (and then trimmed back to a single space). The other numbers gets subtracted from 1 (turning 0 .. -8 into 1 .. 9) and stringified.
jq -nr, 55 bytes
range(17)-8|fabs|" "*.+"123456789"[:9-.]+"87654321"[.:]
fabs is not supported on TIO but you can try it on https://jqplay.org/.
Alternatively try it on TIO with .*.|sqrt instead of fabs, for 59 bytes.
Charcoal, 13 bytes
F⁹«GX⁻⁹ιI⁺ι¹↓
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 down one space before drawing the next one
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,"
")))
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]
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);}
Duocentehexaquinquagesimal, 58 bytes
(Úv×H.F°ǝ=ÉÈNŸ=~ʒ€
₁˨Dûl`N₆rÇX³ΣJd‘×∞*Åv€rØ(0՜֤ΔNā—St€ô
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)
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>>
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)
JavaScript (Node.js), 73 bytes
for(z of(s=z=>"".padEnd(9-z)+(BigInt(10**z)/9n)**2n)(9))console.log(s(z))
Python 3, 54 bytes
saved another 3 bytes by letting the int function crash instead of a loop condition. Thanks to dingledooper.
i=8
while[print(f'{int("1"*(9-abs(i)))**2:^17}')]:i-=1
Python 3, 57 bytes
saved 1 byte by using f-Strings thanks to dingledooper.
i=8
while i+9:print(f'{int("1"*(9-abs(i)))**2:^17}');i-=1
Python 3, 58 bytes
Even shorter solution by a different approach (prints one extra space at the beginning of each line).
i=8
while i+9:j=abs(i);i-=1;print(' '*j,int('1'*(9-j))**2)
Python 3, 61 bytes (without extra spaces)
i=8
while i+9:j=9-abs(i);i-=1;print(f'%{8+j}d'%int('1'*j)**2)
These are my previous solutions:
Python 3, 66 bytes
f=lambda c:f'%{8+c}d'%int('1'*c)**2
for c in f(9):print(f(int(c)))
Python 3, 71 bytes:
f=lambda c:str(int('1'*int(c))**2)
for c in f(9):print(f(c).center(17))
JavaScript (V8), 75 bytes
for(b of x="12345678987654321")print(b-9?"".padEnd(9-b)+"1".repeat(b)**2:x)
Doing the calculations 1**2, 11**2, 111**2, ...
This solution fixes the precision problem. It correctly produces a 1 at the end of the longest line instead of 0. I think, this one is the clearest of the short solutions for JavaScript.
Instead of x="12345678987654321" you could also write x=111111111**2/10+"1" which is equally short.
Jelly, 22 18 12 11 bytes
9ŒḄr1z⁶ṚŒḄY
Done alongside caird coinheringaahing in chat.
How it works
9ŒḄr1z⁶ṚŒḄY - Full program.
9ŒḄ - The list [1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1].
r1 - Generate [N, N-1, ..., 1] for each N in ^.
z⁶ - Zip; transpose ^ with filler spaces.
Ṛ - Reverse.
ŒḄ - Palindromize without vectorization.
Y - Join by newlines.
Saved 6 bytes thanks to Leaky Nun!
JavaScript (V8), 75 bytes
for(a=9;b=--a>0?9-a:9+a;print(s))for(s='',c=9;--c+b;)s+=c<0?b+c:c<b?b-c:' '
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
Zsh, 91 61 bytes
eval s+={1..9}';<<<${(l:8:)s%?}`rev<<<$s`;'>f;{<f;tac f}|uniq
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
```
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);}}
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("");}}
This code always chooses whether to print a space or a number, depending on the values of i and j.
MathGolf, 11 bytes
9╒ñmÆ╒ñyFΩn
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)
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);}}
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);
}
}
StupidStackLanguage, 468 bytes
avdqvdmffffffffqvvviifavvfblffffffflfifdflavvfbfffffflfififdfdflavvfbffffflfifififdfdfdflavvfbfffflfififififdfdfdfdflavvfbffflfifififififdfdfdfdfdflavvfbfflfififififififdfdfdfdfdfdflavvfbflfifififififififdfdfdfdfdfdfdfavvfbfififififififififdfdfdfdfdfdfdfdflavvfbflfifififififififdfdfdfdfdfdfdflavvfbfflfififififififdfdfdfdfdfdflavvfbffflfifififififdfdfdfdfdflavvfbfffflfififififdfdfdfdflavvfbffffflfifififdfdfdflavvfbfffffflfififdfdflavvfbffffffflfifdflavvfbfffffffflf
Explanation
Super simple, we just create a space and the number 1, then every line just increment and print then decrement and print
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]))
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;}}
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);
}
}
}
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))))
Shortened from 127 to 121 to 115.
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
Clojure, 160 bytes
(run! #(println(reduce str "" %))(map (fn[i](map #(let[x(- (if(> % 9)(- 9(- % 9))%)(- 9 i))](if(pos? x)x " "))(range 1 18)))(concat(range 1 10)(range 8 0 -1))))
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`
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.
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.
///, 139 bytes
/~/\/\///;/3&~|/*#~*/5432~&/2^~^/#!~%/$56~$/1234~#/1
~@/ ~!/@@/!!^@ 1&@12; $;$|@ %|@%76| %7876|%789876| %7876|@%76|@ %54;$54; $;@12;@ 1&!#
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.
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));
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.
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`
`
APL (Dyalog Classic), 20 19 bytes
(⍉⊢⍪1↓⊖)⍣2⌽↑,⍨\1↓⎕d
⎕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↓)
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
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!
brainfuck, 158 154 bytes
++++++++[->+>++++>+>++++++<<<<]>>>++<<+[-[-<+>>.<]<[->+<]>>>>+>[->+<<.+>]>+[-<+<.->>]<<<.<<]>>>>-[<<<<+[-<+>>.<]<[->+<]>>>>>[->+<<+.>]>-[-<+<-.>>]<<-<.>>]
[spaceMem, spaceCount, space, lf, "0", numCount, numMem]
++++++++[->+>++++>+>++++++<<<<]>>>++<<+
[ for spaceCount
-[- for spaceCount minus 1
<+ inc spaceMem
>>. print space
< go to spaceCount
]
<[->+<]> fetch spaceCount from spaceMem
>>>+ inc number
>[- for numCount
>+ inc numMem
<<.+ print and inc number
> go to numCount
]
>+[- for numMem plus 1
<+ inc numCount
<.- print and decrement number
>> go to numMem
]
<<<. print lf
<< go to spaceCount
]
>>>>-[ for numCount
<<<<+[- for spaceCount plus 1
<+ inc spaceMem
>>. print space
< go to spaceCount
]
<[->+<]> fetch spaceCount from spaceMem
>>> inc number
>[- for numCount
>+ inc numMem
<<+. print and inc number
> go to numCount
]
>-[- for numMem minus 1
<+ inc numCount
<-. print and decrement number
>> go to numMem
]
<<- decrement number
<. print lf
>> go to numCount
]
Canvas, 7 bytes
9{R⇵]↶┼
Explanation:
9{R⇵]↶┼
9{ ] map over i = 1..9
R push a range 1..i
⇵ and reverse that array
in Canvas, an array of arrays is a list of lines,
and the inner arrays are joined together
↶ rotate anti-clockwise
┼ and quad-palindromize with 1 overlap
I've just fixed a couple built-ins to make a 6 byte version possible:
9{R]/┼
9{R]/┼
9{ ] map over 1..9
R create a range 1..i
/ pad with a diagonal of spaces
┼ and quad-palindromize
Japt -R, 11 bytes
9õ_õ ¬êÃê û
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.
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')}}
T-SQL, 116 115 bytes
DECLARE @ INT=8,@d INT=-1a:PRINT SPACE(@)+STUFF('12345678987654321',9-@,2*@,'')IF @=0SET @d=1SET @+=@d If @<9GOTO a
Pure procedural counter and loop, not very "SQL"-like, but the best I could come up with using what SQL offers. Formatted:
DECLARE @ INT=8, @d INT=-1
a:
PRINT SPACE(@)+STUFF('12345678987654321',9-@,2*@,'')
IF @=0 SET @d=1
SET @+=@d
If @<9 GOTO a
Cuts a length out of a hard-coded string using STUFF. Would work just as easily with any other set of characters.
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 :)
Attache, 49 bytes
Print@Join=>Table[{If[_%9=_,9-_,sp]}@`+,Abs!-8:8]
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.
///, 124 bytes
/(/# '87#//'/"67//&/123//%/43!//$/ //#/654321
//"/&45//!/21
$ /$$$$1
$$$ 1!$ &!$&% "%"#$'('8987($'65%"65% "%$&%$ &!$$1!$$ 1
Shorter than this answer by 109 bytes! Just applies a bunch of substitutions with whitespace and common numbers.
Python 2, 72 bytes
z="123456789"
for i in range(9)+range(8)[::-1]:print"% 8s"%z[:i]+z[i::-1]
///, 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
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
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
Recursiva, 31 22 bytes
{pB9'P+*" "- 9}J""WpB}
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]
Gaia, 9 bytes
9┅…ṫ¦€|ṫṣ
Explanation
9┅ Push [1 2 3 4 5 6 7 8 9]
… Prefixes; push [[1] [1 2] [1 2 3] ... [1 2 3 4 5 6 7 8 9]]
ṫ¦ Palindromize each row: e.g. [1 2 3 4] -> [1 2 3 4 3 2 1]
€| Centre-align the rows, padding with spaces
ṫ Palindromize the rows
ṣ Join with newlines
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
$xin 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.

PHP, 99 91 81 byte
for($y=17;$y--;print"
")for($x=17;$x--;)echo($d=9-abs($x-8)-abs($y-8))>0?$d:" ";
run in command line
php -r "CODE_ABOVE"
18 bytes saved by Jörg Hülsermann
Octave, 38 bytes
x=abs(-8:8);m=x+x';m(m>8)=25;[57-m,'']
To make it work in MATLAB too, you'd need to write x=ndgrid(abs(-8:8));m=x+x';m(m>8)=25;[57-m,''] or x=meshgrid(abs(-8:8));m=x+x';m(m>8)=25;[57-m,'']
Matlab 227 223 221 209 208 bytes
z=@cellfun;
C=z(@(a,b,c)[a b c fliplr([a b])],[mat2cell(repelem(' ',36),1,8:-1:1),{''}],[{''},mat2cell(nonzeros(tril(repmat(49:56,8,1))')',1,1:8)],num2cell(49:57),'un',0)';
z(@(c)disp(c),[C;flipud(C(1:end-1))])
Deadfish, 1446 bytes
iisiisddddooooooooiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddoddddsddddoooooooiiiiiiiiiiiiiiiiioiododddddddddddddddddddddddddddddddddddddddoddddsddddooooooiiiiiiiiiiiiiiiiioioiodododddddddddddddddddddddddddddddddddddddddoddddsddddoooooiiiiiiiiiiiiiiiiioioioiododododddddddddddddddddddddddddddddddddddddddoddddsddddooooiiiiiiiiiiiiiiiiioioioioiodododododddddddddddddddddddddddddddddddddddddddoddddsddddoooiiiiiiiiiiiiiiiiioioioioioiododododododddddddddddddddddddddddddddddddddddddddoddddsddddooiiiiiiiiiiiiiiiiioioioioioioiodododododododddddddddddddddddddddddddddddddddddddddoddddsddddoiiiiiiiiiiiiiiiiioioioioioioioiododododododododddddddddddddddddddddddddddddddddddddddodddsoioioioioioioioiodododododododododddddddddddddddddddddddddddddddddddddddoddddsddddoiiiiiiiiiiiiiiiiioioioioioioioiododododododododddddddddddddddddddddddddddddddddddddddoddddsddddooiiiiiiiiiiiiiiiiioioioioioioiodododododododddddddddddddddddddddddddddddddddddddddoddddsddddoooiiiiiiiiiiiiiiiiioioioioioiododododododddddddddddddddddddddddddddddddddddddddoddddsddddooooiiiiiiiiiiiiiiiiioioioioiodododododddddddddddddddddddddddddddddddddddddddoddddsddddoooooiiiiiiiiiiiiiiiiioioioiododododddddddddddddddddddddddddddddddddddddddoddddsddddooooooiiiiiiiiiiiiiiiiioioiodododddddddddddddddddddddddddddddddddddddddoddddsddddoooooooiiiiiiiiiiiiiiiiioiododddddddddddddddddddddddddddddddddddddddoddddsddddooooooooiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddo
A more human friendly spaced version:
iisiisdddd oooooooo iiiiiiiiiiiiiiiii o dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooooooo iiiiiiiiiiiiiiiii oiodo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oooooo iiiiiiiiiiiiiiiii oioiododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooooo iiiiiiiiiiiiiiiii oioioiodododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oooo iiiiiiiiiiiiiiiii oioioioiododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooo iiiiiiiiiiiiiiiii oioioioioiodododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oo iiiiiiiiiiiiiiiii oioioioioioiododododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd o iiiiiiiiiiiiiiiii oioioioioioioiodododododododo dddddddddddddddddddddddddddddddddddddddo
ddds oioioioioioioioiododododododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd o iiiiiiiiiiiiiiiii oioioioioioioiodododododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oo iiiiiiiiiiiiiiiii oioioioioioiododododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooo iiiiiiiiiiiiiiiii oioioioioiodododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oooo iiiiiiiiiiiiiiiii oioioioiododododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooooo iiiiiiiiiiiiiiiii oioioiodododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oooooo iiiiiiiiiiiiiiiii oioiododo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd ooooooo iiiiiiiiiiiiiiiii oiodo dddddddddddddddddddddddddddddddddddddddo
ddddsdddd oooooooo iiiiiiiiiiiiiiiii o dddddddddddddddddddddddddddddddddddddddo
///, 233 bytes
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
1234321
12321
121
1
Yay.
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>");
Groovy, 62, 57 chars
((1..9)+(8..1)).any{println' '*(9-it)+('1'*it as int)**2}
old version:
((1..9)+(8..1)).any{println"${('1'*it as int)**2}".center(17)}
explanation: we create a list [1,2,...,9,8,7,..,1]. Within the closure we create strings '1', '11', '111,..., convert them to numbers, run power of two and center.
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:]
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.
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.
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.
Ruby, 55
puts (-8..8).map{|i|[?\s*a=i.abs,(?1*(9-a)).to_i**2]*''}
Output:
irb(main):342:0> puts (-8..8).map{|i|[?\s*a=i.abs,(?1*(9-a)).to_i**2]*''}
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
1234321
12321
121
1
Mathematica 83 49 43 54 51
Print@@#&/@(Sum[k~DiamondMatrix~17,{k,0,8}]/.0->" ")

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}

Ruby 59
(-8..8).map{|i|puts' '*i.abs+"#{eval [?1*(9-i.abs)]*2*?*}"}
