g | x | w | all
Bytes Lang Time Link
001Arsla250604T043653ZAitzaz
069Regex 🐇 PCRE2 v10.35 or later220725T203304ZDeadcode
003Thunno 2230827T170742ZThe Thon
003Nekomata 1230827T170620ZThe Thon
005Vyxal230502T131033Zlyxal
002Vyxal230502T125726Zemirps
176Java210210T175224ZJames Br
003Husk210208T225552ZDominic
005Pyth210208T225343Zhakr14
00205AB1E210208T224519ZMakonede
006Japt190301T203820ZOliver
032Python + primefac170130T154654Zmbomb007
nanRuby170202T000312ZValue In
268Java 7170130T163016ZKevin Cr
876Lua170130T221924ZATaco
132SimpleTemplate170130T191511ZIsmael M
nanPerl170130T181339ZZaid
13305AB1E170130T155522ZMagic Oc
043Haskell170129T172137ZWill Nes
028Python with sympy170129T123723ZJonathan
004J170129T163546ZConor O&
074Python 2170129T113849Zovs
007Pyke170129T103935ZBlue
025Perl 6170129T153936Zsmls
003MATL170129T152210ZSuever
002Brachylog170129T140336ZFatalize
045Python 3170129T134420ZDennis
052Bash + coreutils170129T124503ZMitchell
083Python170129T104918Zsagiksp
002Oasis170129T120609ZAdnan
061JavaScript ES7170129T111740ZLuke
034QBIC170129T110226Zsteenber
009Mathematica170129T104506ZGreg Mar
010Maxima170129T104427Zrahnema1
002Jelly170129T104042Zsteenber

Arsla, 1 Byte

P

If a number is stored into last element of stack, Arsla calls the next prime number and updates the stack element. Arsla calling the next prime

Regex 🐇 (PCRE2 v10.35 or later), 80 77 69 bytes

^(?=(?*x(x*))(?!(?*(xx+)(x+$))(?=\2*(x+)).*(?=\3\4$)\1\2*$))\1x+|\B|$

Attempt This Online!

Takes its input in unary, as a string of x characters whose length represents the number. Returns its output as the number of ways the regex can match. (The rabbit emoji indicates this output method.)

This is the first 🐇-regex I've written that can be really said to compute its result before turning it into a number of possible matches. This output method allows actually directly returning the result; before adopting this output method, it was impossible for a regex to return a value greater than the input.

The regex computes \$p-n\$, where \$n\$ is the input and \$p\$ is the next prime. To do this it takes advantage of the fact that there will always be a prime in the interval \$(n,2n]\$. Once that is complete, it outputs \$p-n+n=p\$ in the form of a number of possible matches.

    ^                    # tail = N = input number
    (?=
        (?*              # Non-atomic lookahead (try all options until matching)
            x(x*)        # \1 = N - (P - N), for conjectured P > N
        )
        (?!              # Negative lookahead - assert that this cannot match
            (?*          # Non-atomic lookahead (try all options until matching)
                (xx+)    # \2 = try all numbers from 2 through N-1
                (x+$)    # \3 = N - \2; assert \2 < N
            )
            (?=          # Atomic lookahead (locks onto the first match)
                \2*(x+)  # \4 = N % \2, giving \2 instead of 0
            )
            .*(?=\3\4$)  # tail = \3 + \4
            \1           # tail -= \1
            \2*$         # Assert tail is divisible by \2
        )
    )
    \1                   # tail = tail-\1 = P - N
    x+                   # add tail to the number of possible matches
|
    \B                   # add abs(N-1) to the number of possible matches
|
    $                    # add 1 to the number of the possible matches; this gets
                         # us a return value of 2 for N==0, instead of 0 which we
                         # would have if simply adding N instead of abs(N-1)+1

Molecular lookahead (?*...) is used for convenience. It would be possible to port to flavors that don't have it, but the regex would be much longer – it would still need to emulate arithmetic in the range \$[0,2n]\$, but would need to do so within just \$[0,n/4]\$ or perhaps \$[0,n/3]\$, instead of \$[0,n]\$ as this version does.

Without returning the correct value for \$0\$, this would be 65 bytes.

Regex 🐘 (.NET), 79 77 71 65 bytes

