# The prevprime and nextprime functions

**URL:** <https://discourse.julialang.org/t/the-prevprime-and-nextprime-functions/4163>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 8, 2017, 4:21pm UTC](https://discourse.julialang.org/t/the-prevprime-and-nextprime-functions/4163 "2017-06-08T16:21:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [June 8, 2017, 4:21pm UTC](https://discourse.julialang.org/t/the-prevprime-and-nextprime-functions/4163/1 "2017-06-08T16:21:35Z")

</div>

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]

```

Question:

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 8, 2017, 5:16pm UTC](https://discourse.julialang.org/t/the-prevprime-and-nextprime-functions/4163/2 "2017-06-08T17:16:30Z")

</div>

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: [https://github.com/JuliaMath/Primes.jl/pull/41](https://github.com/JuliaMath/Primes.jl/pull/41)

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.

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [June 8, 2017, 7:25pm UTC](https://discourse.julialang.org/t/the-prevprime-and-nextprime-functions/4163/3 "2017-06-08T19:25:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 9, 2017, 11:43pm UTC](https://discourse.julialang.org/t/the-prevprime-and-nextprime-functions/4163/4 "2017-06-09T23:43:31Z")

</div>

Thank you @leiteiro. I raised the [the issue](https://github.com/JuliaMath/Primes.jl/issues/48).

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [June 10, 2017, 7:29am UTC](https://discourse.julialang.org/t/the-prevprime-and-nextprime-functions/4163/5 "2017-06-10T07:29:35Z")

</div>

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 ‘\<’.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 10, 2017, 10:35am UTC](https://discourse.julialang.org/t/the-prevprime-and-nextprime-functions/4163/6 "2017-06-10T10:35:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [June 10, 2017, 10:55am UTC](https://discourse.julialang.org/t/the-prevprime-and-nextprime-functions/4163/7 "2017-06-10T10:55:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [June 10, 2017, 11:45am UTC](https://discourse.julialang.org/t/the-prevprime-and-nextprime-functions/4163/8 "2017-06-10T11:45:30Z")

</div>

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]
