# How many Wazirs can be placed on an N×N Chessboard?

Suppose a new fairy chess piece named the Wazir is introduced to chess. Wazirs can move from a position (x, y) to:
(x+1, y)
(x, y+1)
(x-1, y)
(x, y-1)

That is, they move orthogonally like the rook, but only one step at a time like the king. How many such wazirs can be placed on an N×N chessboard so that no two wazirs can attack each other?

On a 1×1 board, there can be only 1 such piece.
On a 2×2 board, there can be 2 such pieces.
On a 3×3 board, there can be 5 such pieces.

Given N, return the number of wazirs that can be placed on an N×N chessboard.

This is OEIS sequence A000982.

### More test cases

725

832

1005000

• So the Camel is to the Rook what the King is to the Queen? I.e. can only move orthogonally, and only one step at a time.
Oct 2 '17 at 8:32
• @SashaR May I rewrite your question as a proper code golf challenge?
Oct 2 '17 at 9:09
• As a new user of this site, you've been very lucky this time. A lot of (off-topic) programming questions on this site has been permanently closed and downvoted, not edited as a challenge and upvoted like this one. As other people already explained, this site is for programming competitions only, not for asking homework. You can use the sandbox (at codegolf.meta.stackexchange.com/questions/2140/… ) before posting a challenge to avoid common mistakes next time; and note that most users on this site, as you have seen, use "unreadable" languages. Oct 2 '17 at 14:52
• This question is pretty confusing in that the Camel is already the standard fairy chess name for a piece like a knight that makes longer jumps and the piece you describe already has a fairy chess name: Wazir. Oct 3 '17 at 11:26
• OTOH editing the question to the the standard name after answers have already been written using the name used in the question is also confusing. Oct 4 '17 at 8:40

# Whitespace, 45 bytes





Try it online!

By the way, here is a proof that the ⌈n²/2⌉ formula is correct.

• We can always place at least ⌈n²/2⌉ wazirs: just lay them out in a checkerboard pattern! Assuming the top-left tile is white, there are ⌈n²/2⌉ white tiles and ⌊n²/2⌋ black tiles on the n × n board. And if we place wazirs on the white tiles, no two of them are attacking each other, as every wazir only “sees” black tiles.

Here’s how we place 13 wazirs on a 5 × 5 board (each W is a wazir).

• We can’t do any better: let’s arbitrarily tile the checkerboard with 2 × 1 domino pieces, optionally using a 1 × 1 piece for the final corner of an odd-length chessboard, like so:

We need ⌈n²/2⌉ dominoes to cover the chessboard. Clearly, putting two wazirs on one domino makes it so that they can attack one another! So each domino can only contain at most one wazir, meaning we can’t possibly place more than ⌈n²/2⌉ wazirs on the board.

• You don't need the pigeonhole principle for the last part: you have exactly ⌈n²/2⌉ tiles, and at most camel per tile, so you have at most ⌈n²/2⌉ camels. Oct 3 '17 at 7:29
• @ShreevatsaR What ensures you that you can't put x > ⌈n²/2⌉ camels in ⌈n²/2⌉ tiles? It's the pigeonhole principle... Oct 3 '17 at 7:58
• I thought at first the code didn't load, so I refreshed the page, and it still didn't. Then I realized what language name was written at the top. Oct 4 '17 at 9:28
• I appreciate that you went and changed your C's to W's in your proof illustration. Oct 4 '17 at 13:39
• I also appreciate that the W's are all on the WHITE SPACES with an answer in WHITESPACE. Oct 4 '17 at 16:13

# Oasis, 3 bytes

k>v


Try it online!

square - increment - integer halve

# Prolog (SWI), 22 19 bytes

Saved 3 bytes thanks to Kevin Cruijssen

X*Y:-Y is(X*X+1)/2.


Try it online!

• X*Y:-Y is(X*X+1)/2. (19 bytes) Oct 2 '17 at 13:43
• @KevinCruijssen: Doh! I suggested the same change to another user, but somehow I didn't take my own advice. Thank you :) Oct 2 '17 at 14:06

# APL (Dyalog), 97 6 bytes

Now uses Mr. Xcoder's formula.

This is an anonymous prefix tacit function which takes N as argument.

⌈2÷⍨×⍨


Try it online!

×⍨ square N (lit. multiplication selfie, i.e. multiply by self)

2÷⍨ divide by 2

⌈ ceiling (round up)

