How could I implement the equivalent of NextPrime

8

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

Robert G. Wilson v

Posted 2013-01-18T03:08:32.270

Reputation: 81

2

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.

– Verbeia – 2013-01-18T03:42:30.713

Are you looking for any one way to implement it or do you want to know how it is actually implemented? – Szabolcs – 2013-01-18T04:44:34.203

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 – Rojo – 2013-01-18T04:48:18.877

Answers

9

(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]

Rojo

Posted 2013-01-18T03:08:32.270

Reputation: 40 993

1There are only a few prime even integers. Perhaps you could take some advantage – Dr. belisarius – 2013-01-18T04:37:05.883

2There you go @belisarius – Rojo – 2013-01-18T04:39:57.987

1Ok. You got the NextPrime yellow belt +1 – Dr. belisarius – 2013-01-18T04:41:14.097

@belisarius a few? – Mr.Wizard – 2013-01-18T05:00:52.630

3@Mr.Wizard Well, my first English teacher was proud of me. He was deaf. – Dr. belisarius – 2013-01-18T05:03:38.513

@Artes, better? – Rojo – 2013-01-18T23:19:31.883

@Rojo Now better +1 – Artes – 2013-01-18T23:23:43.467

5

Just a joke:

nextp[i_] := Prime[PrimePi[i] + 1]

Dr. belisarius

Posted 2013-01-18T03:08:32.270

Reputation: 112 848

4somewhatNextPrime[x_] := FindInstance[\[FormalN] > x, \[FormalN], Primes] – Rojo – 2013-01-18T04:54:32.297

5

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

Mr.Wizard

Posted 2013-01-18T03:08:32.270

Reputation: 259 163