26
\$\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. You may use a language made/updated after this challenge, but it is strictly non-competing.

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\$ – Martin Ender May 17 '16 at 14:22
  • 1
    \$\begingroup\$ @MartinBüttner that is acceptable \$\endgroup\$ – Conor O'Brien May 17 '16 at 14:29

45 Answers 45

1
2
1
\$\begingroup\$

J, 18 bytes

2 3 4&([*>:@<.@%~)

Try it online!

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

Mathcad, [tbd] bytes

enter image description here

function version allows for simple evaluation of different divisor sets.


Mathcad byte equivalence scheme not yet determined. Keyboard equivalence ~ 16 bytes.

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

Desmos, 141 136 bytes

a=1
b=2\operatorname{ceil}\left(.5a+.1\right)
c=3\operatorname{ceil}\left(\frac{a}{3}+0.1\right)
d=4\operatorname{ceil}\left(.25a+.1\right)

Demonstration
Fixed the outputs while shaving off a few bytes.

\$\endgroup\$
2
  • \$\begingroup\$ Also, it's possible to remove the a=1\n. Having the user click on the a to set it is equivalent input. \$\endgroup\$ – Conor O'Brien Jun 6 '16 at 19:06
  • \$\begingroup\$ But doing that would create an error. \$\endgroup\$ – weatherman115 Jun 6 '16 at 19:29
1
\$\begingroup\$

Brachylog, 12 bytes

Ṫ;234z%ᵛ0&>ᵛ

Try it online!

Takes input through the output variable and gives output through the input variable. (Shortest I could get it without reversed I/O was ~{>ᵛ}Ṫ;234z%ᵛ0∧Ṫ, which at 16 bytes ties an extremely awkward implementation of Dennis' algorithm: +₁Xṅg;234z%ᵐ+ᵐ↙X.)

           ᵛ    Every element of
         &      the input variable
          >     is strictly greater than
                the output variable.
                Also, the input variable
Ṫ               is a list of three elements
     z          which zipped
 ;234           with 2, 3, and 4
       ᵛ0       produces 0 for each pair
      %         when the first element is taken mod the second.
\$\endgroup\$
1
\$\begingroup\$

Forth (gforth), 40 bytes

: f 5 2 do dup i over i mod - + . loop ;

Try it online!

Explanation

Uses modulo of 2, 3, and 4 to get the distance to the next divisible number and then adds it to the original number (+1)

Code Explanation

: f                \ start a new word definition
  5 2              \ set up parameters loop from 2 to 4
  do               \ start a counted loop
    dup i over     \ duplicate N, get the loop index, and then copy N again
    i mod          \ get N mod Loop-index (distance from last divisible number)
    -              \ subtract from Loop-index to get distance to next divisible number
    +              \ add distance-to-next to N to get number
    .              \ print 
  loop             \ end counted loop
;                  \ end word definition
    
\$\endgroup\$
1
\$\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\$
1
\$\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 Jun 6 at 3:45
  • \$\begingroup\$ @lyxal Nice! I forgot implicit input worked like that. \$\endgroup\$ – A username Jun 6 at 3:46
0
\$\begingroup\$

Scala, 268 bytes

def logic(y:Int)(x: Int) = (x)%y ==0

def fun(n:Int,  logic: Int => Boolean) : Int = { 
  if (logic(n)) n else fun(n+1, logic) 
} 
for { 
    N <- Array[Int](1,4, 43, 123, 420, 31415, 1081177)
    ABC <- Array[Int](2,3,4)
    res = fun(x+1, logic(i) _)
    } yield res
\$\endgroup\$
2
  • \$\begingroup\$ Scala version... \$\endgroup\$ – Avis May 19 '16 at 14:48
  • \$\begingroup\$ Welcome to PPCG! Please include your language's name on the header. This made me realize it is Scala. Please include your bytecount along with the header, since this is code-golf. \$\endgroup\$ – Erik the Outgolfer May 19 '16 at 16:43
0
\$\begingroup\$

Japt, 8 bytes

3õ@Ä c°X

Try it

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

Perl 5 -n, 26 bytes

//;map{say$'+$_-$'%$_}2..4

Try it online!

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

Perl 6, 25 bytes

{[$^a+$_-$a%$_ for 2..4]}

Try it online!

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

Arm Thumb (no div), 30 bytes

Raw machine code:

3001 1c42 2301 439a 600a 2302 3202 439a
608a 1c03 3b03 dcfd 1ac0 6048 4770

Uncommented assembly:

        .text
        .thumb
        .globl smallest_after_n
        .thumb_func
smallest_after_n:
        adds    r0, #1
        adds    r2, r0, #1
        movs    r3, #1
        bics    r2, r3
        str     r2, [r1]
        movs    r3, #2
        adds    r2, #2
        bics    r2, r3
        str     r2, [r1, #8]
        movs    r3, r0
.Lmod_loop:
        subs    r3, #3
        bgt     .Lmod_loop
.Lmod_loop_end:
        subs    r0, r3
        str     r0, [r1,#4]
        bx      lr

Explanation

Happens to be valid Thumb-1, but feel free to use Thumb-2 to compete.

It is easier to understand in the equivalent C code.

void smallest_after_n(int32_t N, int32_t out[3])
{
    ++N; // increment N
    // Even multiple of 2: Lowest bit is zero.
    int32_t A = (N + 1) & ~1;
    out[0] = A;
    // Even multiple of 4: Lowest 2 bits are zero.
    // Since we know the lowest bit of A is zero, we reuse it. 
    int32_t C = (A + 2) & ~2;
    out[2] = C;
    // Calculate the "inverse modulo" using a subtraction loop.
    // N % 3 == 0 => 0
    // N % 3 == 1 => -2
    // N % 3 == 2 => -1
    int32_t inv_modulo = N;
    while ((inv_modulo -= 3) > 0) {}
    // Round up by subtracting a negative
    int32_t B = N - inv_modulo;
    out[1] = B;
}
\$\endgroup\$
0
\$\begingroup\$

Java (JDK), 141 129 bytes

interface C{static void main(String[]a){var n=Double.valueOf(a[0])+1;for(int i=1;++i<5;)System.out.print(i*Math.ceil(n/i)+" ");}}

Try it online!

You may notice that no class has been defined, but instead an interface. This is just to further golf the code. Since the interface hasn't been given a public access modifier, the main method doesn't need it, either.

For a better understanding of this code, it's shown below with the variable a remnamed to its traditional name args:

var n = Double.valueOf(args[0]) + 1;
for (int i = 1; ++i < 5;)
    System.out.print(i * Math.ceil(n / i) + " ");
}

The program inputs a value as a command-line argument which is immediately incremented for compactness before being stored in the double variable n. Next, the for loop cycles through each integer i from 2 through to 4, printing the output values each cycle. See here for an interactive demonstration of the formula used to calculate said values.

In order to further golf this code, I think finding a way to use Java 6 would be ideal due to the static block's functionality, as expressed below:

public class Foo {
    static {
        // Your code here
    }
}

As you can see, although it's definitely good practice, explicitly defining the main method isn't necessary in this particular version of Java. (Correct me if that was incorrectly phrased)

Unfortunately, in performing such a conversion, the var keyword would no longer exist (since it was introduced in Java 10). More importantly, reconstructing the value input mechanism would be necessary, since a static block obviously doesn't have any method parameters, and especially not for command-line arguments. Feel free to leave comments and suggestions.

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

C (gcc), 41 bytes

i=5;f(int*n){for(;--i;n[i-2]=*n+i-*n%i);}

Try it online!

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

MMIX, 36 bytes (9 instrs)

Uses bit tricks to avoid dividing by 2 or 4 (sadly, it's unavoidable for 3)

foo ADDU $2,$0,1        // a = n + 1
    ANDN $2,$2,1        // a &= ~1
    ADDU $1,$0,3        // c = n + 3
    PUT  rD,0
    DIVU $0,$0,3
    GET  $0,rR          // b = n % 3
    SUBU $0,$1,$0       // b = c - b (= n + 3 - n % 3)
    ANDN $1,$1,3        // c &= ~3
    POP  3,0            // return a,b,c
\$\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.