Re: "Substract one and add one" algorithm
- To: mathgroup at smc.vnet.net
- Subject: [mg59074] Re: "Substract one and add one" algorithm
- From: Detlef Müller at smc.vnet.net
- Date: Thu, 28 Jul 2005 02:26:38 -0400 (EDT)
- References: <dc776m$k2h$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Gilmar wrote:
...
> 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.
>
> etc.
>
I just assume, this comes to an end.
So simply write down, what you said as a Module:
Clear[h];
h[n_/;EvenQ[n]&&(n>=4)]:=
Module[{p=n/2,q=n/2},
While[Not[PrimeQ[p]&&PrimeQ[q]],
p=p-1;q=q+1
];
{p,q}
]
In[38]:=
h/@{4,6,8,10,12,14,16}
Out[38]=
{{2,2},{3,3},{3,5},{5,5},{5,7},{7,7},{5,11}}
Greetings,
Detlef