# Draw a big slash X

Given a non-negative integer, print out an X that is that big. X is the output for input 0, and you will add slashes equal to the input in each direction to extend the X for larger inputs.

# Test Cases

0

X

1

\ /
X
/ \

2

\   /
\ /
X
/ \
/   \

...

10

\                   /
\                 /
\               /
\             /
\           /
\         /
\       /
\     /
\   /
\ /
X
/ \
/   \
/     \
/       \
/         \
/           \
/             \
/               \
/                 \
/                   \

# Rules

You may either print the output, or return a string or list of string from a function. A trailing newline, as well as extra interior whitespace that does not affect appear, is allowed.

This is , so shortest answer in bytes wins!

• Jun 8 '17 at 18:16
• ... I'm just gonna wait for the charcoal 1-byte solution. Jun 8 '17 at 18:16
• @LeakyNun I'd be surprised if Charcoal can easily deal with the X in the middle (part of why I added it) but who knows :D Jun 8 '17 at 18:17
• Also related Jun 8 '17 at 20:02
• – xnor
Jun 8 '17 at 20:05

# Charcoal, 6 bytes

ＰX⁺¹ＮX

Your nonsense ain't stopping me ;)

Try it online!

• The TIO looks... interesting when you put a really big number into it Jun 8 '17 at 18:28
• @StephenS Only because of wrapping.
– Okx
Jun 8 '17 at 18:28
• I know, it's just intuitive that all of the wrapped \s get left aligned (because spaces don't get wrapped) Jun 8 '17 at 18:29
• 5 chars (postdates challenge) just to get that little bit closer to Canvas Apr 6 '18 at 7:10

## JavaScript (ES6), 79 bytes

Uses a recursive function g that walks through a grid and builds the output character by character.

n=>(g=x=>/\\ X
[~x?x-y?x+y-w&&2:x-n?1:3:4]+(~y?g(~x--?x:y--&&w):''))(y=w=n*2)

### How?

Both variables x and y iterate from 2n to -1, where n is the input.

For each position (x, y) in the grid, we pick one of these characters:

• 0: /
• 1: \
• 2: space
• 3: X
• 4: newline

using the following tests:

• ~x: Falsy if x == -1: we've reached an end of line.
• x-y: Falsy if x == y: we're located on the anti-diagonal.
• x+y-w: Falsy if x + y == w: we're located on the diagonal.
• x-n: Falsy if x == n: because this test is only performed when x == y, this means that we're located in the exact center of the grid.

and the following decision tree:

### Demo

let f =

n=>(g=x=>/\\ X
[~x?x-y?x+y-w&&2:x-n?1:3:4]+(~y?g(~x--?x:y--&&w):''))(y=w=n*2)

console.log(f(0))
console.log(f(1))
console.log(f(4))

• This is... JavaScript? What has happened to you, old friend. Jun 10 '17 at 7:17

# MATL, 16 bytes

'\/X 'iEQXytEP+)

Try it online!

Consider input 2 as an example. The stack is shown here upside down, i.e. lower elements are the ones most recently pushed.

