I need some help with this algorithm

I'm quite new to Mathematica yet, so there's probably a lot of not-so-subtle problems (indeed the algorithm isn't even running yet). My goal with this algorithm is to input an integer $n$ and get all Goldbach partitions of all even integers > 4 up to $2n$. I'd like it to be as efficient as possible, since this is part of a bigger code, and I really want all partitions for each even. What I considered: I'm gonna input an integer $n$, and I'll return a list (of lists of 2-uples) $G$ with all partitions, wherein $G$[[0]] will give the list of all partitions of the first even > 4 and so on. Also, for efficiency sake, I'm willing to calculate only the primes between $2(n-1)$ and $2n$ each iteration, but I guess that's far from clear on what's coded there. This justifies, though, the prime list $Pr$, since I'm just appending up the new primes that happen at each iteration. So, the algorithm is to calculate the next prime (which is the 'primepi[2t]-th' prime, put it on the list, calculate the sum of all 2-combinations of the list of primes and check whether it sums up to $2t$; if so, it'll append this tuple to $G$.

 GTest[n_Integer] :=
Pr = List[];  G = List[]; k = 0;
ParallelTable[
Append[Pr, Prime[PrimePi[2*t]]] &&
Do[( k = Subsets[Pr, {2}][[j, 0]] +
Subsets[Pr, {2}][[j, 1]] ) &&
(If[k == 2*t, Append[G, Subsets[Pr, {2}][[j]]]]),
{j, 0, Length[Subsets[Pr, {2}] - 1]}],  {t, 3, 2*n}]


The error I'm getting is quite long, I'll paste just the ten last lines:

(kernel 1) Append::normal : Nonatomic expression expected at position 1 in Append[Int,366].

(kernel 2) Part::partd : Part specification {}[[0,1]] is longer than depth of object.

(kernel 1) Part::partd : Part specification {}[[0,1]] is longer than depth of object.

(kernel 2) Append::normal : Nonatomic expression expected at position 1 in Append[Int,330].

(kernel 1) Append::normal : Nonatomic expression expected at position 1 in Append[Int,368].

(kernel 2) General::stop : Further output of Append::normal will be suppressed during this calculation.

(kernel 1) General::stop : Further output of Append::normal will be suppressed during this calculation.

(kernel 2) Part::partd : Part specification {}[[0,1]] is longer than depth of object.

(kernel 1) Part::partd : Part specification {}[[0,1]] is longer than depth of object.

(kernel 2) General::stop : Further output of Part::partd will be suppressed during this calculation.

(kernel 1) General::stop : Further output of Part::partd will be suppressed during this calculation.

During evaluation of In[53]:= Append::normal: Nonatomic expression expected at position 1 in Append[Int,6].

During evaluation of In[53]:= Append::normal: Nonatomic expression expected at position 1 in Append[Int,8].

During evaluation of In[53]:= Append::normal: Nonatomic expression expected at position 1 in Append[Int,10].

During evaluation of In[53]:= General::stop: Further output of Append::normal will be suppressed during this calculation.

• The symbol && is the logical operator And, which means it takes as arguments statements that have to evaluate to True or False. You seem to be using it as a CompoundExpression, i.e. as a way to make Mathematica evaluate those steps in sequence. Use ; instead. In any case, when first learning a programming language, it's a good idea to do simple things first. Take apart the pieces of your code and see if you can get them working on their own first, then try to put pieces together, piece-by-piece. – march Nov 7 '16 at 23:43
• By the way, what is P? You haven't defined it in your post. Is it actually supposed to be Pr? – march Nov 7 '16 at 23:45
• It indeed was Pr. I changed just before posting. And, yes, I was trying to use it as a CompoundExpression. I have little experience programming, and most of it in Python. To be honest, I'm finding Mathematica quite confusing, compared to Python and R, but I haven't sat and tried to do something I really wanted yet. Until now, I explored a few built-in functions, to visualize functions, curves and networks (Manipulate is awesome to see things). This is the first time I'm determined to code something useful (and more complex), but I have experimented some coding before. – izzorts Nov 7 '16 at 23:56
• among many things Append returns the result of the operation, it does not modify its argument. So whatever your code is trying to do Pr and G always remain empty lists. You might want AppendTo. – george2079 Nov 8 '16 at 0:28
• your function as you've defined it is actually just this GTest[n_Integer] :=Pr = List[];  . The first error is because you are evaluating the rest of the code outside the function body and Pr is not defined. Need to wrap it all in ( ) – george2079 Nov 8 '16 at 0:36

You are indeed quite new to Mathematica. I would recommend learning that arrays are indexed 1-based rather than 0-based before leaping straight into a parallel table. And you can't picks two things out of a list of size one. And P is not the same as Pr. But enough admonishment. I'd just use the built in partitioning to do it.

ClearAll@GoldbachPartitions;
Attributes[GoldbachPartitions] = {Listable};
GoldbachPartitions[n_] := IntegerPartitions[2 n, {2}, Prime@Range@PrimePi[2 n]]


In action:

GoldbachPartitions@123


{{241, 5}, {239, 7}, {233, 13}, {229, 17}, {227, 19}, {223, 23}, {199, 47}, {193, 53}, {179, 67}, {173, 73}, {167, 79}, {163, 83}, {157, 89}, {149, 97}, {139, 107}, {137, 109}}

GoldbachPartitions@Range[2, 10]


{{{2, 2}}, {{3, 3}}, {{5, 3}}, {{7, 3}, {5, 5}}, {{7, 5}}, {{11, 3}, {7, 7}}, {{13, 3}, {11, 5}}, {{13, 5}, {11, 7}}, {{17, 3}, {13, 7}}}

• Thanks @wxffles. Admonishments are often useful. As I've seen on another question, using IntegerPartitions won't make the code quite slower, for larger $n$? As I understood reading elsewhere, it would calculate every 2-partition and only then find those made out of prime numbers, won't it? If that's not the case, than it's an awfully elegant solution that of yours. – izzorts Nov 8 '16 at 0:02
• @izzorts Notice that I am using IntegerPartitions with three arguments. The third argument provides the list of the integers it's allowed to use. In this case, it's the list of primes less than $2n$. So it should be very efficient. – wxffles Nov 8 '16 at 0:26
• Awesome. Thanks again. – izzorts Nov 8 '16 at 0:34