30
\$\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\$
20
  • 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
  • 16
    \$\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\$
    – DELETE_ME
    Oct 2 '17 at 14:52
  • 18
    \$\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
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Oct 4 '17 at 8:40

50 Answers 50

1
2
1
\$\begingroup\$

TI-Basic, 7 bytes

round(Ans²/2,0

Alternatively (8 bytes):

int(Ans²/2+.5
\$\endgroup\$
2
  • \$\begingroup\$ -int(-.5Ans² also works \$\endgroup\$
    – Oki
    Oct 5 '17 at 14:56
  • \$\begingroup\$ @Oki It sure does. I just wish they had a ceil( function. \$\endgroup\$
    – Timtech
    Oct 5 '17 at 17:55
1
\$\begingroup\$

///, 35 bytes

/I///,*/+,//+/I//**,/,A//*/A//,//,I

Try it online!

Takes input in unary using symbol *, and output in unary using symbol A. This is allowed for some specific languages, including /// (meta)

Because there is no way to take input in ///, input should be hardcoded:

/I/«put input here»//,*/+,//+/I//**,/,A//*/A//,//,I

for input = 4.


Explanation: (before reading, you need to know that the only syntax of /// are /pattern/replacement/, which replace every occurence of pattern by replacement; and \ for escaping; other characters is printed to output)

For n=4:

/I/****//,*/+,//+/I//**,/,A//*/A//,//,I    Start program.
/I/****/                                   Replace all `I` in the program by the input.

/,*/+,//+/****//**,/,A//*/A//,//,****      Remaining part of the program.
/,*/+,/                                    Use the `,` as a scanner, scan through `*` after it and convert to `+`.
       /+/****//**,/,A//*/A//,//++++,      Note that only `*` in the second group is affected.
       /+/****/                            Replace all `+` (which is just created) by `n` asterisks (from the first `I` group)

/**,/,A//*/A//,//****************,         Now at the last of the program before the `,` there are `n²` asterisks.
/**,/,A/                                   Scan the `,` to the left to perform division by 2:
                                           replace each `**` by a `A` as the scanner `,` pass through.
/*/A//,//,AAAAAAAA                         Remaining program.
/*/A/                                      If there is any `*` remaining (if `n²` is odd), replace it with `A`.
     /,//                                  Remove the scanner `,`.
          AAAAAAAA                         Output the result.
\$\endgroup\$
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\$
2
  • \$\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\$
1
  • \$\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
  • 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\$ Oct 2 '17 at 13:38
0
\$\begingroup\$

Neim, 3 bytes

ᛦ>ᚺ

Try it online!

\$\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\$
0
\$\begingroup\$

MMIX, 16 bytes (4 instrs)

xxd, jelly code page

00000000: 1800 0000 2100 0001 3f00 0001 f801 0000  ð¡¡¡!¡¡¢?¡¡¢ẏ¢¡¡

disassembly

waz MUL $0,$0,$0
    ADD $0,$0,1
    SRU $0,$0,1
    POP 1,0

Uses the obvious trick of getting \$\lceil{n^2\over2}\rceil\$ by adding one and rounding down.

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

BRASCA, 8 bytes

igC2^}2/

Explanation

ig        - Turn charcodes into numbers, and concatenate input.
  C       - Set implicit output to output numbers
   2^     - (n**2
     }    -      +1)
      2/  -         /2
          - NOTE: BRASCA only handles whole integers, and will automatically Math.floor().
          - Implicitly output

Try it online!

\$\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\$
1
2

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.