n=10;
Flatten[DeleteDuplicates[Table[If[PrimeQ[p] && p + q == 2 n, Sort[{p, q}], Nothing], {p, 3,
2 n}, {q, 1, 2 n - p}]], 1]
{{3, 17}, {5, 15}, {7, 13}, {9, 11}, {1, 19}}
Update
Much faster if the duplicates and empty lists are avoided from the get go.
n = 10;
Cases[IntegerPartitions[2 n, {2}], ls : {___, _?PrimeQ, ___} :>
Switch[ls, {1 | 2, Except[_?PrimeQ]} | {Except[_?PrimeQ], 1 | 2}, Nothing, _, ls]]
{{19, 1}, {17, 3}, {15, 5}, {13, 7}, {11, 9}}
Comparison:
n = 1000;
l1 = Flatten[
DeleteDuplicates[
Table[If[PrimeQ[p] && p + q == 2 n, Sort[{p, q}], Nothing], {p,
3, 2 n}, {q, 1, 2 n - p}]], 1]; // AbsoluteTiming
l2 = Cases[IntegerPartitions[2 n, {2}],
ls : {___, _?PrimeQ, ___} :>
Switch[ls, {1 | 2, Except[_?PrimeQ]} | {Except[_?PrimeQ], 1 | 2},
Nothing, _, ls]]; // AbsoluteTiming
Equal @@ (Length /@ {l1, l2}) && Complement[Sort /@ l1, Sort /@ l2] == {}
{1.58086, Null}
{0.004746, Null}
True
StringTake[ToString@{0, {1, 2}, {3, 4, 5}, {{6}}}, {2, -2}]? – Alan Oct 31 '17 at 16:500,{1,2},{3,4,5},{{6}}is not a valid Mathematica expression. Do you want itPrinted? If soStringTake[ToString[{0, {1, 2}, {3, 4, 5}, {{6}}}], {2, -2}]– Marius Ladegård Meyer Oct 31 '17 at 16:52With[{n = 10}, DeleteDuplicatesBy[Flatten[Table[If[PrimeQ[p] && p + q == 2 n, {p, q}, Nothing], {p, 3, 2 n}, {q, 2 n - p}], 1], Sort]]– J. M.'s missing motivation Oct 31 '17 at 17:40DeleteDuplicatesByis not defined in Mathematica 9. :( – asad Oct 31 '17 at 18:02