g | x | w | all
Bytes Lang Time Link
007Pip250409T164314ZDLosc
024CASIO BASIC CASIO fx9750GIII250409T133341Zmadeforl
023AWK170104T180327ZRobert B
065SAKO250321T180249ZAcrimori
002Thunno 2230607T170411ZThe Thon
034Desmos230818T213426Zfwoosh
020ARBLE230807T032404ZATaco
nanFig221013T040609Ztybocopp
3625Vyxal r230807T043854Zlyxal
036Nibbles230807T114655ZDominic
009Itr230807T050123Zbsoelch
020Alice230807T042410ZJulian
008Canvas221011T041722Zhakr14
005Vyxal r221011T033617ZDialFros
005Brachylog v2210201T235917ZUnrelate
2018convey201206T170227Zxash
039Python 3210128T102130ZMthd
015Perl 5 p201207T164458ZXcali
023Rust201204T211119ZAiden4
048Powershell180131T154044Zrpgwaite
015GolfScript201017T025615Z2014MELO
005Husk201017T002808ZLegionMa
075W h d200122T112936Zuser8505
00505AB1E200122T120732ZKevin Cr
013Perl 6200122T131047ZJo King
058Whitespace200122T124424ZKevin Cr
198Mathematica170103T093910ZGreg Mar
011Pyt180131T043002Zmudkip20
017TIBasic170105T165328ZTimtech
028Funky180131T022107ZATaco
024Octave/MATLAB170103T103023Zflawr
036Excel VBA170103T122258ZTaylor R
057SNOBOL4 CSNOBOL4180130T220657ZGiuseppe
nan><>180130T205913ZJo King
012Cubix180130T163134Zuser4854
026Ruby C implementation180130T193758Zhistocra
006Jelly180130T183443Zsporkl
016Add++180130T165743Zcaird co
031Forth gforth180130T162457Zreffu
025Julia 0.6180130T162156Zgggg
018FALSE180130T155750Z12Me21
030C gcc180130T162018ZPrincePo
005Japt180130T160405ZShaggy
074Emojicode170730T121444Zbetseg
046SmileBASIC170124T071354Z12Me21
046Java 7170103T172531Zpeech
013Labyrinth170106T173450ZMartin E
090Clojure170106T015459ZCarcigen
049S.I.L.O.S170105T183337ZRohan Jh
058GameMaker Language170105T165703ZTimtech
028Ruby170104T191251ZAlexis A
036Q/KDB+ 36 Bytes170105T143146ZAdam J
030PHP170103T105430ZArthur S
055C++ 55 Bytes170105T074223Zjyoti pr
025Perl170105T063906ZGreg Nis
021dc170104T084515ZMitchell
033Scala170104T234812ZDan Getz
030C170104T175141Zcmaster
019Perl170103T090753ZDada
037Java 8170104T162901Zuser9023
017Vitsy170104T095938ZAddison
035REXX170104T102307Zidrougge
nanJava 8170104T100955ZAddison
023JavaScript ES6170103T100117ZArnauld
008Japt170103T213420ZOliver
045Matlab170104T000343ZOwen Mor
2321DUP170103T222236ZM L
032FALSE170103T233117ZM L
028Retina170103T212911ZFryAmThe
019Beeswax170103T204021ZM L
053Batch170103T195653ZNeil
024Befunge170103T115226ZJames Ho
026PowerShell170103T191320Zbriantis
048Clojure170103T191106ZNahuel G
174COW170103T184741ZGabriel
005Jelly170103T172442ZDennis
037bash170103T181835ZMitchell
014QBIC170103T163558Zsteenber
006MATL170103T162510ZDJMcMayh
00605AB1E170103T153512ZMagic Oc
nan170103T154815ZBrad Gil
005Pyke170103T151642ZBlue
035C170103T104030ZGiacomo
010Jellyfish170103T110507ZZgarb
052Racket170103T120045Zrnso
028julia170103T090206Zrahnema1
026Haskell170103T114842Zxnor
032Python 2.7.9170103T111643Zxnor
010Brachylog170103T101601Zuser6213
010Brachylog170103T085632ZFatalize
008Dyalog APL170103T105401ZAdá
070C170103T104608ZAbel Tom
005Jelly170103T104156Zuser6213
029Haskell170103T101616Zflawr
028R170103T100515Zrturnbul
007Actually170103T093143Zuser4594
003Pyth170103T091136Zbusukxua
012Hexagony170103T094110ZSok
nan><>170103T092112ZSok
037C#170103T091106ZAlfie Go
006Jelly170103T085652Zsteenber
035Python170103T090318Zorlp
00705AB1E170103T090000ZEmigna

Pip, 7 bytes

Ta%oUoo

Attempt This Online!

Explanation

Ta%oUoo
         ; o is 1; a is command-line argument (implicit)
Ta%o     ; Loop until a mod o is truthy (nonzero):
    Uo   ;   Increment o
      o  ; Output o

CASIO BASIC (CASIO fx-9750GIII), 24 bytes

?→N
1
While Not (N Rmdr Ans
Ans+1
WhileEnd

outputs via Ans (which does display the result on the screen)

AWK,25 29 27 23 bytes

{for(;!($1%++i););}$0=i

Try it online!

NOTE: If using multiline input, input following the first line may not result in the smallest possible possible.

As with all AWK scripts, the code can be placed in a file or typed in at the command line.

Command Line Usage:

awk '{for(;!($1%++i););}$0=i' <<< inputNumber

SAKO, 65 bytes

PODPROGRAM:F(N)
*)POWTORZ:I=1(1)I-0*MOD(ENT(N),ENT(I))
F()=I
WROC

We use here 0 to the power of modulo to check if I is a divisor.

Full programme version, 75 bytes

CZYTAJ:N
*1)POWTORZ:I=1(1)I-0*MOD(ENT(N),ENT(I))
DRUKUJ(9,0):I
STOP1
KONIEC

Thunno 2, 2 bytes

Ƙ%

Try it online!

Explanation

Ƙ%  # Implicit input
Ƙ   # First integer (starting from 1) where the following is truthy:
 %  #  The (implicit) input mod the number
    #  (this is nonzero/truthy iff the input isn't divisible by the number)
    # Implicit output

Desmos, 34 bytes

Note that this does not work for \$n>10000\$ because of Desmos' maximum list length of 10000.

r=[2...n+1]
f(n)=r[\mod(n,r)>0][1]