'\/X '  % Push this string
%   STACK: '\/X '
iEQ     % Input a number, n. Multiply by 2, add 1: gives 2*n+1
%   STACK: '\/X '
5
Xy      % Identity matrix of that size
%   STACK: '\/X '
[1 0 0 0 0;
0 1 0 0 0;
0 0 1 0 0;
0 0 0 1 0;
0 0 0 0 1]
tEP     % Duplicate, multiply each entry by 2, flip vertically
%   STACK: '\/X '
[1 0 0 0 0;
0 1 0 0 0;
0 0 1 0 0;
0 0 0 1 0;
0 0 0 0 1]
[0 0 0 0 2;
0 0 0 2 0;
0 0 2 0 0;
0 2 0 0 0;
2 0 0 0 0]
+       % Add the two matrices
%   STACK: '\/X '
[1 0 0 0 2;
0 1 0 2 0;
0 0 3 0 0;
0 2 0 1 0;
2 0 0 0 1]
)       % Index into the string. Indexing is 1-based and modular, so 1 picks
% the first character ('\'), ..., 0 picks the last (space)
%   STACK: ['\   /';
' \ / ';
'  X  ';
' / \ ';
'/   \']
% Implicit display
• I wanted to do something similar in Octave, but you beat me to it and in MATL it is even shorter than Octave, so great work! Jun 9 '17 at 8:48
• @Michthan Thanks! An Octave version would definitely be worth posting too. My atempt is at 38 bytes, what's yours? Jun 9 '17 at 11:50
• I have been thinking about it all weekend and couldn't find a more efficient way than the one you are using here.. So all credits should go to you for an octave version. Jun 12 '17 at 7:12

# Canvas, 3 bytes

╵＼┼

Try it here!

half of the size of the Charcoal answer :D

╵    increment the input
\   create a diagonal that long
┼  and quad-palindromize, mirroring what's required, with 1 overlap;
This overlaps the / and \, resulting in X
• Dang that is short. Apr 5 '18 at 20:55
• :||||||| +1 for golfiness Apr 6 '18 at 5:51
• also i wonder if stack-based languages basically always outgolf other languages? Apr 6 '18 at 7:59
• @ASCII-only jelly
– Okx
Apr 6 '18 at 8:19
• @Okx *stack-based/tacit Apr 6 '18 at 8:38

# C, 108106 104 bytes

a;g(n){for(int b=2*n,i=1,c=47;a+=i;!b?i=-i,c=92:puts(""),b-=2*i)printf("%*c%*c",a,b?c^115:88,b,b?c:10);}

Try it online!

(−2 golfing thanks to MD XF)

(−1 golfing thanks to ceilingcat)

It prints two characters (at first, c = 47, which is a slash, and c + 45, which is a backslash; then they are swapped) with a dynamic field width.

The field widths start at 1 and 2n, and at each iteration, the first width is incremented by 1, and the second one is decremented by 2.

When the second field width becomes 0, it outputs 'X' and a newline instead of the regular characters, and reverses the direction of increments (i). A newline is printed for all other lines separately (puts("")).

• 106 bytes: a,b,i=1,c;g(n){for(b=2*n,c=47;a+=i;b?puts(""):(i=-i,c=92),b-=2*i)printf("%*c%*c",a,b?c+45*i:88,b,b?c:10);} Try it online! Jun 10 '17 at 2:56
• Thanks for the idea! I only used a part of it to make sure the code can be called twice. Jun 10 '17 at 14:49
• Thanks for the suggestion. Removing unneeded parentheses is always great! May 20 '20 at 9:56

# shortC, 111 bytes

s(x){Wx--)R" ")}j;f(x){O;j<x;j++)s(j),P92),s((x-j)*2-1),R"/\n");s(x);R"X\n");Wj--)s(j),P47),s((x-j)*2-1),R"\\\n

Based on my C answer. Conversions:

• R -> printf(
• P -> putchar(
• W -> while(
• O -> for(
• Auto-inserted closing ");}

This also uses ASCII codes for \ and /.

Try it online!

# Python 2, 81 bytes

r=range(2*input()+1)
for i in r:print''.join(' \/X'[i==j::2][r[~i]==j]for j in r)

Try it online!

# C, 168155 150 bytes

-5 thanks to Computronium

#define p printf(
s(x){while(x--)p" ");}j;f(x){for(;j<x;j++)s(j),p"\\"),s((x-j)*2-1),p"/\n");s(x);p"X\n");while(j--)s(j),p"/"),s((x-j)*2-1),p"\\\n");}

Can certainly be golfed; I'm doing so. Try it online!

Ungolfed:

int space(int x)
{
while (x--)
putchar(' ');
}

