29
\$\begingroup\$

Give credit to whom credit is due.

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
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Can we (consistently) output the results in a different order (e.g. C B A) if it's clearly specified in the answer? \$\endgroup\$ Commented May 17, 2016 at 14:22
  • 1
    \$\begingroup\$ @MartinBüttner that is acceptable \$\endgroup\$ Commented May 17, 2016 at 14:29

47 Answers 47

26
\$\begingroup\$

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.

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

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.
\$\endgroup\$
1
14
\$\begingroup\$

Julia, 16 bytes

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

Try it online!

\$\endgroup\$
12
\$\begingroup\$

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]
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Nice answer! You can save a byte by using 5M (automatic clipboard of function inputs) instead of the second 2:4. \$\endgroup\$
    – David
    Commented May 16, 2016 at 6:49
  • \$\begingroup\$ @David Thanks for the tip! \$\endgroup\$
    – DJMcMayhem
    Commented May 16, 2016 at 6:54
12
\$\begingroup\$

MATL, 8 bytes

Qt_2:4\+

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

Try it online, or, verify all test cases.

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
\$\endgroup\$
5
  • \$\begingroup\$ 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 \$\endgroup\$
    – DJMcMayhem
    Commented May 16, 2016 at 7:34
  • \$\begingroup\$ Ahh but this was just taking Agawa/Dennis' algorithm, it isn't my own idea. \$\endgroup\$
    – David
    Commented May 16, 2016 at 7:40
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Abr001am
    Commented May 16, 2016 at 15:01
  • \$\begingroup\$ @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! \$\endgroup\$
    – David
    Commented May 16, 2016 at 22:46
  • \$\begingroup\$ @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 .... \$\endgroup\$
    – Abr001am
    Commented May 17, 2016 at 15:22
6
\$\begingroup\$

Matlab, 33 bytes

Another slightly different approach

@(a)feval(@(x)a+1+mod(-a-1,x),2:4)
\$\endgroup\$
6
\$\begingroup\$

05AB1E, 8 bytes

Code:

>D(3L>%+

Try it online!.

\$\endgroup\$
1
  • \$\begingroup\$ 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¦). \$\endgroup\$ Commented May 16, 2019 at 11:07
5
\$\begingroup\$

Ruby, 27 bytes

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

->n{(2..4).map{|e|n+e-n%e}}
\$\endgroup\$
4
\$\begingroup\$

CJam, 15 bytes

5,2>rif{1$/)*N}

Try it online! or verify all test cases.