Try it on Desmos!

ARBLE, 27 20 bytes

(range(2,n+9)&n%x)/z

Try it online!

Fig, \$10\log_{256}(96)\approx\$ 8.231 bytes

[FJkxxG@}1

Try it online!

   k        # Get the factors of
    x       # the input,
  J         # then append
     x      # the input to that (k doesn't include it).
 F          # Filter out anything in that list from:
      G     #   an infinite list
       1    #   starting at 1
        @}  #   incrementing each time.
[           # Get the first item from that.

Vyxal r, 29 bitsv1, 3.625 bytes

?%)ṅ

Try it Online!

Nibbles, 3 bytes (6 nibbles)

/|,~%@

Attempt This Online!

I'm always amazed that Nibbles is able to 'fold-from-right' over an infinite list...

/|,~%@      # full program
/|,~%@$$    # with implicit variables added
 |          # filter
  .         # 1..
   ~        #    infinity
            # by truthy result of
     @      # input
    %       # modulo
      $     # it;
/           # then, fold from right
       $    # returning left element each time
            # (so: return left-most element)

Itr, 9 bytes

#ä2+º%Ì1@

online interpreter

Explanation

#         ; get an integer N from standard input
 ä2+º     ; push the range from zero to N+1
     %    ; vectorized remainder
      Ì   ; get indices of non-zero elements (zero-indexed)
       1@ ; get 2nd element (mod 0 is always non-zero)

Alice, 20 bytes

/O
\M@/1!w.?h!?%n$K?

Try it online!

M1!w.?h!?%n$K?O@ # full program
M                # read one argument and put it on the stack
 1!              # save 1 on the tape
   w      n$K    # while loop, until the modulus is not 0
    .            # duplicate the input
     ?h!         # get the number on the tape, increase it and update the tape
        ?%       # pops the input and the number on the tape, push the modulus
             ?O@ # print the last number on the tape, the first number that does not divide the input

Canvas, 8 bytes

1W╵╶┌%!}

Try it here!

Explanation:
1W╵╶┌%!} | Explanation             | Stack Visualization (of first iteration)
---------+-------------------------+-----------------------------------------
1        | Push 1                  | 1
 W     } | Do ...                  | 1
  ╵      |  Increment ToS          | 2
   ╶     |  Push input             | 2, <input>
    ┌    |  Duplicate 2nd from top | 2, <input>, 2
     %!  |  Divides?               | 2, <0/1>
       } | ... while POP is truthy | <output>
         | Print ToS (implicit)    | 

Vyxal r, 5 bytes

?Ḋ¬)ṅ

Try it Online!

Or

5 bytes

₌ɾKFh

Try it Online!

Brachylog v2, 5 bytes

f≡ⁿℕ₁

Try it online!

   ℕ₁    The output is a positive integer
 ≡ⁿ      which is not an element of
f        the input's factors.

convey, 20 18 bytes

-2 thanks to Jo King!

 >!/|!`!}
{"^."^[1

Try it online!

run for 24

The values follow the arrow heads. { gets the N, here 24. This value is " duplicated and both values are then merged into !: repeat 24 for 24 times. The 24s go the right, while their indices are pushed down. Then index | 24 (mod) is calculated (for dividers this is 0). We repeat the corresponding index for i | 24 times, thus dividers get deleted. 5 on the other hand will be repeated 5 times. As we only want 1 of them, we take ! only 1 of them. The rest of the values build up and when nothing moves, the factory comes to a halt. Here is an ungolfed variant.

Python 3, 39 bytes

f=lambda x,i=1:i if x%i!=0else f(x,i+1)

Try it online!

This function just returns the second argument if it's a valid answer, and calls itself with an incremented second argument otherwise.

Perl 5 -p, 15 bytes

1until$_%++$\}{

Try it online!

Rust, 23 bytes

|n|(1..).find(|x|x%n>0)

Try it online!

A closure that iterates over all positive integers returning Some(n) where n is the first integer modulo input is greater than zero.

Powershell, 48 Bytes

function f($n){do{$y++;$d=$n%$y}while($d-eq0)$y}

Edited to be a function. I'm not really sure how I would golf the "function" bit, but I'm sure there's a way.

GolfScript, 15 bytes

~:x;1{).x\%!}do

Try it online!

~:x;              # Store the input in the variable x
    1             # Push 1
     {     !}do   # While it is zero
      )           # Go to the next number
       .x\%       # x mod the current number

Husk, 5 bytes

←`-NḊ

Try it online! The testing code prints the output for the first 100 terms. Note that the function runs in time proportional to the input value; the 1234567 case takes about 12 seconds on my machine.

W h d, 7 5 bytes

W is doing pretty great, given its lack of a break-out-of-loop instruction!

▲²!░╠

Explanation