int f(int x)
{
for (int j = 0; j < x; j++) {
space(j);
printf("\\");
space((x-j)*2-1);
printf("/\n");
}

space(x);
puts("X");

while (j--) {
space(j);
putchar('/');
space((x-j)*2-1);
printf("\\\n");
}
}
• You can lose 5 characters by defining p to be "printf(" instead of "printf". Jun 9 '17 at 10:17

# V, 21 bytes

éXÀñ>HÄÒ r\Á/YGpr/$r\ Try it online! Hexdump: 00000000: e958 c0f1 3e48 c4d2 2072 5cc1 2f59 4770 .X..>H.. r\./YGp 00000010: 722f 2472 5c r/$r\

Explanation:

éX                      " Insert an 'X'
Àñ                    " Arg1 times:
>H                  "   Add a space to every line.
"   Conveniently, this also puts us on the first line
Ä                 "   Duplicate this line
Ò                "   And replace the whole line with spaces
r\             "   Replace the first char with '\'
Á/           "   Append a '/' char
Y          "   Yank this line
G         "   Move to the last line
p        "   And paste the line we yanked
r/      "   Replace the first character with a '/'
$" Move to the end of the line r\ " And replace the last character with a '\' Essentially, we have Insert an X, n times extend the slashes. But it's not quite that simple because we also have to add the slashes the first time. If the slashes were already there, we could write extend the slashes as: >HÄX2pGÙX2p Which would save us 6 bytes. # C#, 157122 120 bytes _=d=>"".PadLeft(d) a=>{var s=_(a)+"X\n";for(int i=0;++i<=a;)s=$@"{_(a-i)}\{_(i*2-1)}/
{s+_(a-i)}/{_(i*2-1)}\
";return s;}

Ungolfed version:

