Re: "Substract one and add one" algorithm

• To: mathgroup at smc.vnet.net
• Subject: [mg59098] Re: "Substract one and add one" algorithm
• Date: Thu, 28 Jul 2005 02:28:20 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```On 7/27/05 at 1:25 AM, gilmar.rodriguez at nwfwmd.state.fl.us (Gilmar)
wrote:

>I want to build a function h[n] that does the following:

>For n even greater and equal than 4:

>Case 1: If m=n/2 is prime then h[n]={n/2,n/2}. Done.

>Case 2: If m =n/2 is not prime; let p[1]=n/2 -1 and q[1]=n/2+1.

>If both p[1], and q[1] are prime then, h[n]={p[1],q[1]}. Done.

>If either one or both p[1] and q[1] are not prime; let p[2]
>=p[1]-1, and q[2]=q[1]+1.

>If both p[2], and q[2] are prime then h[n]={p[2],q[2]}. Done.

>If either one or both p[2] and q[3] are not prime; let p[3]
>=p[2]-1, and q[3]=q[2]+1.

Here is a simple function which does what you described above

h[k_Integer] :=
Module[{p, q, f},
f[p_Integer, q_Integer] :=
If[PrimeQ[p] && PrimeQ[q], {p, q}, f[p - 1, q + 1]];
f[k/2, k/2]] /; k > 3 && EvenQ[k]
--
To reply via email subtract one hundred and four

```

• Prev by Date: Re: Simplification question
• Next by Date: Re: Freeware Mathematica packages(don't know the exact term)