% Since W doesn't support breaking out
% of loops based on a condition, (design
% preference: I don't like while loops):
% We need some reasoning. We know that 
% n and n+1 are always coprime with
% each other, so why not generate a
% range from 1 to n+1?
1+       % Add the inplicit input by 1
     W   % Generate range from 1 to input + 1
         % Keep everything that
         % fullfills the following condition
  bam    % b%a is not falsy (sorry, implicit input
         % isn't working properly)
         % Here, b is the input and a is the current item.

Flag:h % Output the first item of the output
```

05AB1E, 5 bytes

∞Ö0k>

Bugfix thanks to @Grimmy (for n=1) at no additional byte-cost, so it can be undeleted again.

Try it online or verify all test cases.

Explanation:

∞      # Push an infinite positive list: [1,2,3,...]
 Ö     # Check for each whether it evenly divides the (implicit) input-integer
  0k   # Get the first (0-based) index of 0 in this list of 0s/1s
    >  # Increase this index by 1 to make it 1-based
       # (after which this is output implicitly as result)

Perl 6, 13 bytes

{+(1...$_%*)}

Try it online!

Whitespace, 58 bytes

[S S S T    N
_Push_1][S N
S _Dupe_1][S N
S _Dupe_1][T    N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][S N
T   _Swap][N
S S N
_Create_Label_LOOP][S S S T N
_Push_1][T  S S S _Add][S T S S T   N
_Copy_0-based_1st_input][S T    S S T   N
_Copy_0-based_1st_n][T  S T T   _Modulo][N
T   S N
_If_0_Jump_to_Label_LOOP][T N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Integer input = STDIN as integer
Integer n = 1
Start LOOP:
  n = n + 1
  If(input modulo n == 0):
    Go to next iteration of LOOP
  Print n as integer to STDOUT
  (Implicitly terminate the program with an error because no exit is defined)

Example program flow (input 24):

Command  Explanation                   Stack        Heap      STDIN  STDOUT  STDERR

SSSN     Push 1                        [1]
SNS      Duplicate 1                   [1,1]
SNS      Duplicate 1                   [1,1,1]
TNTT     Read STDIN as integer         [1,1]        [{1:24}]  24
TTT      Retrieve from heap #1         [1,24]       [{1:24}]
SNT      Swap top two                  [24,1]       [{1:24}]
NSSN     Create Label LOOP             [24,1]       [{1:24}]
SSSTN     Push 1                       [24,1,1]     [{1:24}]
TSSS      Add top two (1+1)            [24,2]       [{1:24}]
STSSTN    Copy (0-based) 1st from top  [24,2,24]    [{1:24}]
STSSTN    Copy (0-based) 1st from top  [24,2,24,2]  [{1:24}]
TSTT      Modulo (24%2)                [24,2,0]     [{1:24}]
NTSN      If 0: Jump to Label LOOP     [24,2]       [{1:24}]

SSSTN     Push 1                       [24,2,1]     [{1:24}]
TSSS      Add top two (2+1)            [24,3]       [{1:24}]
STSSTN    Copy (0-based) 1st from top  [24,3,24]    [{1:24}]
STSSTN    Copy (0-based) 1st from top  [24,3,24,3]  [{1:24}]
TSTT      Modulo (24%3)                [24,3,0]     [{1:24}]
NTSN      If 0: Jump to Label LOOP     [24,3]       [{1:24}]

SSSTN     Push 1                       [24,3,1]     [{1:24}]
TSSS      Add top two (3+1)            [24,4]       [{1:24}]
STSSTN    Copy (0-based) 1st from top  [24,4,24]    [{1:24}]
STSSTN    Copy (0-based) 1st from top  [24,4,24,4]  [{1:24}]
TSTT      Modulo (24%4)                [24,4,0]     [{1:24}]
NTSN      If 0: Jump to Label LOOP     [24,4]       [{1:24}]

SSSTN     Push 1                       [24,4,1]     [{1:24}]
TSSS      Add top two (4+1)            [24,5]       [{1:24}]
STSSTN    Copy (0-based) 1st from top  [24,5,24]    [{1:24}]
STSSTN    Copy (0-based) 1st from top  [24,5,24,5]  [{1:24}]
TSTT      Modulo (24%5)                [24,5,4]     [{1:24}]
NTSN      If 0: Jump to Label LOOP     [24,5]       [{1:24}]

TNST      Print as integer             [24]         [{1:24}]         5
                                                                             error

Mathematica, 19 bytes (UTF-8 encoding)

1//.x_/;x∣#:>x+1&

Unnamed function taking a nonzero integer argument and returning a positive integer. The vertical bar about halfway through is actually the three-byte character U+2223, which denotes the divisibility relation in Mathematica. Explanation:

1                   Starting with 1,
 //.                apply the following rule until it stops mattering:
    x_                if you see a number x
      /;x∣#           such that x divides the function argument,
           :>x+1      replace it with x+1.
                &   Cool, that's a function.

Edited to add: ngenisis points out that //. will, by default, iterate a maximum of 65536 times. So this implementation works for all input numbers less than the least common multiple of the integers from 1 to 65538 (in particular, on all numbers with at most 28436 digits), but technically not for all numbers. One can replace x//.y with ReplaceRepeated[x,y,MaxIterations->∞] to fix this flaw, but obviously at the cost of 34 additional bytes.

Pyt, 11 bytes

Đ⁺ř|¬ĐŁř*ž↓

Explanation:

                       Implicitly takes input
Đ                      Duplicates input
 ⁺                     Increments by 1
  ř                    Push [1,2,...,input+1]
   |¬                  Does each of [1,2,...,input+1] not divide the input
     Đ                 Duplicate the array
      Ł                Get the length
       ř               Push [1,2,...,length]
        *              Multiply the top two array elementwise
         ž             Remove all zeroes
          ↓            Get the minimum
                       Implicit print

Try it online!

TI-Basic, 17 bytes

Absolutely genius to use the input as upper bound for for loop?

For(I,1,Ans
If not(fPart(Ans/I
End
I

Funky, 28 bytes

n=>fori=1i<n i++ifn%i breaki

Obvious solution, but also short enough for me.

Might be the first time I've used break instead of return competitively.

Try it online!

Octave/MATLAB, 26 24 bytes

@(n)find(mod(n,1:n+1),1)

find(...,1) returns the index (1-based) of the first nonzero element of the vector in the first argument. The first argument is [n mod 1, n mod 2, n mod 3, n mod 4,...,n mod (n+1)] That means we have to add +1 to the index, since we start testing at 1. Thanks @Giuseppe for -2 bytes.

Try it online!

Excel VBA, 36 Bytes

Immediates window function; Takes input from cell A1 and prints to the Immediates window.

i=2:While([A1]Mod i=0):i=i+1:Wend:?i

SNOBOL4 (CSNOBOL4), 57 bytes

F	F =F + 1
	GT(REMDR(N,F))	:S(RETURN)F(F)
	DEFINE('F(N)')

Try it online!

><>, 12 + 3 = 15 bytes

:l%?v:
;nll<

Try it online!

Takes input via the -v flag. Uses the length of the stack as a counter to check the divisibility of the number.

Cubix, 14 12 bytes

I2/L/);?%<@O

Saved 2 bytes thanks to MickyT.

Try it

Explanation

In cube form, the code is:

    I 2
    / L
/ ) ; ? % < @ O
. . . . . . . .
    . .
    . .

Basically, this just takes the input and starts a counter. It then checks each successive value of the counter until it finds one that isn't a factor of the input.

Ruby (C implementation), 26 bytes

->n{-(~n...0).min{|x|n%x}}

Explanation: ~n 2s-complement-bitwise-negates n, so it's equivalent for positive n to -(n+1). Let's work through with n=6. We generate a range from -7 to 0, excluding 0, and then find the minimum of that range using a custom comparison function. min expects a block that takes two arguments and returns a negative number if the first one is smaller, 0 if they're the same, and 1 if the first one is larger. Here, we're implicitly telling the block to ignore the second argument, so x is just the first element in the comparison. The block will return zero when x is the negation of a factor of n: in our n=6 example, 6%-3 is 0. When it's not, it'll be a negative number: 6%-4 is -2.

So as Ruby iterates through the range, it'll do this: -7 is the first candidate for a minimum. Is -6 less than it? It passes -6 (x) and -7 (ignored) into the block, and the block returns 0 (since 6 is a factor of 6), so no. Is -5 less than it? It passes -5 in to the block and it returns a negative number, so yes. So the current candidate is -5. Is -4 less than -5? The block says yes. -3, -2, and -1 all get 0s, so -4 wins.

Finally we negate the result to get 4.

I'm calling this implementation-dependent since the Ruby spec would allow the sorting to be done differently, although my guess is most implementations do it this way.

Jelly, 6 bytes

R‘ḟÆDḢ

Try it online!

Explanation:

R‘ḟÆDḢ Example input: 5
R      Range 1 to implicit input. [1,2,3,4,5]
 ‘     Increment. [2,3,4,5,6]
  ḟ    Filter out...
   ÆD  The divisors. [2,3,4,6]
     Ḣ Get the first one. 2
       Implicit output

Add++, 16 bytes

L,RqVAFB]qG$_bUm

Try it online!

How it works

L,		; Create a lambda function
		; Example argument: [24]
	R	; Range;            [[1 2 3 4 ... 22 23 24]]
	q	; Set;              [{1 2 3 4 ... 22 23 24}]
	V	; Save and pop;     []
	AFB]q	; Push the factors; [{1 2 3 4 6 8 12 24}]
	G	; Retrieve;         [{1 2 3 4 6 8 12 24} {1 2 3 ... 22 23 24}]
	$_h	; Set difference;   [{5 7 9 ... 22 23 24}]
	bUm	; Minimum;          5

Forth (gforth), 31 bytes

: f 1 begin 1+ 2dup mod until ;

Try it online!

Explanation

 1        \ place a 1 on the stack (divisor)
 begin    \ start an indefinite loop
 1+       \ add 1 to the divisor
 2dup     \ duplicate the top 2 stack items (n and divisor)
 mod      \ get n%divisor and place it on top of the stack
 until    \ end the loop when the top of the stack is anything other than 0

Julia 0.6, 25 bytes

f(n,x=2)=n%x>0?x:f(n,x+1)

Try it online!

FALSE, 22 19 18 bytes

1[$$3ø\/*2ø=][1+]#

This is a modified version of the other FALSE answer (which is itself a modified version of the DUP answer)

1[[\$@$@$@$@\/*-0=][1+]#.]a:\a;!

First, of course, we can remove that function definition/call, as well as outputting the number to the stack instead of printing:

1[\$@$@$@$@\/*-0=][1+]#

\$@$@$@$@\ is used to create 2 copies of the top 2 values on the stack {a,b} -> {a,b,a,b,a,b}. This can be shortened to 1ø1ø1ø1ø or $2ø\$2ø\

1[$2ø\$2ø\/*-0=][1+]#

For some reason, they are using a-b==0 rather than just a==b...

1[$2ø\$2ø\/*=][1+]#

And then I messed around some more to save 1 character:

1[$$3ø\/*2ø=][1+]#

C (gcc) 30 bytes

i;f(n){for(i=0;n%++i<1;);i=i;}

Try it online!

Japt, 5 bytes

@uX}a

Try it here

Emojicode, 74 bytes

🐖🔢➡️🚂🍇🍮i 1🔁😛0🚮🐕i🍇🍮i➕1i🍉🍎i🍉

Try it online!

SmileBASIC, 46 bytes

INPUT N
FOR I=2TO N+1
IF N MOD I THEN ?I:Q
NEXT

I hope that triggering an error to end the program is allowed.

Java 7, 54 51 49 47 46 bytes

Golfed:

int m(int x){int i=1;for(;x%++i<1;);return i;}

Ungolfed:

int m(int x)
{
    int i = 1;
    for (; x % ++i < 1;);
    return i;
}

Nothing fancy... I did try with a while(1>0) loop, was 2 bytes longer

Labyrinth, 13 bytes

+:#
" %#!
?:;

Try it online!

Explanation

To avoid shifting around too many values to keep track of both the input and the current potential divisor, we're storing the latter implicitly as the stack depth by creating copies of the input. The 3x3 block on the left is the main loop. The first iteration doesn't really do anything, but it helps with the overall layout to delay reading the input until later.

+   Add top two stack elements. Does nothing on the first two iterations,
    but removes a zero on subsequent iterations.
:   Duplicate. Does nothing on the first iteration, but copies the input
    later on.
#   Push the stack depth. 2 on the first two iterations, increasingly larger
    values later on.
%   Modulo. Gives zero on the first iteration, and acts as the trial division
    later on. If this is positive, the loop is exited.
;   Discard the zero.
:   Duplicate a zero on the first iteration, the input on subsequent iterations,
    increasing the stack depth.
?   Read input on first iteration, push zero later on.

Once % gives a positive value, we've found a non-divisor of the input. # pushes the stack depth once more, ! prints it. Then the IP hits a dead and turns around. Now #% will leave the top of the stack unchanged (and positive), so the IP now enters the 3x3 main loop in counter-clockwise order. #:+ pushes twice the stack depth but that's irrelevant. ?:; all together push a single zero so that the % now terminates the program due to a division by zero.

Clojure, 90 bytes

Horribly long, but I like how it reads ungolfed.

#(apply min(remove(into #{}(filter(fn[m](=(rem % m)0))(range 1(+ % 1))))(range 2(+ % 2))))

I probably have a lot of room to golf this given this was a totally naïve attempt at an algorithm.

Ungolfed:

(defn smallest [n]
  (let [mults (into #{} (filter (fn [m] (= (rem n m) 0)) (range 1 (+ n 1))))] ; Find multiples, and place in a set for membership lookup
    (apply min ; Find minimum non-multiple
      (remove mults ; Remove multiples from the range 2 to (n+2)
              (range 2 (+ n 2))))))

S.I.L.O.S, 49 bytes

readIO
lbla
x+1
b=i
b%x
a=1
a-b
if a a
printInt x

Try it online!
Fairly simple. I was confused for a little while.

GameMaker Language, 58 bytes

a=argument0 for(i=1;i<=a;i++){if a mod i return i}return i

Ruby, 28 bytes

->n{(1..n+1).find{|x|n%x>0}}

Q/KDB+ 36 Bytes

f:{$[0=(y%x)mod 1;f[x+1;y];x]}
f[1;n]

Description:
Function f

f:{$[0=(y%x)mod 1;f[x+1;y];x]}

which consists of a conditional

$[0=(y%x)mod 1;f[x+1;y];x] 

The conditional has 3 statements, each separated by a semi colon.

0=(y%x)mod 1  //y divided by x, mod 1 to determine if it is a decimal or not.
f[x+1;y]      //If it is not a decimal, call the function again and increment x.
x             //If it is a decimal, return x (as it doesn't divide n evenly.

To call the function:

f[1;n]

Start at 1 and pass in the value of n.

PHP, 30 bytes

for(;$argv[1]%++$i<1;);echo$i;

if run from console with -r option (thx to @ais523)

php -r 'for(;$argv[1]%++$i<1;);echo$i;' 232792560

32 bytes

<?for(;$argv[1]%++$i<1;);echo$i;

thanks to @manatwork for removing 1 byte

33 bytes (original)

<?for(;$argv[1]%++$i==0;);echo$i;

C++ 55 Bytes

start: while(n%i>0) {;goto end;} i++; goto start; end:;

The while loop checks whether the number(input given by the user) is divisible by i(here the divisor which is continuously increasing).

The entire code is

#include<iostream>
using namespace std;
int main(){
int n,i=1;
cout<<"Enter A Number";
cin>>n;
start:
while(n%i>0)
{
cout<<"The lowest integer that does not divide "<<n<<" is "<<i;
goto end;
}
   i++;
goto start;
  end:
return 0;
}

Perl, 25 bytes

sub{1 until$_[0]%++$i;$i}

dc, 21 bytes

?sn1[1+dlnr%0=b]dsbxp

Try it online!

The program works by running through all integers starting with 2 until it finds one that isn't a divisor of the input. The input is kept in register n, and the current number being tested as a divisor/non-divisor is on the stack.

Scala, 33 bytes

(n:Int)=>(1 to n+1)find(n%_>0)get

Straightforwardly constructs a Range that covers the possible answers, finds the first one that is an answer, and gets it from the Option, knowing that it must exist.

C, 30 bytes

Recursion is your friend:

f(x,i){return x%i?i:f(x,i+1);}

Call as follows:

#include <stdio.h>

f(x,i){return x%i?i:f(x,i+1);}

int main() {
    for(int i = 1; i < 10; i++) {
        printf("f(%d, 1) = %d\n", i, f(i, 1));
    }
}

Perl, 19 bytes

18 bytes of code + -p flag.

$_=$_%++$.?$.:redo

To run it:

perl -pE '$_=$_%++$.?$.:redo' <<< 12252240

Not very detailed explanations:
- $. is a special variable whose default value is the current line number of the last filehandle accessed (stdin here), so after reading the first line of input, it's set to 1.
- $_ holds the input and is implicitly printed at the end (thanks to -p flag).
- redo (in that context) considers that the program is in a loop and redo the current iteration (only $. will be different since it got incremented).
- So if we found the smallest number (stored in $.) that doesn't divide $_, then we set $_ to it, otherwise, we try the next number (thanks to redo).

Java 8, 37 bytes

n->{int i=1;while(n%++i<1);return i;}

Vitsy, 17 bytes

V2v[VvDD1+v{M(x&]

The output is the exit code of this program. Assuming that less that LCM(Range(1,128)) is acceptable range.

Try it online!

Explanation:

Let [...] at the end of the line signify the state of the stack at that line.

V2v[VvDD1+v{M(x&]
V                 Capture the input as a final global variable (FGV).
 2v               Save 2 as a temporary variable.
   [            ] Repeat the item in brackets forever.
    V             Push the FGV to the stack.
     v            Dump the temporary variable (TV) to the stack. (call it "x")
      DD          Duplicate twice. The stack looks like [FGV, x, x, x].
        1+        Add one to the top value. [FGV, x, x, x+1]
          v       Capture the top value as the new temp var. (TV = x+1) [FGV, x, x]
           {      Rotate the top to the bottom of the stack. [x, FGV, x]
            M     Pop the top two values, push second-to-top mod top. [x, FGV%x]
             (    Pop the top value. If it is zero, then... [x]
              x   Pop the top value and exit with that exit code. []
               &  Enter a new stack. (Reset the stack).

REXX, 35 bytes

arg a
do n=1 until a//n>0
end
say n

Java 8, 44 + 2 = 46 bytes

int m(int x,int y){return x%++y>0?y:m(x,y);}

Recursive solution which requires an extra ,1 when calling. Call with m(x,1).

JavaScript (ES6), 25 23 bytes

f=(n,k)=>n%k?k:f(n,-~k)

Note: One interesting thing here is that the k parameter is initialized ex nihilo on the first iteration. This works because n % undefined is NaN (falsy as expected) and -~undefined equals 1. On the next iterations, -~k is essentially equivalent to k+1.

Test

f=(n,k)=>n%k?k:f(n,-~k)

// first 100 terms
for(i = 1, list = []; i <= 100; i++) {
  list.push(f(i));
}
console.log(list.join(' '));

// larger test cases
console.log(f(1234567));
console.log(f(12252240));
console.log(f(232792560));

Japt, 8 bytes

U%°V?V:ß

This was inspired by Arnauld's solution.

Thanks ETHproductions for golfing this even more!

Try it Online!

Matlab, 45 Bytes

function x=n(a)
x=2;while(~mod(a,x))x=x+1;end

DUP, 23 bytes (21 chars)

DUP is a derivative of FALSE, with a similar solution.

1[[^^/%0=][1+]#.]⇒a\a

Insert the value to be tested between the first a and the \, like this:

1[[^^/%0=][1+]#.]⇒a1234567\a

Explanation:

                         data
                         stack
1                        [1]                PUSH 1 on data stack
 [              ]⇒a                         define operator a
                   n     [1,n]              PUSH integer n on data stack
                    \    [n,1]              SWAP
                     a                      execute operator a
1[[^^/%0=][1+]#.]⇒a1234567\a
  [      ][  ]#                             while loop: while first block true,
                                                        execute second block
  [               ~1~
   ^                     [n,1,n]            OVER
    ^                    [n,1,n,1]          OVER
     /                   [n,1,n%1,n/1]      MODDIV: computes mod and division
      %                  [n,1,n%1]          POP
       0                 [n,1,n%1,0]        PUSH 0
        =                [n,1,true/false]   n%1 == 0 ?
         ]                                  if false (0), continue at ~2~
                                            if true (-1), execute next block
          [
           1             [n,1,1]            PUSH 1
            +            [n,2]              ADD
             ]#                             return to ~1~
               .  ~2~    [n]                print number to STDOUT
                                            exit operator a, end program

Try it out here.

Or clone my DUP GitHub repository for a DUP interpreter written in Julia, with full documentation.

FALSE, 32 bytes

This FALSE solution is similar to the DUP solution because DUP is a dialect of FALSE with some extra abilities.

1[[\$@$@$@$@\/*-0=][1+]#.]a:\a;!

In contrast to DUP, FALSE lacks the convenient MOD,DIV operator that computes both the MOD and DIV of two numbers at one time. FALSE only offers an integer division operator /. FALSE also does not offer the convenient OVER operator, and using the PICK operator combo instead costs 3 bytes each. So, the solution above is actually the cheapest way to implement the OVER and MOD operators in FALSE.

Insert the value to be tested between the : and the \, like this:

1[[\$@$@$@$@\/*-0=][1+]#.]a:1234567\a;!

Explanation:

1[[\$@$@$@$@\/*-0=][1+]#.]a:n\a;!   (n marks the number)

1                                 [1]               PUSH 1 (counter c)
 [                       ]a:                        define function a
                            n     [1,n]             PUSH n
                             \    [n,1]             SWAP
                              a;!                   fetch address of a, execute a
  [               ][ ]#                             while loop while 1st block true,
                                                                execute 2nd block
   \                       ~1~    [1,n]             SWAP
    $                                               DUP
     @                                              ROT
      $@$@$@                      [n,1,n,1,n,1]     DUP,ROT,DUP,ROT,DUP,ROT,SWAP
                                                    (create 2 duplicates of the pair)
            /                     [n,1,n,1,n/1=n]   DIV
             *                    [n,1,n,1*n]       MUL
              -                   [n,1,n-n=0]       SUB
               0                  [n,1,0,0]         PUSH 0
                =                 [n,1,-1]          if 0==0 PUSH true(-1),
                                                    else push false (0)
                                                    if true, execute next block,
                                                    if false, continue at ~2~
                 ]
                  [1+]#           [n,2]             PUSH 1, ADD, (increment c),
                                                    loop back to ~1~
                  
                       .  ~2~     [n,c]             print integer c (top stack value) to STDOUT

Try it out here.

Retina, 28 bytes

.+
$*11
(1+?)(?!1\1*$).*
$.1

Try it online!

Thanks to Martin for 6 bytes!

In the first stage we generate N + 1 1s. Then we find the smallest number of ones such that we cannot fit that number evenly into N by hard-coding the offset by one that we introduced in the first step. This offsetting is used to allow 1 and 2 to work.

Beeswax, 19 bytes

 >~P~q
{~b"%g<~1fT_

Try it online!

Example, using 3 as value

                  lstack     gstack  print
           _      [0,0,0]    []            create bee
          T       [0,0,3]                  enter number
         f                   [3]           push top lstack value on gstack
        1         [0,0,1]                  push 1 on lstack
       ~          [0,1,0]                  swap lstack 1st and 2nd
     g<           [0,1,3]                  push gstack 1st on lstack
    %             [0,1,0]                  lstack 1st = 1st % 2nd
   "                                       if lstack 1st > 0 skip next, else don’t skip
  b                                        redirect to upper left
 >                                         redirect to right
  ~               [0,0,1]                  swap lstack 1st and 2nd
   P              [0,0,2]                  increment lstack 1st
    ~             [0,2,0]                  swap lstack 1st and 2nd
     q                                     redirect to lower right
      <                                    redirect to left
     g            [0,2,3]                  push gstack 1st on lstack
    %             [0,2,1]                  lstack 1st = 1st % 2nd
   "                                       lstack 1st > 0 → skip next
 ~                [0,1,2]                  swap lstack 1st and 2nd
{                                    "2"   print lstack 1st to STDOUT
                                           end program

Batch, 53 bytes

@set/ad=%2+1,r=%1%%d
@if %r%==0 %0 %1 %d%
@echo %d%

Well I beat Java anyway... Explanation:

Befunge, 27 25 24 bytes

&:1>:00p%v
g1+^@.g00_:00

Try it online!

Explanation

&                 Read N from stdin.
 :                Save a duplicate copy.
  1               Push initial test divisor, D.

   >              Main loop starts here.
    :00p          Save a copy of the current D.
        %v        Calculate N modulo D and move down.
         _        If not zero (i.e. N is not divisible by D), then break to the left.
          :       Otherwise continue to the right and prepare another copy of N.
g          00     Retrieve the previously saved D (wrapping to the beginning of the line).
 1+               Increment D.
   ^              Repeat the loop again.

         _        We break out of the loop going left.
      g00         Retrieve the last value of D.
    @.            Write it to stdout and exit.

Thanks to Mistah Figgins for saving me a byte.

PowerShell, 26 bytes

for(;!("$args"%++$i)){};$i

Try it online!

Clojure, 48 Bytes

(defn f[n](some #(if(>(mod n %)0)%)(range 1 n)))

COW, 174 bytes

oomMOOMMMmoOmoOmoOMMMmOomOoMoOMMMmoOmoOmoOMMMmOoMOOmoO
MOomoOMoOmOoMOOmoOmoomoOMOOmOoMoOmoOMOomoomOomOoMOOmOo
moomoOMOomoomoOmoOMOOmOomOomOomOoOOMOOOMOomOOmoomOomOo
mOomOomOomoo

Try it online!

This code is only partially my own -- it implements a modulus algorithm that I ported from brainfuck. The rest of the code is my own. However, since I did not write the modulus algorithm, I haven't truly investigated how it works and cannot document that part of the code. Instead, I'll give my usual breakdown, followed by a more in-depth explanation of why the code works.

Code breakdown

oom                          ;Read input into [0].
MOO                          ;Loop while [0].  We never change [0], so the program only terminates forcibly after a print.
  MMMmoOmoOmoOMMMmOomOo      ; Copy [0] to [3] and navigate to [1].
  MoOMMMmoOmoOmoOMMM         ; Increment [1], and copy it to [4]
  mOo                        ; Navigate back to [3].
  MOO                        ; Modulus algorithm.  Direct port of brainfuck algorithm.
    moOMOomoOMoOmOo
    MOO
      moO
    moo
    moO
    MOO
      mOoMoOmoOMOo
    moo
    mOomOo
    MOO
      mOo
    moo
    moOMOo
  moo                        ; End modulus algorithm.
  moOmoO                     ; Navigate to [5].  This contains our modulus.
  MOO                        ; Only perform these operations if [5] is non-zero -- i.e. [0] % [1] != 0
    mOomOomOomOoOOMOOOMOomOO ;  Navigate to [1], print its contents, then error out.
  moo                        ; End condition
  mOomOomOomOomOo            ; Since we're still running, [0] % [1] == 0, so navigate back to [0] and try again.
moo                          ;End main loop.

Explanation

The code first reads the integer into [0]. Each iteration of the main loop (lines 2 through 26) increments [1], then copies everything necessary over to the modulus algorithm, which spits out its result into [5]. If [5] contains any value, then [1] is the number we need to print. We print it, and then force-quit the program.

Since COW is a brainfuck derivative, it functions relatively similar to the way brainfuck operates -- infinite strip of tape, you can move left or right, increase or decrease, and "loop" while the current tape value is non-zero. In addition to brainfuck, COW comes with a couple of useful features.

(0) moo -- Equivalent to ]
(1) mOo -- Equivalent to <
(2) moO -- Equivalent to >
(3) mOO -- No equivalent.  Evaluate current tape value as instruction from this list.
(4) Moo -- If tape is 0, equivalent to ,; if tape is non-zero, equivalent to .
(5) MOo -- Equivalent to -
(6) MoO -- Equivalent to +
(7) MOO -- Equivalent to [
(8) OOO -- No equivalent.  Set tape (positive or negative) to 0
(9) MMM -- No equivalent.  If register is empty, copy tape to register.  If register is non-empty, paste register to tape and clear register.
(10) OOM -- No equivalent.  Print an integer from tape to STDOUT
(11) oom -- No equivalent.  Read an integer from STDIN and store it on tape

The real point of interest here is instruction 3, mOO. The interpreter reads the current tape value, and executes an instruction based on that tape value. If the value is less than 0, greater than 11, or equal to 3, the interpreter terminates the program. We can use this as a quick-and-dirty force quit of the main loop (and the program entirely) once we've found our non-divisor. All we have to do is print our number, clear [1] (with OOO), decrement it to -1 with MOo, and then execute instruction -1 via mOO which ends the program.

The tape itself for this program functions as follows:

[0]  -- Read-in integer from STDIN.
[1]  -- Current divisor to test
[2]  -- Placeholder for modulus algorithm
[3]  -- Temporary copy of [0] for use for modulus algorithm
[4]  -- Temporary copy of [1] for use for modulus algorithm
[5]  -- Placeholder for modulus algorithm.  Location of remainder at end of loop.
[6]  -- Placeholder for modulus algorithm
[7]  -- Placeholder for modulus algorithm

The modulus algorithm naturally clears [2], [3], [6], and [7] at the end of the operation. [4]'s contents get overwritten with the register paste on line 4, and [5] is zero when [0] is divisible by [1], so we don't have to clear it. If [5] is non-zero, we force-quit on line 23 so we don't have to worry about it.

Jelly, 5 bytes

‘ḍ€i0

Try it online!

How it works

‘ḍ€i0  Main link. Argument: n

‘      Increment; yield n+1.
 ḍ€    Divisible each; test 1, ..., n+1 for divisibility by n.
   i0  Find the first index of 0.

bash, 37 bytes

(($1%${2-1}))&&echo $2||$0 $1 $[$2+1]

Save the program in a file. Run it from the command line with the input number N as an argument:

scriptname N

QBIC, 14 bytes

:[a+1|~a%b|_Xb

Explanation:

:      Read the first cmd line param as a number, called 'a'
[a+1|  FOR (b=1 ; b <= a+1; b++) <-- a+1 for cases a = 1 or 2
~a%b   IF A modulo B ( == 0, implicit)
|_Xb   THEN exit the program, printing b
       [IF and FOR implicitly closed by QBIC]

MATL, 6 bytes

t:\f1)

Try it online!

05AB1E, 6 bytes

ÌL¹ÑK¬

Try it online!

Also, it spells "LINK!"... Kinda...

ÌL     # Push [1..n+2]
  ¹Ñ   # Push divisors of n.
    K¬ # Push a without characters of b, and take first item.

Perl 6, 17 bytes

{first $_%*,1..*}

Try it

Expanded:

{  # bare block lambda with implicit parameter 「$_」

  # return the first value
  first

  # where the block's argument 「$_」 modulus the current value 「*」
  # doesn't return 0 ( WhateverCode lambda )
  $_ % *,
  # ( 「$_ !%% *」 would be the right way to write it )

  # from 1 to Whatever
  1 .. *
}

Pyke, 5 bytes

1D.f%

Try it here!

1D.f  - first number after 1 where
    % -  i%input != 0

C, 32 35 bytes

i;f(x){for(i=1;x%++i<1;);return i;}

Edit: added i=1 in the loop

Usage

main(c,v)char**v;{printf("%d",f(atoi(*++v)));}

Full Program version, 64 Bytes:

main(c,v)char**v;{*++v;for(c=1;atoi(*v)%++c<1;);printf("%d",c);}

Jellyfish, 12 10 bytes

p\~~|1
 >i

Takes input from STDIN and outputs to STDOUT. Try it online!

Martin Ender saved 2 bytes, thanks!

Explanation

 \~~|
 >i

This part is one function that uses the input value in its definition.

   ~|

This ~-cell is given a function, so it flips its arguments: its produces the binary function "left argument modulo (|) right argument". The built-in modulo function in Jellyfish takes its arguments in the reverse order.

  ~~|
  i

This ~-cell is given a value and a function, so it does partial application: it produces the binary function "input (i) modulo right argument". Let's call that function f.

 \~~|
 >i

The \-cell is given two functions, so it does iteration: it produces the unary function "increment (>) until the function f applied to previous and current values gives a truthy (nonzero) result, then return current value". This means that the argument is incremented until it doesn't divide the input.

p\~~|1
 >i

Finally, we apply this function to the initial value 1 and print the result with p.

Racket 52 bytes

(let p((n 2))(cond[(= 0(modulo m n))(p(+ 1 n))][n]))

Ungolfed:

(define (f m)
  (let loop ((n 2))
    (cond
      [(= 0 (modulo m n))
       (loop (+ 1 n))]
      [else n])))

Testing:

(f 24)
(f 1234567)
(f 12252240)
(f 232792560)

Output:

5
2
19
23

julia, 28 bytes

N->findfirst(x->N%x>0,1:N+2)

Note: since 1:N+2 doesn't allocate memory there is no memory problems for large N s
- @flawr N+2 save for me some bytes
- @Martin 's suggestion saved 1 bytes

Haskell, 26 bytes

f n=until((>0).mod n)(+1)1

Everyone forgets about until!

Python 2.7.9, 32 bytes

f=lambda n,d=1:n%d>0or-~f(n,d+1)

Test on Ideone

Recursively counts up potential non-divisors d. It's shorter to recursively the increment the result than to output d. An offset of 1 is achieved by the Boolean of True, which equals 1, but since d==1 is always a divisor, the output is always converted to a number.

Python 2.7.9 is used to allow allow 0or. Versions starting 2.7.10 will attempt to parse 0or as the start of an octal number and given a syntax error. See this on Ideone.

Brachylog, 10 bytes

~{=#>:A'*}

Try it online!

This came out very similar to (but shorter than) Fatalize's original solution. Fatalize has since switched to a different algorithm that ties with this one via a different method, so I'm going to have to explain it myself:

~{=#>:A'*}
~{       }    inverse of the following function:
  =           try possible values for the input, if it's unbound
   #>         the input is a positive integer
     :A'*     there is no A for which the input times A is the output

When we invert the function, by swapping "input" and "output", we get a fairly reasonable algorithm (just expressed in an awkward way): "try possible positive integers, in their natural order (i.e. 1 upwards), until you find one that can't be multiplied by anything to produce the input". Brachylog doesn't do floating-point calculations unless all inputs are known, so it'll only consider integer A.

Brachylog, 11 10 bytes

,.=:?r'%0'

Try it online!

Explanation

,.=           Assign an integer to the output
 . :?r'%0     Input mod Output ≠ 0
        0'    Output ≠ 0

Dyalog APL, 8 bytes

1⍳⍨0≠⍳|⊢

1⍳⍨ position of first True in

0≠ the non-zero values of

⍳| the division remainders of 1...N when divided by

N

TryAPL online!

Note: this works for 1 and 2 because 1⍳⍨ returns 1 + the length of its argument if none is found.

C 70 bytes

 s(int *n){ int f=1;for(f=1;f<(*n);f++){if((*n)%f!=0)return f;}return f;}

Jelly, 5 bytes

1%@#Ḣ

Try it online!

Explanation:

1%@#Ḣ
1  #      Find the first … numbers, counting up from 1, such that
 %@       dividing those numbers into … gives a truthy remainder
    Ḣ     then return the first

This is a horrendous abuse of #; there are plenty of operators in this program, but a ton of missing operands. # really wants the 1 to be given explicitly for some reason (otherwise it tries to default to the input); however, everything else that isn't specified in the program defaults to the program's input. (So for example, if you give 24 as input, this program finds the first 24 numbers that don't divide 24, then returns the first; kind-of wasteful, but it works.)

Haskell, 29 bytes

f n=[k|k<-[2..],mod n k>0]!!0

The expression [k|k<-[2..]] just creates an infinite list [2,3,4,5,...]. With the condition mod n k>0 we only allow those k in the list that do not divide n. Appending !!0 just returns the first entry (the entry at index 0) form that list.

Try it online!

R, 28 bytes

Pretty straightforward, nothing fancy. Takes input from stdin, increments value T until i modulo T is nonzero.

i=scan()
while(!i%%T)T=T+1
T

If you want something a little more fancy, there's the following for 29 bytes:

i=scan()
match(0,!i%%1:(i+1))

Explained:

i=scan() : Read i from stdin.

1:(i+1) : Generate all integers from 1 to i+1 (the +1 accounting for the cases of 1 and 2).

i%%1:(i+1) : Modulo the input by every number in our list.

!i%%1:(i+1) : Negate the resulting list; this implicitly converts it to a logical type, such that 0 is FALSE and nonzero is TRUE. After negating, TRUE values become FALSE and vice-versa. Now, all originally nonzero values are coded as FALSE.

match(0,!i%%1:(i+1)) : Return the index of the first instance of 0 in our list. 0 is FALSE, so this returns the index of the first FALSE in the list, which is the first nonzero value from the modulo operation. Since our original list began at 1, the index is equal to the value of the smallest non-divisor.

Actually, 7 bytes

;÷@uR-m

Try it online! (note: this is a very slow solution, and will take a long time for large test cases)

Explanation:

;÷@uR-m
;÷       duplicate N, divisors
  @uR    range(1, N+2)
     -   set difference (values in [1, N+1] that are not divisors of N)
      m  minimum

Pyth, 3 bytes

f%Q

Basically, f loops the code until %QT (Q % T where T is the iteration variable) is true.

Try it online here.

Hexagony, 12 bytes

\\)?}'@{!%.}

Embiggened:

   \ \ )
  ? } ' @
 { ! % . }
  . . . .
   . . .

Try it online!

><>, 15 +3 = 18 bytes

1\n;
?\1+:{:}$%

Input is expected to be on the stack at program start, so +3 bytes for the -v flag. Try it online!

C#, 39 37 bytes

n=>{int i=0;while(n%++i<1);return i;}

Saved two bytes thanks to Martin!

Jelly, 6 bytes

%R;‘TḢ

Try it online!

Explanation:

                                               Assume 24 is our N
 R      Generate all numbers from 1 to N         [1, 2, 3, 4 .., 24]
  ;‘    Attach N+1 to that list (for cases 1,2)  [1, 2, 3, 4 .., 25]
%       And modulo-divide our input by it
        Yields a list with the remainder         [0, 0, 0, 0, 4 ...]
    T   Return all thruthy indexes               [5, 7, ...]
     Ḣ  Takes the first element of that list -->  5

Python, 43 36 35 bytes

f=lambda n,d=2:d*(n%d>0)or f(n,d+1)

05AB1E, 7 bytes

Xµ¹NÖ_½

Try it online!

Explanation

Xµ       # run until counter is 1
  ¹      # push input
   N     # push iteration counter
    Ö_   # push input % iteration counter != 0
      ½  # if true, increase counter
         # output last iteration