(?=(x|^)+)(?<1>x|^)+?(x*$)(?<!(?=\4*(?<=(?=\2\4*$)\3))(^x+)(x+x))

Try it online!

Returns its output as the capture count of group \1.

                        # tail = N = input number
(?=(x|^)+)              # \1.captureCount = {N, if N>0, else 1}
(?<1>x|^)+?             # Assert N > 0; \1.captureCount += {N - P, if N>0, else 1}
(x*$)                   # \2 = N - (N - P) = 2*N - P
(?<!                    # Right-to-left negative lookbehind
    (?=                 # Atomic lookahead
        \4*             # tail = Z = N % \4, giving \4 instead of 0
        (?<=            # Right-to-left atomic lookbehind
            (?=         # Atomic lookahead
                \2\4*$  # Assert tail-\2, that is, \3+Z-\2, is divisible by \4
            )
            \3          # tail += \3
        )
    )
    (^x+)               # \3 = N - \4; assert \4 < N
    (x+x)               # \4 = try all numbers from 2 through N-1
)

Without returning a value for \$0\$, this would be 61 bytes.

Thunno 2, 3 bytes

µƘP

Try it online!

Explanation

µƘP  # Implicit input
µƘ   # Find the next number after the input
  P  # which is a prime number
     # Implicit output

Nekomata -1, 3 bytes

Ƥ$>

Attempt This Online!

Explanation

Ƥ$>  # Implicit input
Ƥ    # Find the first prime
  >  # which is greater than
 $   # the input integer
     # Implicit output

Vyxal, 40 bits1, 5 bytes

