# Smallest integers after N divisible by 2, 3, and 4

Objective Given an integer N > 0, out the smallest integers A, B, and C so that:

1. All of A, B, and C are strictly greater than N;
2. 2 divides A;
3. 3 divides B;
4. and 4 divides C.

This is a code-golf, so the shortest answer in bytes wins.

## Test cases

N => A, B, C
1 => 2, 3, 4
4 => 6, 6, 8
43 => 44, 45, 44
123 => 124, 126, 124
420 => 422, 423, 424
31415 => 31416, 31416, 31416
1081177 => 1081178, 1081179, 1081180

• Can we (consistently) output the results in a different order (e.g. C B A) if it's clearly specified in the answer? May 17, 2016 at 14:22
• @MartinBüttner that is acceptable May 17, 2016 at 14:29

## 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.

• Of the possible variants my preference is for n+k-n%k.
– Neil
May 16, 2016 at 8:40
• Does n&3+1 do the addition first too?
– Tim
May 17, 2016 at 21:30
• @Tim Yup, same with all boolean operations.
– xnor
May 18, 2016 at 11:45

# Jelly, 8 bytes

~%2r4¤+‘


### 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.

• 7 bytes Dec 24, 2020 at 20:07

# Julia, 16 bytes

n->n-n%(r=2:4)+r


Try it online!

# MATL, 15 10 9 bytes

2:4+t5M\-


Try it online!

Explanation:

2:4          #The array [2, 3, 4]
+         #Add the input to each element, giving us [12, 13, 14]
t        #Duplicate this array
5M      #[2, 3, 4] again
\     #Modulus on each element, giving us [0, 1, 2]
-    #Subtract each element, giving us [12, 12, 12]

• Nice answer! You can save a byte by using 5M (automatic clipboard of function inputs) instead of the second 2:4. May 16, 2016 at 6:49
• @David Thanks for the tip! May 16, 2016 at 6:54

# MATL, 8 bytes

Qt_2:4\+


Uses Denis' Jelly algorithm, I'm surprised it's the same length!

