Problem 60. The Goldbach Conjecture
Solution Stats
Problem Comments

16 Comments
for n =10,p1=[3 5 7],p2=[7 5 3],is the correct answer. but the judgement is not allowed.
Nice one.
Thank you very much! Its a really nice one!
Well made. I am still trimming down my code furiously. My favorite problem so far.
Currently the best one for me!
nice problem . I like problem related to prime numbers
Very nice problem!
this was a fun one
something I am doing worng.
please help.
p1=[]
p2=[]
for i = 1:5000
x1 = ceil(286*rand(1));
x2 = ceil(286*rand(1));
s = x1+x2;
a =isprime(x1);
b=isprime(x2);
if a==1 && b==1 && s ==n
p1=x1
p2=x2
end
end
[p1 p2]
cool
有中国的小伙伴吗？
Really interesting the Goldbach conjecture!
有中国的小伙伴呀
At first,I thought I should find all the possible answers.But I misunderstood the text............
nice question
perfect nice
Solution Comments

1 Comment
Happier now. Loop though nList handled in test suite. No loop in my function and returns the 2 primes closest together

1 Comment
given that the looping through nList is handled in the test suite, I don't need to.

1 Comment
returning single value of prime pair instead of vector will help

1 Comment
with functions like nchoosek and primes, it is quite easy

1 Comment
tai tama nan le.:)

2 Comments
Forgetting 1 isn't prime doesn't help
See https://en.wikipedia.org/wiki/Prime_number#Primality_of_one

2 Comments
my solution is correct,, what is the problem
Problem is with the test suite it expects just 2 numbers and you generated vector of all possible solutions, if you return after finding the first pair it will work.

1 Comment
Lists all of them.

1 Comment
High score but simple and two lines.

2 Comments
Can't use combnk()? Getting an error for using?
Oh, I can use nchoosek() instead...

1 Comment
Bizarrely I couldn't submit this solution when b, b1 and b2 were labelled p, p1 and p2 due to 'inappopriate content'. I'd be interested to know what looked so inappropriate...

1 Comment
Why is this not working?

1 Comment
how nice

1 Comment
n  (1:n) can be written as (n1):1:0

1 Comment
This one passed tests, but solves another problem: p1 is odd and p2 is prime. It fails to find 2+2=4 and finds incorrectly 9+89=98.

1 Comment
This is the best I can do...

1 Comment
Although the naming of primes is a poor choice,
primes = primes(nums);
can replace:
nums=2:n;
primes=nums(isprime(nums));

1 Comment
Although it passes the test suite, this solution is incorrect for n = 4, 6. The main reason for this error is that the nested loop for j is incorrect. It should be for j=i:c

1 Comment
It would be interesting that the test suite does the assertion on all entries of p1 & p2 in case they return all possible solutions.
assert(all(isprime(p1) & isprime(p2) & (p1+p2==n))

1 Comment
This is a very interesting approach to the problem  learned something new today :D.

2 Comments
I dont understand what the problem with solution is ?
Your for loop bounds exceed the number of prime factors of n. Also, it looks like you are solving for the stated case n=286 and the test cases are using additional inputs.

1 Comment
To compute all possible.. replace b=find(isprime(p2),1,'first'); with b=isprime(p2);
enjoy

2 Comments
smart and short... i needed a "few" lines more :)
smart!

2 Comments
Shouldn't pass, but does. No check of both numbers being primenumbers
I see your point. You were lucky with the original test suite, but I've added a few more test points. Thanks.

1 Comment
Nice! I didn't know they have a function for C_n^k before.

1 Comment
Sometimes vectorized code is not the shortest.
Problem Recent Solvers4794
Suggested Problems

Arrange Vector in descending order
7956 Solvers

Create a vector whose elements depend on the previous element
534 Solvers

616 Solvers

6047 Solvers

742 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!