{›:æ¬|

Try it Online!

Non-built-in answer, but does use a prime check built-in

Explained

{›:æ¬|
{    | # While
 ›     # incrementing the top of the stack
  :æ¬  # gives a non prime number:
       #  do nothing. just run the condition actually.

Vyxal, 52 bits1, 6.5 bytes

{›:KḢ₃¬|

Try it Online!

Uses KḢ₃ as a prime check (len(factors(x)[1:] == 1)

Vyxal, 2 bytes

∆Ṗ

Try it Online!

Here for completeness. Like answers in many other golfing langs, ∆Ṗ is simply a builtin for "next prime greater than n". It uses sympy.nextprime from Python's sympy library to do this

Java, 176 Bytes

import java.math.*;import java.util.*;class a{public static void main(String[] args){System.out.print(new BigInteger(new Scanner(System.in).nextLine()).nextProbablePrime());}}

Husk, 4 3 bytes

Edit: -1 byte thanks to Leo

ḟṗ→

Try it online!

ḟ     # first element that
 ṗ    # is a prime,
  →   # counting up beginning at one greater than the input

Pyth, 5 bytes

fP_Th

Try it online!


Explanation:

f      # first integer greater than or equal to
    hQ # input + 1
 P_T   # which is prime

05AB1E, 2 bytes

ÅN

Try it online!

ÅN  # full program
ÅN  # push smallest prime greater than...
    # implicit input
    # implicit output

Japt, 6 bytes

_j}aUÄ

Run it online.

Python + primefac, 34 32 bytes

Not quite as short as using sympy (another answer already uses that), but it's still pretty short, and it's much faster.

import primefac as p
p.nextprime

Try it online

Input of 2**2000 completes in a couple seconds.

Ruby, 28+6 = 34 bytes

Uses the -rprime flag.

f=->i{i+=1;i.prime??i :f[i]}

Non-recursive version for 31+6=37 bytes:

->i{i+=1;i+=1 while i.prime?;i}

Java 7, 373 343 334 303 268 bytes

import java.math.*;class M{public static void main(String[]a){BigInteger n,i,o,r=new BigInteger(a[0]);for(r=r.add(o=r.ONE);;r=r.add(o)){for(n=r,i=o.add(o);i.compareTo(n)<0;n=n.mod(i).compareTo(o)<0?r.ZERO:n,i=i.add(o));if(n.compareTo(o)>0)break;}System.out.print(r);}}

-75 bytes thanks @Poke

Ungolfed:

import java.math.*;
class M{
  public static void main(String[] a){
    BigInteger n,
               i,
               o,
               r = new BigInteger(a[0]);
    for(r = r.add(o = r.ONE); ; r = r.add(o)){
      for(n = r, i = o.add(o); i.compareTo(n) < 0; n = n.mod(i).compareTo(o)< 0
                                                        ? r.ZERO
                                                        : n,
                                                   i = i.add(o));
      if(n.compareTo(o) > 0){
        break;
      }
    }
    System.out.print(r);
  }
}

Try it here.

Some example input/outputs:

7 -> 11
1609 -> 1613
104723 -> 104729

Lua, 876 Bytes

function I(a)a.s=a.s:gsub("(%d)(9*)$",function(n,k)return tostring(tonumber(n)+1)..("0"):rep(#k)end)end function D(a)a.s=a.s:gsub("(%d)(0*)$",function(n,k)return tostring(tonumber(n)-1)..("9"):rep(#k)end):gsub("^0+(%d)","%1")end function m(a,b)local A=K(a)local B=K(b)while V(0,B)do D(A)D(B)end return A end function M(a,b)local A=K(a)local B=K(b)while V(m(B,1),A)do A=m(A,B)end return A end function l(n)return#n.s end function p(a)local A=K(a)local i=K(2)while V(i,A)do if V(M(A,i),1)then return false end I(i)end return true end function V(b,a)A=K(a)B=K(b)if l(A)>l(B)then return true end if l(B)>l(A)then return false end for i=1,l(A)do c=A.s:sub(i,i)j=B.s:sub(i,i)if c>j then return true elseif c<j then return false end end return false end function K(n)if(type(n)=='table')then return{s=n.s}end return{s=tostring(n)}end P=K(io.read("*n"))repeat I(P)until p(P)print(P.s)

Lua, unlike some other languages, does have a Maximum Integer Size. Once a number gets larger than 232, things stop working correctly, and Lua starts trying to make estimates instead of exact values.

As such, I had to implement a new method of storing numbers, in particular, I've stored them as Base10 strings, because Lua doesn't have a size limit on Strings, other than the size of the memory.

I feel this answer is much more to the Spirit of the question, as it has to itself implement arbitrary precision integers, as well as a prime test.

Explained

-- String Math
_num = {}

_num.__index = _num

-- Increase a by one.
-- This works by grabbing ([0-9])999...$ from the string.
-- Then, increases the first digit in that match, and changes all the nines to zero.
-- "13", only the "3" is matched, and it increases to 1.
-- "19", firstly the 1 is turned to a 2, and then the 9 is changed to a 0.
-- "9" however, the 9 is the last digit matched, so it changes to "10"
function _num.inc(a)
    a.str = a.str:gsub("(%d)(9*)$",function(num,nines)
            return tostring(tonumber(num)+1)..("0"):rep(#nines)
        end)
end


-- Decrease a by one
-- Much like inc, however, uses ([0-9])0...$ instead.
-- Decrements ([0-9]) by one and sets 0... to 9...
-- "13" only the "3" is matched, and it decreases by one.
-- "10", the "1" is matched by the ([0-9]), and the 0 is matched by the 0..., which gives 09, which is clipped to 9.
function _num.dec(a)
    a.str = a.str:gsub("(%d)(0*)$",function(num,zeros)
        return tostring(tonumber(num)-1)..("9"):rep(#zeros)
    end)         :gsub("^0+(%d)","%1")
end

-- Adds a and b
-- Makes A and B, so that the original values aren't modified.
-- B is then decremented until it hits 0, and A is incremented.
-- A is then returned.
function _num.__add(a,b)
    local A = str_num(a)
    local B = str_num(b)
    while B > 0 do
        A:inc()
        B:dec()
    end
    return A
end

-- Subs b from a
-- Works just like Addition, yet Dec's A instead of Incs.
function _num.__sub(a,b)
    local A = str_num(a)
    local B = str_num(b)
    while B > 0 do
        A:dec()
        B:dec()
    end
    return A
end

-- A % B
-- Makes A and B from a and b
-- Constantly subtracts B from A until A is less than B
function _num.__mod(a,b)
    local A = str_num(a)
    local B = str_num(b)
    while A >= B do
        A = A - B
    end
    return A
end

-- #a
-- Useful for golfiness
function _num.__len(n)
    return #n.str
end

-- Primacy Testing
-- Generates A from a and i from 2.
-- Whilst i is less than A, i is incremented by one, and if A % i == 0, then it's not a prime, and we return false.
-- Once that finishes, we return true.
function _num.isprime(a)
    local A = str_num(a)
    local i = str_num(2)
    while i < A do
        if A%i < 1 then
            return false
        end
        i:inc()
    end
    return true
end

-- b < a
-- A and B are generated from a and b
-- Fristly, if the length of A and B aren't equal, then that result is output.
-- Otherwise, each character is searched from left to right, the moment they are unequal, the difference is output.
-- If all the characters match, then it's equal. Return false.
function _num.__lt(b,a)
    A=str_num(a)
    B=str_num(b)
    if #A > #B then
        return true
    end
    if #B > #A then
        return false
    end
    for i=1, #A.str do
        As = A.str:sub(i,i)
        Bs = B.str:sub(i,i)
        if As > Bs then
            return true
        elseif As < Bs then
            return false
        end
    end
    return false
end


-- b <= a
-- Same as b < a, but returns true on equality.
function _num.__le(b,a)
    A=str_num(a)
    B=str_num(b)
    if #A > #B then
        return true
    end
    if #B > #A then
        return false
    end
    for i=1, #A.str do
        As = A.str:sub(i,i)
        Bs = B.str:sub(i,i)
        if As > Bs then
            return true
        elseif As < Bs then
            return false
        end
    end
    return true
end

-- Just straight up returns it's string component. Endlessly faster than the int equivalent, mostly because it never is anything _but_ the string form.
function _num.__tostring(a)
    return a.str
end

-- Just set up the metatable...
function str_num(n)
    if(type(n)=='table')then
        return setmetatable({str = n.str}, _num)
    end
    return setmetatable({str = tostring(n)}, _num)
end

-- Generate a new str_num from STDIN
Prime = str_num(io.read("*n"))

-- This is handy, because it will call Prime:inc() atleast once, and stop at the next prime number it finds.
-- Basically, if it weren't for all that overhead of making the math possible, that's all this would be.
repeat
    Prime:inc()
until Prime:isprime()
print(Prime)

Although the above uses Metatables, instead of just regular functions like the actual answer, which worked out smaller.

SimpleTemplate, 132 bytes

The algorithm is terrible, since I have to do my own code to check if a number is prime or not.
It proved to be horrible, but works.

{@setY argv.0}{@setX 1}{@whileX}{@setX}{@set+T Y,-1}{@for_ from2 toT}{@ifY is multiple_}{@incX}{@/}{@/}{@ifX}{@incY}{@/}{@/}{@echoY}

Receives the number as the first argument, outputting the result.


Ungolfed:

{@set number argv.0}
{@set remainder 1}
{@while remainder}
    {@set remainder 0}
    {@set+ tmp number, -1}
    {@for divisor from 2 to tmp}
        {@if number is multiple divisor}
            {@inc by 1 remainder}
        {@/}
    {@/}
    {@if remainder}
        {@inc by 1 number}
    {@/}
{@/}
{@echo number}

Any tips on how to remove that last @if?

Perl, 30 bytes (29 +1 for -p):

(1x++$_)=~/^(11+?)\1+$/&&redo

Usage

Input the number after pressing return (input 12345 in example below, outputs 12347):

$ perl -pe '(1x++$_)=~/^(11+?)\1+$/&&redo'
12345
12347

How it works

05AB1E, 16 13 bytes (Emigna @ -3 bytes)

2•7£?ÿ•o[>Dp#

Try it online!

2•7£?ÿ•o        # Push current largest prime.
        [   #    # Until true..
         >Dp    # Increment by 1, store, check primality.
                # After infinite loop, implicitly return next prime.

Haskell, 42 46 43 bytes

f n=[i|i<-[n..],all((>0).rem i)[2..i-1]]!!1

the usual code for brute force.

Of course this finds the next smallest prime number after n. There is no biggest prime.

Works for n > 0.

edit: Assumes n is prime. Thanks to @Laikoni's advice in the comments.

Python with sympy, 28 bytes

import sympy
sympy.nextprime

sympy.nextprime is a function which does what it says on the tin. Works for all floats.

repl.it


Python, 66 59 bytes

-4 bytes thanks to Lynn (use -~)
-3 bytes thanks to FlipTack (use and and or, allowing ...==1 to be switched to a ...-1 condition.)

f=lambda n:sum(-~n%-~i<1for i in range(n))-1and f(n+1)or-~n

repl.it

A recursive function that counts up from n until a prime is found by testing that only one number exists up to n-1 that divides it (i.e. 1). Works for all integers, raises an error for floats.

Works on 2.7.8 and 3.5.2, does not work on 3.3.3 (syntax error due to lack of space between ==1 and else)

J, 4 bytes

4&p:

Simple built in for next prime.

Python 2, 78 77 76 74 bytes

def f(n):
 while 1:
    n+=1
    if[i for i in range(1,n)if n%i<1]==[1]:return n

-1 byte thanks to @KritixiLithos
-1 byte thanks to @FlipTack
-2 bytes thanks to @ElPedro

Pyke, 8 7 bytes

~p#Q>)h

Try it here!

4 bytes, noncompeting

(Interpreter updated since challenge posted)

~p<h

Try it here!

~p   -   primes_iterator()
  <  -  filter(^, input() < i)
   h - ^[0]

Perl 6, 25 bytes

{first *.is-prime,$_^..*}

How it works

{                       }  # A lambda.
                  $_ ..*   # Range from the lambda argument to infinity,
                    ^      # not including the start point.
 first           ,         # Iterate the range and return the first number which
       *.is-prime          # is prime.

Perl 6, 32 bytes

{first {all $_ X%2..^$_},$_^..*}

With inefficient custom primality testing.

How it works

Outer structure is the same as above, but the predicate passed to first (to decide if a given number is prime), is now:

{               }  # A lambda.
     $_            # Lambda argument (number to be tested).
          2..^$_   # Range from 2 to the argument, excluding the end-point.
        X          # Cartesian product of the two,
         %         # with the modulo operator applied to each pair.
 all               # Return True if all the modulo results are truthy (i.e. non-0).

MATL, 3 bytes

_Yq 

The function Yq returns the next prime of the absolute value of the input if the input is negative so we implicitly grab the input, negate it (_) and find the next prime using Yq.

Try it Online!

Brachylog, 2 bytes

<ṗ

Try it online!

Explanation

(?) <   (.)      Input < Output
      ṗ (.)      Output is prime
                 (Implicit labelization of the Output at the end of the predicate)

Python 3, 45 bytes

f=lambda n,k=1,m=1:m%k*k>n or-~f(n,k+1,m*k*k)

Try it online!

Bash + coreutils, 52 bytes

for((n=$1,n++;`factor $n|wc -w`-2;n++)){ :;};echo $n

Try it online!

The documentation for bash and factor do not specify a maximum integer value they can handle (although, in practice, each implementation does have a maximum integer value). Presumably, in the GNU of the future on your infinitely large machines, bash and factor will have unlimited size integers.

Python, 114 83 bytes

def g(b):
 while 1:
  b+=1
  for i in range(2,b):
   if b%i<1:break
  else:return b

Without builtins, if there are any.

-30 by removing whitespace and -1 by changing b%i==0 to b%i<1

Oasis, 2 bytes

Run with the -n flag.

Code:

p

Try it online!

JavaScript (ES7), 61 bytes

a=>{for(;a++;){for(c=0,b=2;b<a;b++)a%b||c++;if(!c)return a;}}

Usage

f=a=>{for(;a++;){for(c=0,b=2;b<a;b++)a%b||c++;if(!c)return a;}}
f(2)

Output

3

QBIC, 34 bytes

:{a=a+1[2,a/2|~a%b=0|b=a]]~a<b|_Xa

Based off this QBIC primality tester. Explanation:

:{a=a+1    Read 'a' from the command line, start an infinite loop 
           and at the start of each iteration increment 'a'
[2,a/2|    FOR b = 2, b <= a/2, b++
~a%b=0|    IF b cleanly divides a, we're no prime
b=a]]      so, break out of the FOR loop ( ]] = End if, NEXT )
~a<b|      If the FOR loop completed without breaking
_Xa        then quit, printing the currently tested (prime) number
           The second IF and the DO-loop are implicitly closed by QBIC.

Mathematica, 9 bytes

NextPrime

Maxima, 10 bytes

next_prime

A function returns the smallest prime bigger than its argument.

Jelly, 2 bytes

Æn

Try it online!

This implicitly takes input z and, according to the manual, generate the closest prime strictly greater than z.