Smallest integers after N divisible by 2, 3, and 4
Python 2, 32 bytes
lambda n:[n+2&-2,n/3*3+3,n+4&-4]
Bit arithmetic for 2 and 4, modular arithmetic for 3.
I found four 7-byte expressions for the next multiple of k
above n
but none shorter:
n-n%k+k
~n%k-~n
n/k*k+k
~n/k*-k
Any gives 34 bytes when copies for k=2,3,4
, and 33 bytes if combined:
[n/2*2+2,n/3*3+3,n/4*4+4]
[n/k*k+k for k in 2,3,4]
But, 2 and 4 are powers of 2 that allow bit tricks to zero out the last 1 or 2 bytes.
n+2&-2
n+4&-4
This gives 6 bytes (instead of 7) for getting the next multiple, for 32 bytes overall, beating the for k in 2,3,4
.
Unfortunately, the promising-looking n|1+1
and n|3+1
have the addition done first, so incrementing the output takes parentheses.
Jelly, 8 bytes
~%2r4¤+‘
Try it online! or verify all test cases.
How it works
~%2r4¤+‘ Main link. Argument: n (integer)
~ Bitwise NOT; yield ~n = -(n + 1).
¤ Combine the three links to the left into a niladic chain:
2 Yield 2.
r4 Yield the range from 2 to 4, i.e., [2, 3, 4].
% Yield the remainder of the division of ~n by 2, 3 and 4.
In Python/Jelly, -(n + 1) % k = k - (n + 1) % k if n, k > 0.
‘ Yield n + 1.
+ Add each modulus to n + 1.
Julia, 16 bytes
n->n-n%(r=2:4)+r
Try it online!