The prevprime and nextprime functions



Consider the following outputs:

# Maple seq(nextprime(n), n=10..14);
# [11, 13, 13, 17, 17]

# Mathematica Table[NextPrime[n, 1], {n, 10, 14}]
# [11, 13, 13, 17, 17]

# Julia println([nextprime(n) for n in 10:14])
# [11, 11, 13, 13, 17]

# Maple seq(prevprime(n), n=10..14);
# [7, 7, 11, 11, 13]

# Mathematica Table[NextPrime[n, -1], {n, 10, 14}]
# [7, 7, 11, 11, 13]

# Julia println([prevprime(n) for n in 10:14])
# [7, 11, 11, 13, 13]


Were the Julia functions so designed only to confuse the Russians
or are there less profane reasons for doing so?


I assume you’re referring to the Primes.jl package, since there has never been a nextprime function in Julia’s standard library as far as I know. nextprime was added there a month ago, and the behavior nextprime(p) == p for prime p was apparently based on PARI/GP and also on the nextpow2 function:

However, since this was added so recently, it probably would not be onerous to change if there are good arguments for doing so. Feel free to file an issue in Primes.jl.


Yes, I am referring to primes.jl which I regard as quasi-standard
as it houses now the expatriated Base functions isprime, primes,
primesmask and factor.

The arguments I have already implicitly given: Since the most widely
used CASs use a different definition, it is foreseeable that this will
lead to a large number of errors in the use.

Under these conditions the current implementations seem to me to be negligent.


Thank you @leiteiro. I raised the the issue.


Thank you for doing so. I was not aware of this page.

What amazes me in the discussion there is that the linguistic aspect was not mentioned, which is not unimportant for a public API.

If you are in a queue and someone calls “the next please” and no one moves because the parlance includes “=” this certainly violates the rule of least surprise for many users.

Now that I see that the author comes from France and was inspired by a piece of French software this explains much: In France and there in the French number-theoretic community the use of “is less” is indeed different from the use in the Englisch community, i.e. ‘<=’ versus ‘<’.


Maybe, adding another type Prime, we can have floor(Prime,n) and ceil(Prime,n) return the current meaning of prevprime and nextprime.

Having a Prime subtype of Integer (or Unsigned) could facilitate its use in parameters of modular arithmetic types (to make prime fields). This would lead to defining PrimePower perhaps and SmoothInteger etc. etc.


Nice idea!

Mentioning prime powers gives me a chill. In this case PARI is fine however the OEIS is not. OEIS-A000961 even starts with a silly comment. I hope the implementator will use OEIS-A246655.


Just for completeness I also looked up SageMath.
It gives the same results as Mathematica and Maple.

  • SageMath
    print [previous_prime(n) for n in (10…14)]
    [7, 7, 11, 11, 13]

  • SageMath
    print [next_prime(n) for n in (10…14)]
    [11, 13, 13, 17, 17]