• Wow!I have no idea how you did this !!Didn't get the logic thoughsigh Oct 2 '17 at 8:49
• Darn, someone already found out the pattern. Oct 2 '17 at 18:26
• Huh, just realized the formula is on the OEIS page. Probably shouldn't have linked that. Oct 2 '17 at 18:27

# JS (ES6) / C# polyglot, 11 bytes

n=>n*n+1>>1


### Test cases

let f =

n=>n*n+1>>1

console.log(f(7))
console.log(f(8))
console.log(f(100))

• C# polyglot Oct 2 '17 at 16:41
• @Shaggy Thanks for noticing! Oct 2 '17 at 16:49
• JS and C# polyglot seems extremely rare, given the differences between them. +1 Oct 4 '17 at 11:36

# dc, 6 bytes

2^2~+p


2^: square; 2~: divide by 2, pushing the quotient then the remainder; +p: add the remainder to the quotient & print.

Try it online!

# 05AB1E, 3 bytes

Straight implementation of the formula given by A000982

n;î


Try it online!

Explanation

n      # square
;     # divide by 2
î    # round up

• *;î works too May 10 at 21:45

# Python 2, 17 bytes

1 byte thanks to @ovs.

lambda x:x*x+1>>1


Try it online!

• Alternatively: lambda x:-~x**2/2 Oct 2 '17 at 23:46

# C (gcc), 2318 17 bytes

• Saved a byte thanks to Tahg; golfing n/2+n%2 to n+1>>1.
f(n){n=n*n+1>>1;}


Try it online!

# C (gcc), 22 bytes (not using undefined behavior)

f(n){return n*n+1>>1;}


Try it online!

Some people really do not like exploiting a certain compiler's undefined bahavior when using specific compiler flags. Doing so does save bytes, though.

• Odd way of providing an answer IMO, but: f(n){n=n*n+1>>1;} to save a byte.
– Tahg
Oct 2 '17 at 9:56
• @Tahg Thanks; though in what way do you find my way of providing an answer odd? Oct 2 '17 at 10:51
• I didn't think changing the input argument was a normal way of returning a value in C.
– Tahg
Oct 2 '17 at 13:41
• @YSC Yet in the compiler's opinion it is understandable and creates an executable which works. Oct 3 '17 at 15:10
• @YSC We believe here on PPCG that, if the program works on one interpreter, it is a valid submission. It works on an online interpreter, it is therefore valid without any further remarks. Oct 3 '17 at 22:37

# Brachylog, 6 bytes

^₂/₂⌉₁


Try it online!

# Python 3, 19 bytes

lambda x:-(-x*x//2)


Try it online!

lambda x:-(-x*x//2)  # Unnamed function
lambda x:            # Given input x:
x*x      # Square
-         # Negate
//2   # Halve and Floor (equivalent of Ceil)
-(       )  # Negate again (floor -> ceil)


-1 byte thanks to Mr. Xcoder

• x**2 -> x*x Oct 2 '17 at 13:00
• @Mr.Xcoder Facepalm thanks Oct 2 '17 at 13:06
• What about lambda x:x*x+1>>1 ? Oct 2 '17 at 15:47
• Or lambda x:x*x+1//2 Disclaimer: I don't know this language's syntax or order of operations, so I guessed; I'm saying add 1 before you //2 instead of negating twice. Oct 2 '17 at 15:57
• @DanHenderson You still need parentheses otherwise it's parsed as (x*x) + (1//2), so that isn't actually shorter. Oct 2 '17 at 17:09

# x86_64 machine language (Linux), 9 8 bytes

0:       97                      xchg   %eax,%edi
1:       f7 e8                   imul   %eax
3:       ff c0                   inc    %eax
5:       d1 f8                   sar    %eax
7:       c3                      retq


To Try it online!, compile and run the following C program.

#include<stdio.h>
const char *f="\x97\xf7\xe8\xff\xc0\xd1\xf8\xc3";
int main() {
for(int i=1; i<10; i++) {
printf("%d\n", ((int(*)())f)(i));
}
}

• Segmentation fault on TIO
– l4m2
Apr 30 at 20:20
• @l4m2 Fixed, thanks! May 9 at 20:36

# J, 8 bytes

Anonymous tacit prefix function.

2>.@%~*:


Try it online!

*: square

>. ceiling (round up)
@ after
2%~ dividing by two

• Alternate solutions: <.@-:@*: and *:<.@%2: Oct 3 '17 at 22:39
• @ConorO'Brien 2>.@%~*:‽ Where did I get that from? I can't read that – looks like line noise to me…
Oct 3 '17 at 22:47
• >.@-:@*: gets my vote. Oct 4 '17 at 2:56
• @Jonah If you squint, you can see a camel.
Oct 4 '17 at 8:30

# Actually, 3 bytes

²½K


Try it online!

# R, 22 21 bytes

cat((scan()^2+1)%/%2)


Try it online!

Square, increment, integer divide. Easy peasy.

Input from stdin; it can take space or newline separated input and it will compute the max wazirs for each input boardsize. Output to stdout.

-1 byte thanks to plannapus

• @plannapus fixed, thank you. Dec 11 '17 at 20:48
• Is the cat necessary? if you run it as an Rscript, it will auto output to stdout.
– qwr
Apr 30 at 21:34
• @qwr there was an older consensus that you had to explicitly write output out; we've mostly gone away from that at this point, but this answer was following that particular rule. Apr 30 at 21:44

# Pyth, 6 bytes

.E**.5


Try it online!

# Pyke, 3 bytes

Xhe


Try it here!

## How?

X   - Square.
h  - Increment.
e - Floor halve.


# C# (.NET Core), 14 12 bytes

-2 bytes thanks to Emigna

n=>(n*n+1)/2


Try it online!

• Save 2 bytes Oct 2 '17 at 12:46
• n=>n*n+1>>1 (11 bytes) Oct 2 '17 at 13:40
• I have noticed the javascript answer to do the same, and this change would make our answers byte to byte equivalent. Oct 2 '17 at 13:41

# Perl 5, 16 bytes

$_=0|$_*$_/2+.5  Try it online! • $_=$_*$_+1>>1 14 bytes Oct 4 '17 at 8:11

# MATL, 5 bytes

UH/Xk


Try it online!

• or UQ2/k at the same byte count. Oct 2 '17 at 13:31

# Cubix, 11 bytes

Iu*:^\)2,O@


Heheh, :^\)

