       Re: "Substract one and add one" algorithm

• To: mathgroup at smc.vnet.net
• Subject: [mg59060] Re: "Substract one and add one" algorithm
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Thu, 28 Jul 2005 02:26:04 -0400 (EDT)
• Organization: The Open University, Milton Keynes, U.K.
• References: <dc776m\$k2h\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Gilmar wrote:
> Dear Mathematica Users Forum Friends:
>
> 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=n/2 -1 and q=n/2+1.
>
>                     If both p, and q are prime then,
>                     h[n]={p,q}. Done.
>
>                     If either one or both p and q are not prime;
>                     let p =p-1, and q=q+1.
>
>                     If both p, and q are prime then
>                     h[n]={p,q}. Done.
>
>                     If either one or both p and q are not prime;
>                     let p =p-1, and q=q+1.
>
>                     etc.
>
> I want to test empirically that a value h[n] = {p[k],q[k]}
> (for an appropriate integer k) exists.
>
> A few examples:
>
>
> n=4
> n/2=2 is prime; so h={2,2}.
>
> n=6
> n/2=3 is prime; so h={3,3}.
>
> n=8 is not prime; so p=n/2 -1 =3 is prime and q=n/2+1=5 is prime;
>                   so h={3,5}.
>
> n=10
> n/2=5 is prime; so h={5,5}.
>
> n=12
> n/2=6 is not prime; so p=n/2-1=5 is prime and q=n/2+1=7 is prime;
>       so h={5,7}.
>
>
> n=14
> n/2=7 is prime; so h={7,7}.
>
> n=16
> n/2=8 is not prime;
>       so p=n/2-1=7 is prime but, q=n/2+1=9 is not prime,
>       so p=7-1=6 is not prime, and q=9+1=10 is not prime,
>       so p=6-1=5 is prime, and q=10+1=11 is prime,
>       so h={5,11}.
>
> Thank you for your help!
>
Hi Gilmar,

You could try something along the following lines:

In:=
h[(n_)?(EvenQ[#1] && #1 >= 4 & )] := {n/2, n/2} /; PrimeQ[n/2]

In:=
h[(n_)?(EvenQ[#1] && #1 >= 4 & )] := {n/2 - 1, n/2 + 1} /;
PrimeQ[n/2 - 1] && PrimeQ[n/2 + 1]

In:=
h[(n_)?(EvenQ[#1] && #1 >= 4 & )] := Module[{p, q},
p = n/2 - 1; q = n/2 + 1; checkpq[p, q]]

In:=
checkpq[(p_)?Positive, q_] := {p, q} /; PrimeQ[p] && PrimeQ[q]

In:=
checkpq[(p_)?Positive, q_] := checkpq[p - 1, q + 1]

In:=
h

Out=
h

In:=
h

Out=
{2, 2}

In:=
h

Out=
h

In:=
h

Out=
{3, 3}

In:=
h

Out=
{3, 5}

In:=
h

Out=
{5, 5}

In:=
h

Out=
{5, 7}

In:=
h

Out=
{7, 7}

In:=
h

Out=
{5, 11}

Best regards,
/J.M.

```

• Prev by Date: Re: Showing Mathematica notebooks within Firefox browser (like Adobe PDF's)
• Next by Date: Re: Functional equations for HermiteH[n,x]