Q    % takes implicit input and increments by one
t_   % duplicate, and negate top of stack (so it's -(n+1))
2:4  % push vector [2 3 4]
\    % mod(-(n+1),[2 3 4])
+    % add result to input+1
% implicit display

• Well darn it. I was really proud of my 10 byte solution, but I can't beat this. Also, fun trivia: This is exactly my 300th vote. =D May 16, 2016 at 7:34
• Ahh but this was just taking Agawa/Dennis' algorithm, it isn't my own idea. May 16, 2016 at 7:40
• sometimes i keep glaring at those unicode mandarinoid elementary characters while scratching my head then i say "for sake of everything readable is that a forking runnable code" ? lol nice thing have my upvote + i ll join the matl train sooon. May 16, 2016 at 15:01
• @Agawa001 You should! Given know your way around Matlab pretty well, you should find it fairly straightforward, the big difference is that MATL s stack-based. There is a MATL chat-room too if you need any help! May 16, 2016 at 22:46
• @David that pancake based nature of matl makes it more evil scary knowignly that the raw matlab itself is a golf-friendly language regarding its high-level interactability , so imagine .... May 17, 2016 at 15:22

# Matlab, 33 bytes

Another slightly different approach

@(a)feval(@(x)a+1+mod(-a-1,x),2:4)


# 05AB1E, 8 bytes

Code:

>D(3L>%+

• Ah, didn't notice there was already an 05AB1E answer which is rather similar as what I had. I've deleted it, and will suggest it as a -1 golf here instead: ±D2xŸ%α (the 2xŸ is just an alternative for your 3L>; and two other equal-byte alternatives could be Ƶ…S or 4L¦). May 16, 2019 at 11:07

# Ruby, 27 bytes

Maps 2, 3, and 4 to the next multiple above n.

->n{(2..4).map{|e|n+e-n%e}}


# CJam, 15 bytes

5,2>rif{1$/)*N}  ## Pyth, 11 10 bytes m*dh/QdtS4  Test suite.  tS4 generate range [2, 3, 4] m for d in range... *dh/Qd d*(1+input/d)  Thanks to Dennis for a byte! ## Pyke, 11 9 8 bytes 3FODQRc+  Try it here! 3FODQRc+ - Q = input() 3F - for i in range(3): # for i in [0,1,2] O - i += 2 Q c - Q-(Q%i) + - i+^  ## Mathematica, 21 bytes Ceiling[#+1,{2,3,4}]&  This is an unnamed function which takes a single integer as input and returns a list of the multiples. The Ceiling function takes an optional second parameter which tells it to round up to the next multiple of the given number. Thankfully, it also automatically threads over its second argument such that we can give it a list of values and in turn we'll get rounded up multiples for all of those. # Octave, 20 bytes @(n)n-mod(n,d=2:4)+d  Examples: octave:60> f(123) ans = 124 126 124 octave:61> f(1081177) ans = 1081178 1081179 1081180 octave:62> f(420) ans = 422 423 424  Worth noting that we can do this up to 9 without adding any extra bytes: @(n)n-mod(n,d=2:9)+d  Output (2520 is the smallest positive integer evenly divisible by all the single digit numbers): octave:83> f(2520) ans = 2522 2523 2524 2525 2526 2527 2528 2529  # C, 50 46 bytes i;f(int*a,int n){for(i=1;++i<5;*a++=n+i-n%i);}  Thanks to Neil and nwellnhof for saving 4 bytes! Disappointingly long. I feel like there's some bit-shifting hack in here that I don't know about, but I can't find it yet. Returns a pointer to an array holding the three elements. Full program: i;f(int*a,int n){for(i=1;++i<5;*a++=n+i-n%i);} int main() { int array; int n=10; f(array, n); printf("A:%d\tB:%d\tC:%d\n",array,array,array); return 0; }  • I looked at @xnor's bit-twiddling but you need to unroll the loop for that which ends up costing you a byte overall. – Neil May 16, 2016 at 8:49 • Doesn't n + i - n % i++ result in undefined behavior? May 16, 2016 at 12:55 • Also s/a[i-2]/*a++/ to save two bytes. May 16, 2016 at 12:57 • @nwellnhof Bah, I thought of that when I unrolled his loop but it didn't occur to me that he could use it anyway. – Neil May 16, 2016 at 14:11 • @Neil But the undefined behavior can be worked around without making the code larger. Here's an even shorter version using K&R function declarations: f(a,n,i)int*a;{for(i=1;++i<5;)*a++=n+i-n%i;} May 16, 2016 at 14:48 # Haskell, 27 bytes f n=[div n d*d+d|d<-[2..4]]  # Reng, 40 bytes i1+#i2341ø>(1+)31j i(2[¤, q!^$]æl0eq!~


## 1: init

i1+#i2341ø


i1+#i sets the input to 1 + input; this is because we are to work on the numbers strictly greater than the input. 234 initializes the tape with our iteration values, and 1ø jumps to the beginning of the next line.

## 2a: loop

i(2[¤,  q!^$]æl0eq!~  i( puts the input at the STOS, and 2[ makes a new stack with the top 2 elements. ¤ duplicates the stack, and , does modulus. If there is a remainder, q!^ breaks out of the loop to go to (b). Otherwise, we're fine to print. $ removes the extra thingy, ] closes the stack, and æ prints it nicely. l0wq!~ terminates iff the stack contains zero members.

## 2b: that other loop

          >(1+)31j
q!^


(1+) adds 1 to the STOS, and 31j jumps to the part of the loop that doesn't take stuff from the stack. And profit.

That extra whitespace is really bothering me. Take a GIF. ## Labyrinth, 19 bytes

:?
:
#/)
\ #
!"*@
"


Try it online!

This outputs the results in the order C, B, A separated by linefeeds.

### Explanation

As usual, a short Labyrinth primer:

• Labyrinth has two stacks of arbitrary-precision integers, main and aux(iliary), which are initially filled with an (implicit) infinite amount of zeros. We'll only be using main for this answer.
• The source code resembles a maze, where the instruction pointer (IP) follows corridors when it can (even around corners). The code starts at the first valid character in reading order, i.e. in the top left corner in this case. When the IP comes to any form of junction (i.e. several adjacent cells in addition to the one it came from), it will pick a direction based on the top of the main stack. The basic rules are: turn left when negative, keep going ahead when zero, turn right when positive. And when one of these is not possible because there's a wall, then the IP will take the opposite direction. The IP also turns around when hitting dead ends.

Despite the two no-ops (") which make the layout seem a bit wasteful, I'm quite happy with this solution, because its control flow is actually quite subtle.

The IP starts in the top left corner on the : going right. It will immediately hit a dead end on the ? and turn around, so that the program actually starts with this linear piece of code:

:   Duplicate top of main stack. This will duplicate one of the implicit zeros
at the bottom. While this may seem like a no-op it actually increases
the stack depth to 1, because the duplicated zero is *explicit*.
?   Read n and push it onto main.
:   Duplicate.
:   Duplicate.


That means we've now got three copies of n on the main stack, but its depth is 4. That's convenient because it means we can the stack depth to retrieve the current multiplier while working through the copies of the input.

The IP now enters a (clockwise) 3x3 loop. Note that #, which pushes the stack depth, will always push a positive value such that we know the IP will always turn east at this point.

The loop body is this:

#   Push the stack depth, i.e. the current multiplier k.
/   Compute n / k (rounding down).
)   Increment.
#   Push the stack depth again (this is still k).
*   Multiply. So we've now computed (n/k+1)*k, which is the number
we're looking for. Note that this number is always positive so
we're guaranteed that the IP turns west to continue the loop.
"   No-op.
!   Print result. If we've still got copies of n left, the top of the
stack is positive, so the IP turns north and does another round.
Otherwise, see below...
\   Print a linefeed.
Then we enter the next loop iteration.


After the loop was traversed (up to !) three times, all copies of n are used up and the zero underneath is revealed. Due to the " at the bottom (which otherwise seems pretty useless) this position is a junction. That means with a zero on top of the stack, the IP tries to go straight ahead (west), but because there's a wall it actually makes a 180 degree turn and moves back east as if it had hit a dead end.

As a result, the following bit is now executed:

"   No-op.
*   Multiply two zeros on top of the stack, i.e. also a no-op.
The top of the stack is now still zero, so the IP keeps moving east.
@   Terminate the program.


# Matlab, 50 bytes

@(a)arrayfun(@(k)find(~rem(a+1:a+k,k))+a,[2 3 4])

• At the very least, you can use 2:4 instead of [2 3 4]. May 16, 2016 at 23:47

## JavaScript (ES6), 26 bytes

Interestingly porting either @KevinLau's Ruby answer or @xnor's Python answer results in the same length:

n=>[2,3,4].map(d=>n+d-n%d)
n=>[n+2&-2,n+3-n%3,n+4&-4]


I have a slight preference for the port of the Ruby answer as it works up to 253-3 while the port of the Python answer only works up to 231-5.

• ) -> ] as I think May 16, 2016 at 15:58
• @Qwertiy Whoops, sorry for the typo.
– Neil
May 16, 2016 at 17:03

# Retina, 6243 26 bytes

17 bytes thanks to @Martin Büttner.

^
1111:
M!&(11+):(\1*)
:



(Note the trailing newline.)

Try it online!

Input in unary in 1, output in unary in 1 separated by newlines.

## Previous 43-byte version:

.+
11:$&;111:$&;1111:$& \b(1+):(\1*)1*$1$2  Try it online! Input in unary, output in unary separated by semi-colon (;). ## Previous 62-byte version: .+$&11;$&111;$&1111
((11)+)1*;((111)+)1*;((1111)+)1*
$1;$3;$5  Try it online! Input in unary, output in unary separated by semi-colon (;). • 26 if output in the opposite order is allowed: retina.tryitonline.net/… ... Otherwise 33 although I think there must be a shorter way that avoids the reverse sorting: retina.tryitonline.net/… May 16, 2016 at 12:27 • That's enough to qualify as a separate answer? May 16, 2016 at 12:29 • I don't know, it's still the same idea, I just replaced generating the list manually with using overlapping matches. May 16, 2016 at 12:31 • Conor confirmed that the 26-byte solution is valid. May 17, 2016 at 18:18 # Octave, 2722 20 bytes MATLAB and Octave: f=2:4;@(x)f.*ceil((x+1)./f)  Better (solutions are equivalent, but one may outperform the other when further golfed), MATLAB and Octave: @(x)x-rem(x,2:4)+(2:4) f=2:4;@(x)x+f-rem(x,f)  Only in Octave: @(x)x-rem(x,h=2:4)+h  ## Minkolang 0.15, 17 bytes n$z3[zi2+$d%-+N].  Try it here! ### Explanation n$z                  Take number from input and store in register
3[                Open for loop that repeats 3 times
z               Push value in register on stack
i2+            Loop counter plus 2
$d Duplicate stack %-+ Mod, subtract, add N Output as number ]. Close for loop and stop.  • + is division?? May 18, 2016 at 14:41 • @Downgoat: Whoops. >_> May 18, 2016 at 15:23 # APL (Dyalog Unicode), 11 bytes -1 thanks to Adam ⎕(+-|⍨)1+⍳3  Try it online! Explanation: ⎕(+-|⍨)1+⍳3 1+⍳3 ⍝ 2 3 4 ⎕ ⍝ read n +-|⍨ ⍝ {⍺ + ⍵ - ⍵ | ⍺} Let's look at how {⍺ + ⍵ - ⍵ | ⍺} is transformed to +-|⍨: {⍺ + ⍵ - ⍵ | ⍺} {⍺} + {⍵ - ⍵ | ⍺} ⊣ + ({⍵} - {⍵ | ⍺}) ⊣ + (⊢ - ({⍵} | {⍺})) ⊣ + (⊢ - (⊢ | ⊣)) (⊣ + ⊢ - (⊢ | ⊣)) (+ - (⊢ | ⊣)) (+ - (⊣ |⍨ ⊢)) (+ - (|⍨)) (+-|⍨)  • + and - and | are scalar functions, so ¨ isn't needed here. – Adám Jan 11, 2021 at 16:33 • No need for ⎕← – Adám Jan 11, 2021 at 16:34 • i.. think that's how my solution already looks like? Jan 11, 2021 at 19:39 • Sure, but you don't need ⎕← on TIO either, making the code length easier to understand. – Adám Jan 11, 2021 at 19:57 # ><>, 31 bytes &2v :&\& ?!\1+:{:}% ao\n1+:5=?;  Expects N to be present on the stack at program start. Try it online! # Mathematica 28 bytes f@n_:=n-n~Mod~#+#&/@{2,3,4}  f f f f f f f  {2, 3, 4} {6, 6, 8} {44, 45, 44} {124, 126, 124} {422, 423, 424} {31416, 31416, 31416} {1081178, 1081179, 1081180} The general case produces a general answer: f[r]  {2 + r - Mod[r, 2], 3 + r - Mod[r, 3], 4 + r - Mod[r, 4]} # R, 30 26 bytes (Reduced 4 bytes thanks to @Neil) N=scan();cat(N+2:4-N%%2:4)  This (similarly to the rest of the answers I guess) adds 2:4 to the input and the reduces the remainder after running modulo on the same numbers. • As I don't know the language, is there a reason you don't use N+2:4-N%%2:4? – Neil May 16, 2016 at 17:04 • @Neil Cause I haven't thought of it I guess. Thanks though. May 16, 2016 at 18:03 # UGL, 513125 24 bytes icu$l_u^^/%_u^*ocO$dddd:  Try it online! ## Previous 25-byte version: iRcu$l_u$r^/%_u*ocO$dddd:


Try it online!

iRcuuulu$cuuuuuu%-r^/%_u*oddcO:  Try it online! ## Previous 51-byte version: i$\$cuuu/%_ucuuu*@cuuuu/%_ucuuuu*@cuu/%_ucuu*ocOocOo


Try it online!

# Java 70 57

a->System.out.print(a/2*2+2+" "+(a/3*3+3)+" "+(a/4*4+4))

• I don't know Java, but perhaps you can remove the white space around = in int a = new Integer(z); May 23, 2016 at 23:04

# ><>, 28 bytes

i2&::&::&@%-+nao&1+:&5=?;20.


# How it Works

i2&::&::&@%-+nao&1+:&5=?;20.
i2&                                  Takes N as input and stores 2 in the register
::                                Duplicates N twice
&::&                            Pushes the register onto the stack and duplicates twice
@                          Shifts top the 3 values in the stack to the right
Stack now looks like: N N & N & (& = register)
%-+nao                   Computes the smallest multiple of k > N: N+k-N%k, where k is the value of the register
nao                   Outputs answer and a newline.
&1+:&5=?;          Increments register by 1, ends the program if the register equals 5
20.       Jumps back into the loop, skipping taking input and initializing the register


You could save a byte if you are allowed to take input by initializing the stack with N, but TIO doesn't allow for that. The interpreter on https://fishlanguage.com/ does.

2&::&::&@%-+nao&1+:&5=?;10.


Try it online!

# Vyxalr, 8 bytes

3ɾ›ƛ⁰/⌈*


Try it Online!

-1 thanks to lyxal

 ɾ       # Range 1...
3        # Literal
›      # Incremented (2...4)
ƛ     # Map to...
* # Product of...
# Current item (implicit)
* # And...
⌈  # Floor of
# (Implicit) current item
/   # Divided by...
⁰    # Input
`
• Try it Online! for 8 bytes Jun 6, 2021 at 3:45
• @lyxal Nice! I forgot implicit input worked like that. Jun 6, 2021 at 3:46