Func<int, string> _ = (d) =>"".PadLeft(d);
Func<int, string> func = a => {

var s = _(a) + "X\n";

for (int i = 0; ++i <= a;) {

s = $@"{_(a - i)}\{_(i * 2 - 1)}/ {s + _(a - i)}/{_(i * 2 - 1)}\ "; } return s; }; • You can move the other anonymous function out of the first and then include it as _=d=>new string(' ',d); (note no need for the braces around d). You can remove the curly braces around the for loop. Make use of a verbatim string with so you don't need to escape all of the backslashes. If you set i=0 then you can do ++i<a+1 and remove the i++. Jun 9 '17 at 8:41 • Also with verbatim strings you don't need to include \n an actual line feed will work, though I'm not sure if you will then be able to remove the braces around the for loop, you would have to try. Jun 9 '17 at 8:42 • After applying the first suggestion by TheLethalCoder, you can also replace the ++i<a+1 for ++i<=a EDIT You can also save 4 more bytes by changing the Func from new string(' ',d) to "".PadLeft(d) Jun 9 '17 at 8:54 • Thanks for suggestions, @TheLethalCoder am I allowed to declare 2 functions like that, wouldn't that make a lot of C# golfing shorter if we can do that? Jun 9 '17 at 9:22 • @StefanDelport There's a meta discussion about it somewhere but as it stands I believe you can as long as you show the functions name. In this case _. Jun 9 '17 at 9:36 ## Mathematica, 71 bytes (Partially inspired by Jenny_mathy's 104-byte solution) ""<>#&/@(#"\\"+Reverse@#"/"&@IdentityMatrix[2#+1]/.{0->" ",a_+_->"X"})& Returns a list of strings. Explanation: IdentityMatrix[2#+1] makes a matrix of the right size with 1s along the diagonal and 0s elsewhere. Next, we multiply it by "\\" (an escaped backslash), which makes it a matrix with backslashes along the diagonal and 0s elsewhere, since of course 1 times backslash is backslash and 0 times backslash is 0. We add this to "/" times its reverse to make the X shape. We're nearly done, except there are still 0s everywhere, and the middle is "\\" + "/". We fix these two things by substituting " " for 0 and "X" for a_+_, which matches any sum of two things (like _+_ should, except Mathematica is too clever for its own good and interprets that as 2 times _). Finally, ""<>#&/@ turns this into a list of strings. # Java (OpenJDK 8), 135 bytes i->{int k=0,j,l=2*i+1;String[]s=new String[l];for(;k<l;k++)for(s[k]="",j=0;j<l;j++)s[k]+=j==k?j==i?"X":"\\":j==l-1-k?"/":" ";return s;} Lambda expression that takes and integer and returns an array of Strings Try it online! Ungolfed: i->{ int k=0,j,l=2*i+1; // Some variables to be used String[]s=new String[l]; // Return array (size 2*i+1) for(;k<l;k++) // For each array entry for(s[k]="",j=0;j<l;j++) // Set each character to s[k]+=j==k?j==i?"X":"\\" // \ or X if it's the jth character of the jth row :j==l-1-k?"/" // / if it's the opposite char :" "; // else blank return s; } # T-SQL, 201 bytes DECLARE @ INT SELECT @=a FROM t DECLARE @i INT=@ WHILE @>0BEGIN PRINT SPACE(@i-@)+'\'+SPACE(2*@-1)+'/'SET @-=1 END PRINT SPACE(@i)+'X'WHILE @<@i BEGIN SET @+=1 PRINT SPACE(@i-@)+'/'+SPACE(2*@-1)+'\'END Formatted: DECLARE @ INT SELECT @=a FROM t DECLARE @i INT=@ WHILE @>0 BEGIN PRINT SPACE(@i-@)+'\'+SPACE(2*@-1)+'/' SET @-=1 END PRINT SPACE(@i)+'X' WHILE @<@i BEGIN SET @+=1 PRINT SPACE(@i-@)+'/'+SPACE(2*@-1)+'\' END Input is via column a in named table t, per our guidelines. # Ruby, 66 bytes Recursive function. f=->x{?X[x]||"\\#{s=' '*(2*x-1)}/ #{f[x-1].gsub /^/,' '} /#{s}\\"} Try it online! ### Explanation f=->x{ # Start of recursive function named f ?X[x]|| # Return 'X' if x==0, otherwise the following: "\\#{s=' '*(2x-1)}/ # Top edge of the cross. Save no. of spaces needed #{f[x-1] # Get result of f[x-1] .gsub /^/,' '} # Regex sub to left-pad every line w/ a space /#{s}\\" # Bottom edge of cross (insert saved no. of spaces) # Jelly, 24 17 bytes Ḥ‘=þµḤ+Uị“/\x ”Y Try it online! How it Works Ḥ‘=þµḤ+Uị“/\x ”Y main link, input a Ḥ‘ input doubled and incremented þ Make a table: Apply = "equals"/ to  each element in range(2a+1) cartesian multiplied with itself. eg. For input 1: [1=1,1=2,1=3],[2=1,2=2,2=3],[3=1,3=2,3=3] µ on this array: + add: Ḥ double of it to U its reverse (changes south-east to north-west) ị“/\x ” index into the string "/\x " to get the right characters Y join by newlines for the final output. -6 bytes thanks to @LeakyNun and -1 byte with an additional improvement • 18 bytes Jun 13 '17 at 14:05 # ///, 223 bytes /|/\\\\//i///& / &// &///> /^b|b^v|v^^$|+^
f|fvv&i|||^$|-^^$|-^$|+^^<|$^<-|$>|->vv&i|||| ^^<-|$^<|$^^f|f| ^ f|f^^>|->| ^ >|->^^f|fv|v^||||^^>|->v|v^|||^^^v|v ^ b|b^>//^/\///b\/>/b\/\/$|+\/\/\/<|$\/\///>$+//>i$+ iX/<$//

Try it online!

# Retina, 74 bytes

.+
$* X +^ ( *).( *)$1\  $2/¶$&
+¶ ( *).( *).?&¶$1/$2\
m (\W)1

Try it online! Explanation:

.+
$* X Place the X. +^ ( *).( *)$1\  $2/¶$&

Starting at the X, working upwards, place a \ diagonally to the left each time. Also place a / two more spaces after the / than last time.