\$\endgroup\$
4
\$\begingroup\$

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!

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

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+^
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

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[3];
    int n=10;
    f(array, n);
    printf("A:%d\tB:%d\tC:%d\n",array[0],array[1],array[2]);
    return 0;
}
\$\endgroup\$
6
  • \$\begingroup\$ I looked at @xnor's bit-twiddling but you need to unroll the loop for that which ends up costing you a byte overall. \$\endgroup\$
    – Neil
    Commented May 16, 2016 at 8:49
  • \$\begingroup\$ Doesn't n + i - n % i++ result in undefined behavior? \$\endgroup\$
    – nwellnhof
    Commented May 16, 2016 at 12:55
  • \$\begingroup\$ Also s/a[i-2]/*a++/ to save two bytes. \$\endgroup\$
    – nwellnhof
    Commented May 16, 2016 at 12:57
  • \$\begingroup\$ @nwellnhof Bah, I thought of that when I unrolled his loop but it didn't occur to me that he could use it anyway. \$\endgroup\$
    – Neil
    Commented May 16, 2016 at 14:11
  • 2
    \$\begingroup\$ @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;} \$\endgroup\$
    – nwellnhof
    Commented May 16, 2016 at 14:48
4
\$\begingroup\$

Haskell, 27 bytes

f n=[div n d*d+d|d<-[2..4]]
\$\endgroup\$
4
\$\begingroup\$

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

REENNNNNGGG

\$\endgroup\$
4
\$\begingroup\$

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.
\$\endgroup\$
3
\$\begingroup\$

Matlab, 50 bytes

@(a)arrayfun(@(k)find(~rem(a+1:a+k,k))+a,[2 3 4])
\$\endgroup\$
1
  • \$\begingroup\$ At the very least, you can use 2:4 instead of [2 3 4]. \$\endgroup\$
    – Suever
    Commented May 16, 2016 at 23:47
3
\$\begingroup\$

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.

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

Retina, 62 43 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 (;).

\$\endgroup\$
4
  • \$\begingroup\$ 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/… \$\endgroup\$ Commented May 16, 2016 at 12:27
  • \$\begingroup\$ That's enough to qualify as a separate answer? \$\endgroup\$
    – Leaky Nun
    Commented May 16, 2016 at 12:29
  • \$\begingroup\$ I don't know, it's still the same idea, I just replaced generating the list manually with using overlapping matches. \$\endgroup\$ Commented May 16, 2016 at 12:31
  • \$\begingroup\$ Conor confirmed that the 26-byte solution is valid. \$\endgroup\$ Commented May 17, 2016 at 18:18
3
\$\begingroup\$

Octave, 27 22 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

Try here.

\$\endgroup\$
1
3
\$\begingroup\$

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.
\$\endgroup\$
2
  • \$\begingroup\$ + is division?? \$\endgroup\$
    – Downgoat
    Commented May 18, 2016 at 14:41
  • \$\begingroup\$ @Downgoat: Whoops. >_> \$\endgroup\$ Commented May 18, 2016 at 15:23
3
+100
\$\begingroup\$

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 +-|⍨:

        {⍺ + ⍵ - ⍵ | ⍺}
       {⍺} + {⍵ - ⍵ | ⍺}
         ⊣ + ({⍵} - {⍵ | ⍺})
         ⊣ + (⊢ - ({⍵} | {⍺}))
         ⊣ + (⊢ - (⊢ | ⊣))
        (⊣ + ⊢ - (⊢ | ⊣))
          (+ - (⊢ | ⊣))
          (+ - (⊣ |⍨ ⊢))
          (+ - (|⍨))
          (+-|⍨)
\$\endgroup\$
4
  • \$\begingroup\$ + and - and | are scalar functions, so ¨ isn't needed here. \$\endgroup\$
    – Adám
    Commented Jan 11, 2021 at 16:33
  • \$\begingroup\$ No need for ⎕← \$\endgroup\$
    – Adám
    Commented Jan 11, 2021 at 16:34
  • \$\begingroup\$ i.. think that's how my solution already looks like? \$\endgroup\$ Commented Jan 11, 2021 at 19:39
  • 1
    \$\begingroup\$ Sure, but you don't need ⎕← on TIO either, making the code length easier to understand. \$\endgroup\$
    – Adám
    Commented Jan 11, 2021 at 19:57
2
\$\begingroup\$

><>, 31 bytes

&2v
:&\&
?!\1+:{:}%
ao\n1+:5=?;

Expects N to be present on the stack at program start. Try it online!

\$\endgroup\$
2
\$\begingroup\$

Mathematica 28 bytes

f@n_:=n-n~Mod~#+#&/@{2,3,4}

f[1]
f[4]
f[43]
f[123]
f[420]
f[31415]
f[1081177]

{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]}

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ As I don't know the language, is there a reason you don't use N+2:4-N%%2:4? \$\endgroup\$
    – Neil
    Commented May 16, 2016 at 17:04
  • \$\begingroup\$ @Neil Cause I haven't thought of it I guess. Thanks though. \$\endgroup\$ Commented May 16, 2016 at 18:03
2
\$\begingroup\$

UGL, 51 31 25 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!

Previous 31-byte version:

iRcuuulu$cuuuuuu%-r^/%_u*oddcO:

Try it online!

Previous 51-byte version:

i$$cuuu/%_ucuuu*@cuuuu/%_ucuuuu*@cuu/%_ucuu*ocOocOo

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Java 70 57

a->System.out.print(a/2*2+2+" "+(a/3*3+3)+" "+(a/4*4+4))
\$\endgroup\$
1
  • \$\begingroup\$ I don't know Java, but perhaps you can remove the white space around = in int a = new Integer(z[0]); \$\endgroup\$ Commented May 23, 2016 at 23:04
2
\$\begingroup\$

><>, 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!

\$\endgroup\$
2
\$\begingroup\$

Vyxal r, 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
\$\endgroup\$
2
  • \$\begingroup\$ Try it Online! for 8 bytes \$\endgroup\$
    – lyxal
    Commented Jun 6, 2021 at 3:45
  • \$\begingroup\$ @lyxal Nice! I forgot implicit input worked like that. \$\endgroup\$
    – emanresu A
    Commented Jun 6, 2021 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.