# Trying to generate random even number

I am trying to generate a random even number, and then try to write the list of primes that are composing it as Goldbach conjecture says.

It's just a test to learn how to use mathematica, so I would really appreciate comments.

I tried this:

f[i_]:=f[i]=Table[{i,1,100},i];


Generate an invisible table of integers up to 100. My teacher says you have to generate a list first to work with it.

a = If[f[i], Divisible[i,2],Print[i],Print[i+1]]


Here I attempted to print out only if the randomly generated integer was even.

DeleteCases[IntegerPartitions[a], If[a = PrimeQ, Print[a],]


Here I meant to tell Mathematica to sort out all the possibilities where a is composed of two primes, if not just do not print a.

This doesn't work of course. I tried to google the problem but I don't find something close enough.

• Range[2,100,2] for your first step. – Αλέξανδρος Ζεγγ Jun 15 '18 at 8:49
• Thank you. If I try that, would the second step be: Range[2,100,2] Select[Random[#]]? – Dovendyr Jun 15 '18 at 11:53
• If possible, please describe, what you want next, in another way that I could understand. – Αλέξανδρος Ζεγγ Jun 15 '18 at 15:54
• Thank you again. After generating a random number I need to select one, and then run the same partition as Fraccalo did. – Dovendyr Jun 16 '18 at 4:34

This should work:

(*generate a random integer between 3 and 100*)
n = 2*RandomInteger[{2, 50}]

(*list of primes smaller than the random integer n*)
listPrimes = Prime[Range[PrimePi[n]]]

(*list of pairs {prime,n-prime} for each prime in listPrimes*)
pairs = {#, n - #} & /@ listPrimes

(*select all the pairs where both the elements are primes and remove repetitions*)
result = DeleteDuplicates[Sort[#] & /@ Select[pairs, And @@ (PrimeQ[#]) &]]


EDIT: If you need the sum of 3 primes instead of two:

(*list of three primes {prime1,prime2,n-prime1-prime2} for each prime in listPrimes*)
tris = {#[[1]], #[[2]], n - #[[1]] - #[[2]]} & /@ Tuples[listPrimes, 2];

(*select all the three primes where all the elements are primes and remove repetitions*)
result= DeleteDuplicates[Sort[#] & /@ Select[tris, And @@ (PrimeQ[#]) && And @@ (Positive[#]) &]]


And you could represent your result as follows:

BarChart[result, ChartLayout -> "Stacked"]


I'll leave it with you for making it nice.

• Thank you, this is beautiful and works. Any idea to represent it graphically? – Dovendyr Jun 15 '18 at 11:33
• Hello again, how to modify this line if I want an addition of 3 primes instead of two? (list of pairs {prime,n-prime} for each prime in listPrimes) pairs = {#, n - #} & /@ listPrimes – Dovendyr Jun 15 '18 at 11:40
• See edit to the answer for the 3 primes case. Not sure what you need as a graphical representation. – Fraccalo Jun 15 '18 at 15:33
• This is only thought as a test, so I guess beautiful will do :) – Dovendyr Jun 16 '18 at 4:35