8
$\begingroup$

I would like to know what an implementation of the function NextPrime would look like if it were implemented in Mathematica's core language.

$\endgroup$
3
  • 2
    $\begingroup$ Welcome to Mathematica.SE, Robert. What have you tried? It is preferable if you show some effort in working out your problem for yourself, and give some indication of where you got stuck. Please see the FAQ for more details. $\endgroup$
    – Verbeia
    Jan 18, 2013 at 3:42
  • $\begingroup$ Are you looking for any one way to implement it or do you want to know how it is actually implemented? $\endgroup$
    – Szabolcs
    Jan 18, 2013 at 4:44
  • $\begingroup$ What Szabolcs means to say is... NextPrime IS actually implemented in Mathematica. Try Trace[NextPrime[6]]. The core of it is quite similar to what I posted $\endgroup$
    – Rojo
    Jan 18, 2013 at 4:48

3 Answers 3

9
$\begingroup$
(nextPrime[#1] = #2) & @@@ {{-3, 2}, {-2, 2}, {-1, 2}, {0, 2}, {1, 2}, {2, 3}};
nextPrime[n_Integer?EvenQ] := nextPrime[n - 1];
nextPrime[n_Integer] /; PrimeQ[n + 2] := n + 2;
nextPrime[n_Integer] := nextPrime[n + 2]
nextPrime[n_ /; n \[Element] Reals] := nextPrime[Floor@n]
$\endgroup$
7
  • 1
    $\begingroup$ There are only a few prime even integers. Perhaps you could take some advantage $\endgroup$ Jan 18, 2013 at 4:37
  • 2
    $\begingroup$ There you go @belisarius $\endgroup$
    – Rojo
    Jan 18, 2013 at 4:39
  • 1
    $\begingroup$ Ok. You got the NextPrime yellow belt +1 $\endgroup$ Jan 18, 2013 at 4:41
  • $\begingroup$ @belisarius a few? $\endgroup$
    – Mr.Wizard
    Jan 18, 2013 at 5:00
  • 3
    $\begingroup$ @Mr.Wizard Well, my first English teacher was proud of me. He was deaf. $\endgroup$ Jan 18, 2013 at 5:03
5
$\begingroup$

Just a joke:

nextp[i_] := Prime[PrimePi[i] + 1]
$\endgroup$
1
  • 4
    $\begingroup$ somewhatNextPrime[x_] := FindInstance[\[FormalN] > x, \[FormalN], Primes] $\endgroup$
    – Rojo
    Jan 18, 2013 at 4:54
5
$\begingroup$

For reference, here is the v7 code behind NextPrime, which is hard to read before stripping all the private context names.

NextPrime[1]; (* preload the definition *)
Unprotect[NextPrime];
ClearAttributes[NextPrime, ReadProtected];
$Context = "NumberTheory`NextPrimeDump`";
FullDefinition[NextPrime]

Yields:

Attributes[NextPrime] = {Listable}

NextPrime[-3] := -2

NextPrime[-2] := 2

NextPrime[-1] := 2

NextPrime[0] := 2

NextPrime[1] := 2

NextPrime[n_Integer] := Block[{res}, res = integerNextPrime[n]; res /; IntegerQ[res]]

NextPrime[r_] /; NumericQ[r] && ! IntegerQ[r] := 
 Block[{res, n}, 
  n = Quiet[Block[{$MaxExtraPrecision = 
           Max[$MaxExtraPrecision, 1 + Ceiling[Log[10., Abs[N[r]]]]]}, Floor[r]]]; (res = 
     NextPrime[n]; res /; IntegerQ[res]) /; IntegerQ[n]]

NextPrime[n_, k_Integer] /; NumericQ[n] && Positive[k] := 
 Block[{res}, res = Nest[NextPrime, n, k]; res /; IntegerQ[res]]

NextPrime[n_, k_Integer] /; NumericQ[n] && Negative[k] := 
 Block[{res}, res = Nest[PreviousPrime, n, -k]; res /; IntegerQ[res]]

NextPrime[n_?PrimeQ, 0] := n

NextPrime[n_, 0] := NextPrime[n]

NextPrime[n___] := (ArgumentCountQ[NextPrime, Length[{n}], 1, 2]; Null /; False)


integerNextPrime[n_Integer] := 
 Block[{res}, res = n + 1 + Mod[n, 2]; While[! PrimeQ[res], res += 2]; 
  res /; IntegerQ[res]]

integerNextPrime[___] := $Failed


PreviousPrime[n_] := Block[{res}, res = -NextPrime[-n]; res /; IntegerQ[res]]

PreviousPrime[___] := $Failed
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.