Goldbach Partition
Take a look at IntegerPartitions
, although it relies on brute-force enumeration that is unlikely to scale well.
f1[n_] := IntegerPartitions[n, {2}, Prime @ Range @ PrimePi @ n, 1]
f2[n_] := Length @ IntegerPartitions[n, {2}, Prime @ Range @ PrimePi @ n]
Test:
f1[3412]
{{3407, 5}}
f2[3412]
43
Further experiments
Anton Antonov's recent answer surprised me, in that for larger n values his use of Select[FrobeniusSolve[{1, 1}, n], And @@ Map[PrimeQ, #] &]
is faster than f2
above. (In version 10.1 under Windows.) It seems that long lists for the third parameter of IntegerPartitions
is slow.
At the cost of increased memory consumption over f2
but less memory consumption than FrobeniusSolve
we may enumerate with IntegerPartitions
and then filter:
f3[n_] := Cases[IntegerPartitions[n, {2}], {__?PrimeQ}]
Timings on a fairly large integer:
f2[1787834] // AbsoluteTiming
Length @ f3[1787834] // AbsoluteTiming
{7.73629, 6643} {0.846121, 6643}
And Anton's method with the benefit of somewhat faster filtering (as used above):
Length @ Cases[FrobeniusSolve[{1, 1}, 1787834], {__?PrimeQ}] // AbsoluteTiming
{2.2008, 13285}
Note that in this output each solution is enumerated twice which I conjecture is the reason for the lower performance relative to f3
.
I propose to use FrobeniusSolve
. It seems it gives results fairly quickly. How large is the number $n$ ?
AbsoluteTiming[
res = Select[FrobeniusSolve[{1, 1}, 120022], And @@ Map[PrimeQ, #] &];
]
(* {0.340291, Null} *)
Row[{"Number of prime partitions: ", Length[res]}]
(* "Number of prime partitions: ", 1668 *)
Row[{"Sample: ", Take[SortBy[res, Abs[Subtract @@ #] &], 4]}]
(* "Sample: ", {{59981, 60041}, {60041, 59981}, {59921,
60101}, {60101, 59921}} *)
Here is another computation with a larger number:
In[9]:= AbsoluteTiming[
res = Select[FrobeniusSolve[{1, 1}, 7878344],
And @@ Map[PrimeQ, #] &];
]
Out[9]= {25.3882, Null}
As @Mr.Wizard showed, IntegerPartitions
answers both your questions directly, and he warned that it will be slow for large $n$ because it calculates all possible partitions. There is a faster answer to your first question of finding just one partition of even $n=p+q$. Set $p\le q$, and note that usually $p$ is a small prime. The function GoldbachTest
uses a While
loop to check a sequence of small primes in the input list of candidates $p$. If the list is exhausted, the failure condition $\{0,0\}$ is returned.
GoldbachTest[n_?EvenQ, p_List] :=
Block[{m = Length[p], i = 1},
While[i <= m && CompositeQ[n - p[[i]]], i += 1];
If[i > m, {0, 0}, {#, n - #} &[p[[i]]]]
]
Given a list $p$ of the first $k$ primes, there is a smallest even $n$ which cannot be represented as $n=p+q$, with prime $q$. The sequence begins $\{6,12,30,98,98,98,98,220,308,...\}$, which is Sloane's A152522. This page links to a paper by Granville, Van de Lune, and te Riele, where they conjecture that the smallest prime $p$ in a Goldbach partition of even $n=p+q$ is $p<1.603*\log[n]^2 \log[\log[n]]$. They confirmed their conjecture for even $n \le 2*10^{10}$.
Thus, for an efficient test of Goldbach's conjecture from $n=n_{min}$ to $n=n_{max}$ try the following. Using ParallelTable
will be even faster.
GoldbachTestList[nmin_?EvenQ, nmax_Integer] :=
With[{p = Prime[Range[PrimePi[1.603*Log[nmax]^2 Log[Log[nmax]]]]]},
Table[GoldbachTest[n, p], {n, nmin, nmax, 2}]
]
Timings show that GoldbachTestList
is over 100 times faster than the equivalent IntegerPartitions
formulation for $n_{max} \ge 10^5$.