+¶ ( *).( *).?&¶$1/$2\

Starting at the X, working downwards, place a / diagonally to the left each time. Also place a \ two more spaces after the / than last time.

m (\W)1

The number of spaces between the two diagonals needs to be odd, so the last space on each line (except the original X line) is deleted.

# Mathematica, 131 bytes

(F[x_,y_]:=Table[x<>StringJoin@Table[" ",i]<>y,{i,1,#*2,2}];Column[Join[Reverse@F["\\","/"],{"X"},F["/","\\"]],Alignment->Center])&

# Mathematica, 104 bytes

here is another approach using Grid

(S=DiagonalMatrix[Table["\\",r=2#+1]];Table[S[[r+1-i,0+i]]="/",{i,r}];S[[#+1,#+1]]="X";Grid@S/. 0->" ")&

# Bash, 138 bytes

for i in seq $1;seq$[$1-1] -1 1 {$[a++]
printf "%*c%*s\n" echo ' '$i$[a>$1?1:2]$[($1-i)*2]' '$[a<$1?1:2] }|sed 's/22/X/'|tr 12 /\\ Try it online! Really long, bash heates '\ and /' Less golfed for i in {1..10} {9..1};{$[a++];                      #argument as padding, prints 1 for \ and 2 for /

# QBIC, 90 bytes

~b=0|?@X\[0,:-1|X=space$(a)┘Z=Z+X+@\+space$((b-a)*2-1)+@/+X+@┘`]Z=Z+space$(b)+A+_fZ How this monstrosity works, is left as an excercise for the reader... Sample output: Command line: 3 \ / \ / \ / X / \ / \ / \ ## Visual Basic.Net, 454 450 Bytes Option Strict Off module m sub main(a As String()) dim v=Convert.toInt32(a(0)) for i as Integer=v to 1 step -1 for j as Object=1 to v-i w(" ") next w("\") for j as Object=1 to i*2-1 w(" ") next console.writeline("/") next console.writeline(new String(" ",v)&"X") for i as Object=1 to v for j as Object=1 to v-i w(" ") next w("/") for j as Object=1 to i*2-1 w(" ") next console.writeline("\") next end sub sub w(s) console.write(s) end Sub end module not sure whether making a func for writeline will save some bytes thanks to Stephen S for pointing at as ... removal also changed integer into object last edit changed the first one back • You're a brave one :) I believe you can remove all of the as Types, because if you have Option Strict Off, VB.NET acts like a loosely typed language. Jun 8 '17 at 21:25 • Currently on Linux mono is anoying Jun 8 '17 at 21:27 • mono doesn't cares about Option Strict, or at least thats what I think Jun 8 '17 at 21:29 • Oh it seemed functions != for loops thanks for the golf Jun 8 '17 at 21:36 • Isn't Option Strict off by default? It is in Visual Studio at least Jun 8 '17 at 22:25 # 05AB1E, 22 bytes F'\IN-úR.∞})Âí'Xs)˜.c» Try it online! Explanation F # for N in [0...input-1] do '\ # push a backslash IN-ú # prepend input-N spaces R # reverse .∞ # mirror } # end loop ) # wrap stack in a list Â # bifurcate í # reverse each item 'Xs # push an "X" between the 2 lists on the stack )˜ # wrap in flattened list .c # pad lines to equal length » # join on newlines Alternative 22 byte solution F'\N·>ú'/ì})Âí'X¸«ì.c» ## Pyke, 14 bytes \XQV.X \ /\/ Try it here! \X - "X" QV - repeat input times: .X \ /\/ - surround(^, all=" ", tl="\", left=" ", right=" ", lower=" ", tr="/", br="\", bl="/") # tcl, 134 proc P {x s b} {time {puts [format %[incr ::i$x]s%[expr ($::n-$::i)*2+2]s $s$b]} \$::n}
P 1 \\ /
puts [format %[incr i]s X]
P -1 / \\

## demo

Set n on the first line.

May be I can golf it more using a recursive approach