0
$\begingroup$

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.

Can you please help with that?

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

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.

enter image description here

$\endgroup$
  • $\begingroup$ Thank you, this is beautiful and works. Any idea to represent it graphically? $\endgroup$ – Dovendyr Jun 15 '18 at 11:33
  • $\begingroup$ 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 $\endgroup$ – Dovendyr Jun 15 '18 at 11:40
  • $\begingroup$ See edit to the answer for the 3 primes case. Not sure what you need as a graphical representation. $\endgroup$ – Fraccalo Jun 15 '18 at 15:33
  • $\begingroup$ This is only thought as a test, so I guess beautiful will do :) $\endgroup$ – Dovendyr Jun 16 '18 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.