| Bytes | Lang | Time | Link |
|---|---|---|---|
| 010 | x86 32bit machine code | 250409T122956Z | m90 |
| 016 | Redirection | 250409T172916Z | m90 |
| 005 | Jelly | 250407T191325Z | Jonathan |
| 023 | AWK | 250407T154115Z | xrs |
| 009 | Charcoal | 250407T085114Z | Neil |
| 005 | 05AB1E | 250407T070300Z | Kevin Cr |
| 014 | JavaScript Node.js | 250407T051130Z | l4m2 |
| 019 | Python | 250407T025556Z | xnor |
x86 32-bit machine code, 10 bytes
99 D1 E8 83 D8 FF 4A 7A F8 C3
Following the regparm(1) calling convention, this function takes a 32-bit integer in EAX and returns a 32-bit integer in EAX.
This uses the headline definition from A110657: apply A028242 (2m ↦ m+1, 2m+1 ↦ m) twice.
In assembly:
f: cdq # Set EDX to 0 by sign-extending EAX.
r: shr eax, 1 # Shift EAX right by 1 bit. The last bit goes into CF.
sbb eax, -1 # Subtract (-1 - CF) from EAX: +1 if the last bit was 0.
dec edx # Decrement EDX, to -1 the first time and -2 the second time.
jpe r # Jump back if the sum of the eight low bits is even.
# This is true for -1 (...11111111) but not for -2 (...11111110).
ret # Return.
Alternative: 7 bytes, works on 8-bit integers
40 D4 04 D5 01 48 C3
In assembly:
f: inc eax # Add 1 to EAX; its low byte, AL, is increased by 1.
aam 4 # Divide AL by 4; AH:=quotient, AL:=remainder.
aad 1 # Set AL to AH*1+AL (and AH to 0).
dec eax # Subtract 1 from EAX (AL is decreased by 1).
ret # Return.
Re:direction, 16 bytes
v>
++>
>++
v>+v
This uses the headline definition from A110657: apply A028242 (2m ↦ m+1, 2m+1 ↦ m) twice.
The input number n initialises the queue with n >s and one v. At the top-left, a v is added and execution is sent down, into the loop on line 1.
This loop consumes the >s, adding a > for every two >s consumed, then goes down with the v. If the number was even, it goes down on the left column, and one more > is added.
In either case, we arrive at the same +. The first time we get there, it consumes the extra v to go down, into a path that adds v>v and reenters the loop on line 1.
The second time we arrive at that +, the queue contains >v followed by a number of >s. The > sends execution right, into another + which consumes the v to go down, leaving just the number of >s. Then one > is added, one > is consumed, and finally the v adds one v and points to itself to end execution.
Jelly, 5 bytes
_3d4S
A monadic Link that accepts the number of checkers and yields the number of leftovers.
How?
$$\Bigl \lfloor \frac{n-3}{4} \Bigr \rfloor + ((n-3) \mod 4)$$
_3d4S - Link: non-negative integer, N
_3 - {N} subtract three -> N - 3
d4 - {that} div-mod four -> [(N - 3) // 4, (N - 3) % 4]
S - sum -> (N - 3) // 4 + ((N - 3) % 4)
Charcoal, 9 bytes
I⁻N׳÷⊕θ⁴
Try it online! Link is to verbose version of code. Explanation: I independently found @xor's first and third (see @KevinCruijssen's answer) formulae, and I also double-checked his second approach, but it was the first one that produces the shortest port.
θ First input
⊕ Numerically incremented
÷ Integer divided by
⁴ Literal integer `4`
× Multiplied by
³ Literal integer 3
⁻ Subtracted from
N First input as a number
I Cast to string
Implicitly print
05AB1E, 6 5 bytes
>4‰O<
Originally two equal-bytes ports of @xnor's Python answers, but now golfed an additional byte by @xnor with a third variation:
$$\left\lfloor\frac{n+1}{4}\right\rfloor+((n+1)\bmod 4)-1$$
Try it online or verify the first 12 test cases.
Explanation:
> # Increase the (implicit) input-integer by 1: n+1
4‰ # Divmod it by 4: [(n+1)//4,(n+1)%4]
O # Sum this pair together: (n+1)//4+(n+1)%4
< # Decrease it by 1: (n+1)//4+(n+1)%4-1
# (after which the result is output implicitly)
Python, 19 bytes
lambda n:n--~n//4*3
The formula is $$n-3\left\lfloor \frac{n+1}{4}\right\rfloor$$
This comes from starting with \$n\$ checkers and removing sandwiches of 3. How many sandwiches? The color alternation doesn't let sandwiches touch, so the densest they pack is: sandwich of 3, skip 1, sandwich of 3, skip 1, and so on. This allows one sandwich per 4 checkers except the first one uses only 3, for \$\left\lfloor \frac{n+1}{4}\right\rfloor\$ sandwiches.
An alternate formulation is:
lambda n:n//4+n%4%3
with n//4 leftovers as one per group of 4, then the remaining n%4 are leftovers unless this is 3 where they form another sandwich leaving 0 leftovers.
