Writing a script for Goldbach's Conjecture

17 次查看(过去 30 天)
Hello Matlab Community,
I want to write a script that shows that all even integers between n = 4 and n = N (with N = 1000) can be represented as the sum of two primes, and use the function "assert" to confirm the claim.
This is what I've written so far, unfortunately the assertion fails:
n = 4; N=1000;
Even = n:2:N;
Number = Even(randperm(length(Even),1))
P = primes(N); P = P(P>=n);
Primenumbers = P(randperm(numel(P),2))
sum(Primenumbers)
assert(isequal(Number, sum(Primenumbers)))
Please no loops! I was told the functions "all" and "unique" might be useful. I read the documentation on both, but I am not sure how I can use them in this code!
I am very thankful for any tips!

采纳的回答

Rik
Rik 2022-3-19
编辑:Rik 2022-3-19
I think the goal is to use implicit expansion to create an array with the sums of all primes. A bit like this:
A=[1 2 3 5];
B=A+(A.');
Now you have all the possible sums of two primes. How can you use this to compare it to your other list?
I think I would suggest ismember instead of unique.
  1 个评论
Theresia Dannbauer
Theresia Dannbauer 2022-3-19
Thank you! So first of all, my list for all even integers between 4 and 1000, meaning Line 1-3, is correct? I only started with Matlab last week, sorry :-)

请先登录,再进行评论。

更多回答(1 个)

John D'Errico
John D'Errico 2022-3-19
Let me suggest you are starting from the wrong end?
Instead, start with all primes less than your target maximum, so 1000. Can you find them easily? (Hint: what does primes do?)
help primes
PRIMES Generate list of prime numbers. PRIMES(N) is a row vector of the prime numbers less than or equal to N. A prime number is one that has no factors other than 1 and itself. Class support for input N: float: double, single integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64 See also FACTOR, ISPRIME. Documentation for primes doc primes Other functions named primes symfun/primes
Can you now easily find the sum of all combinations of two of those primes? This is not that difficult, since there are only 168 primes less than 1000. (Hint: you could use meshgrid here.)
help meshgrid
MESHGRID Cartesian rectangular grid in 2-D or 3-D [X,Y] = MESHGRID(x,y) returns 2-D grid coordinates based on the coordinates contained in vectors x and y. X is a matrix where each row is a copy of x, and Y is a matrix where each column is a copy of y. The grid represented by the coordinates X and Y has length(y) rows and length(x) columns. [X,Y,Z] = MESHGRID(x,y,z) returns 3-D grid coordinates defined by the vectors x, y, and z. The grid represented by X, Y, and Z has size length(y)-by-length(x)-by-length(z). [X,Y] = MESHGRID(x) is the same as [X,Y] = MESHGRID(x,x), returning square grid coordinates with grid size length(x)-by-length(x). [X,Y,Z] = MESHGRID(x) is the same as [X,Y,Z] = MESHGRID(x,x,x), returning 3-D grid coordinates with grid size length(x)-by-length(x)-by-length(x). MESHGRID outputs are typically used for the evaluation of functions of two or three variables and for surface and volumetric plots. MESHGRID and NDGRID are similar, but MESHGRID is restricted to 2-D and 3-D while NDGRID supports 1-D to N-D. In 2-D and 3-D the coordinates returned by each function are the same. The difference is the shape of their outputs. For grid vectors x, y, and z of length M, N, and P respectively, NDGRID(x,y) outputs have size M-by-N while MESHGRID(x,y) outputs have size N-by-M. Similarly, NDGRID(x,y,z) outputs have size M-by-N-by-P while MESHGRID(x,y,z) outputs have size N-by-M-by-P. Example: Evaluate and plot the two-variable function f(x,y) = x*exp(-x^2-y^2) for -2 <= x <= 2 and -4 <= y <= 4 [X,Y] = meshgrid(-2:.2:2,-4:.4:4); F = X .* exp(-X.^2 - Y.^2); surf(X,Y,F) Class support for inputs x, y, and z: float: double, single integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64 See also MESH, SURF, SLICE, NDGRID. Documentation for meshgrid doc meshgrid Other functions named meshgrid codistributed/meshgrid gpuArray/meshgrid
In fact, you may even have learned an easier way to form an addition table. What does this do?
P = [2 3 5 7];
P'+P
ans = 4×4
4 5 7 9 5 6 8 10 7 8 10 12 9 10 12 14
Once you have the sum of ALL combinations of 2 primes, convert the result to a vector, and now can you now use unique? Actually, even easier is to use setdiff here. So find the difference between two sets of numbers, where one of those sets is composed of the even numbers 2:2:1000. If this last computation is empty, then ALL even integers had some way to be formed of exactly 2 primes.
Since this is homework, I truly hope that nobody writes the code for you. However, if you make a credible effort along the lines I just gave in detail, then show what you tried if you still have a problem. Out it in a comment to my answer. However, I might even bet you will be successful, IF you make an effort.
  2 个评论
Theresia Dannbauer
Theresia Dannbauer 2022-3-19
Hello John, first of all thanks a lot for this detailed answer!! Technically, it's not part of my homework, it is an extra excercise "for the ones that are interested". :-)
I have actually never seen this P'+P line before, but I think I understand what it does. Could one call this "automatic expansion"? Now, I want to convert the result of Primenumbers to a vector, but Matlab tells me the class of Primenumbers is 'char' and the type is variable. I tried using the "cast" function, but that does not seem to be it.
The script has to end with the "assert" function, thats unfortunately given. And my Professor mentioned using "unique" and "all", because they are new functions to me!
n = 4; N = 1000;
Even = n:2:N;
Number = Even(randperm(length(Even),1))
P = primes(N);
Primenumbers = P' + P
assert(isequal(Number,Primenumbers))
I modified the script to this, but I am afraid I am stuck again.
I am very grateful for any help, because I am one of the interested ones in Matlab!
Theresia Dannbauer
Theresia Dannbauer 2022-3-19
Ok, I think I managed to do it.
n = 4; N = 100;
Even = n:2:N;
Number = Even(randperm(length(Even),1))
P = primes(N); P = P(P>=n);
Primenumbers = P.' + P;
assert(ismember(Number,Primenumbers))
This assertion gives nothing, which it's supposed to do I think.What do you think of this code? Any way of integrating this "unique" function? (I have to agree with Rik though, ismember seems more practical!)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by