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.
&&
is the logical operatorAnd
, which means it takes as arguments statements that have to evaluate toTrue
orFalse
. You seem to be using it as aCompoundExpression
, 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. $\endgroup$P
? You haven't defined it in your post. Is it actually supposed to bePr
? $\endgroup$Append
returns the result of the operation, it does not modify its argument. So whatever your code is trying to doPr
andG
always remain empty lists. You might wantAppendTo
. $\endgroup$GTest[n_Integer] :=Pr = List[];
. The first error is because you are evaluating the rest of the code outside the function body andPr
is not defined. Need to wrap it all in( )
$\endgroup$