Try it online!

Expands to the following cube:

    I u
* :
^ \ ) 2 , O @ .
. . . . . . . .
. .
. .


Which is the same algorithm that many use.

• ^Iu : read in input as int and change directions
• :* : dup top of stack, multiply
• \) : change direction, increment
• 2, : push 2, integer divide
• O@ : print output as int, end program.

# Mathematica, 12 bytes

⌈#^2/2⌉&

• 11 bytes: ⌈#.5#⌉& Oct 5 '17 at 0:48

# Pyke, 3 bytes

Xhe


Try it here!

X   -   input ** 2
h  -  ^ + 1
e - floor_half(^)

• I ninja'd you by about 12 hours :-/ Oct 3 '17 at 11:56

# Ohm v2, 3 bytes

²½ı


Try it online!

² squares, ½ halves, ı ceils.

# Jelly, 3 bytes

-1 thanks to Mr. Xcoder.

Based on my APL solution.

²HĊ


Try it online!

² Square

HHalve

ĊCeiling (round up)

# Japt, 4 bytes

Been sitting on these since the challenge was closed.

²Ä z


Try it

Explanation: Square, add 1, floor divide by 2

## Alternative

²ÄÁ1


Try it

Explanation: Square, add 1, bit-shift right by 1.

# ,,,, 7 bytes

2*1+1»


Try it online!

Yay, commata works for something!

Fudge, I didn't implement ceiling...

# Gaia, 3 bytes

sḥ⌉


Try it online!

s squares, ḥ halves, ⌉ ceils.

# Commentator, 19 bytes

//{-//-}! {-#  -}<!


Try it online!

Who needs golfing languages? I've got confusing languages!

Ungolfed version:

5//{-8}//{5-}
print(10!= 5)
x={-1,3,4} # Smiley :-}
print(5<!=10)*/ # Weird comparision.


Try it online!

How does it work? I'll explain, with input 5

//                         - Take input.                           Tape: [5 0 0]
{-//-}!                  - Square the input.                     Tape: [25 0 0]
{-                         - Move one along the tape
//                       - Copy the input to the tape.         Tape: [5 5 0]
-}                     - Move one back along the tape
!                    - Take the product of the tape.       Tape: [25 5 0]
<space>           - Increment the tape head.              Tape: [26 5 0]
{-#  -}<! - Halve the tape head (floor division). Tape: [13 2 0]
{-          - Move one along the tape
#         - Set the tape head to 2.             Tape: [26 2 0]
-}     - Move one back along the tape
<!   - Reduce the tape by floor division.  Tape: [13 2 0]


# OCaml, 19 bytes

let f n=(n*n+1)/2;;


Try it online!

I'm a bit bummed the name got changed from "camels" to "wazirs" before I managed to write this, but I figured I'd post it anyway.