29
\$\begingroup\$

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

\$\endgroup\$
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – Adám Oct 2 '17 at 8:32
  • 2
    \$\begingroup\$ @SashaR May I rewrite your question as a proper code golf challenge? \$\endgroup\$ – Adám Oct 2 '17 at 9:09
  • 2
    \$\begingroup\$ Sure! That way I can also see how to word coding related questions in future \$\endgroup\$ – Sasha R Oct 2 '17 at 9:10
  • 15
    \$\begingroup\$ 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. \$\endgroup\$ – user202729 Oct 2 '17 at 14:52
  • 16
    \$\begingroup\$ 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. \$\endgroup\$ – Mark S. Oct 3 '17 at 11:26

48 Answers 48

1
\$\begingroup\$

Cubically, 12 bytes

$:*FDF'+8/0%

Try it online!

Personally I don't see any point with using language features newer than the challenge to get less bytes, because you mostly compete with yourself (almost always there are at most one answer/language/challenge), but I still updated, by MDXF's suggestion. : defaults to :7 is a good idea anyway.


Explanation:

$:*7FDF'+8/0%
$                  Read input as number.
 :                 Set notepad value (face 6) to input.
  *                Multiply notepad value with itself.
   FDF'            Scramble the cube so face 8 value will be 1 
                   (unsolved) and face 0 value will be 2.
       +8          Add 1 (face 8 value) to notepad value.
         /0        Divide notepad value by 2 (face 0 value),
                   get integer part.
           %       Output notepad value as number.

This program works because ceil(n^2 / 2) == floor((n^2 + 1) / 2).

\$\endgroup\$
  • \$\begingroup\$ Save 1 byte by replacing *7 with * as * implicitly is equivalent to *6, another byte by replacing %6 with %. Thanks for suggesting these changes! \$\endgroup\$ – MD XF Oct 8 '17 at 21:39
  • \$\begingroup\$ :7 can be replaced with :, %6 can be replaced with %, and *7 can be replaced with *6 which can be replaced with *. Also, the rule about languages/interpreters needing to be older than the language was revoked, so the golfed program does not need to be non-competing. \$\endgroup\$ – MD XF Jan 9 '18 at 3:30
0
\$\begingroup\$

Husk, 3 bytes

⌈½□

Try it online!

The code is pretty much self-explanatory:

  • Square.

  • ½ Halve.

  • Ceil.

\$\endgroup\$
  • \$\begingroup\$ ? Hust uses APL symbols :-) \$\endgroup\$ – Adám Oct 2 '17 at 16:19
0
\$\begingroup\$

Convex, 5 bytes

{²½¯}

Try it online!

This is the same as CJam's {2#2./m]}.

\$\endgroup\$
0
\$\begingroup\$

Java 8, 20 11 bytes

i->i*i+1>>1
\$\endgroup\$
  • 1
    \$\begingroup\$ i->(i*i+1)/2 (with int instead of double as return type) is shorter (12 bytes). And i->i*i+1>>1 (11 bytes) is probably the shortest. \$\endgroup\$ – Kevin Cruijssen Oct 2 '17 at 13:38
0
\$\begingroup\$

,,,, 7 bytes

2*1+1»

Try it online!

Yay, commata works for something!

Fudge, I didn't implement ceiling...

\$\endgroup\$
0
\$\begingroup\$

Neim, 3 bytes

ᛦ>ᚺ

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Gaia, 3 bytes

sḥ⌉

Try it online!

s squares, halves, ceils.

\$\endgroup\$
0
\$\begingroup\$

RProgN 2, 6 bytes

2^1+2÷

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PocoLithp, 21 bytes

(#(N)(/(+(* N N)1)2))

Anonymous lambda taking argument N. Can be used in either of the following ways:

  • ((#(N)(/(+(* N N)1)2))7) - call with argument 7

  • (begin (define camel (#(N)(/(+(* N N)1)2))) (camel 7))

Enter either of these lines into the REPL to test.

This language was created a few weeks ago, and the most recent changes a day ago are just bug fixes.

\$\endgroup\$
0
\$\begingroup\$

ARBLE, 11 bytes

ceil(n^2/2)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Add++, 13 bytes

+?
^2
+1
/2
O

Try it online!

Looks like I got here too late (again)

\$\endgroup\$
0
\$\begingroup\$

ASP, 13 bytes

a((x*x+1)/2).

Call with clingo -c x=7 or by putting #const x=7. in source.

\$\endgroup\$
0
\$\begingroup\$

FALSE, 6 bytes

$*1+2/

Explanation:

  • $ duplicate top of stack
  • * multiply
  • 1 push 1
  • + add
  • 2 push 2
  • / divide

False only has 32-bit integers.

\$\endgroup\$
0
\$\begingroup\$

Haskell, 18 bytes

w n=ceiling(n*n/2)

Run as:

main = print $ w n

Where n is the input number.

\$\endgroup\$
0
\$\begingroup\$

x86 machine code, 6 bytes

Takes its argument in eax register, returns also there.

f7 e0   mul eax
40      inc eax
d1 e8   shr eax,1
c3      ret
\$\endgroup\$
0
\$\begingroup\$

Bash, 18 bytes

echo $[$1*$1+1>>1]

Try it online

\$\endgroup\$
0
\$\begingroup\$

Excel, 15 bytes

=ROUND(A1^2/2,)

Because we are dividing by 2, we can safely replace CEILING with ROUND, saving 3 bytes.

=CEILING(A1^2/2,1)
\$\endgroup\$
-1
\$\begingroup\$

JavaScript (ES8), 16 bytes

x=>(h=x*x/2)+h%1
x=>                  // arrow function
   (h=x*x/2)         // dictionarying
            +h%1     // Math.ceil function
\$\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.