Cody

# Problem 60. The Goldbach Conjecture

Solution 2107802

Submitted on 27 Jan 2020 by Diego Saldonid
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
nList = 28:6:76; for i = 1:length(nList) n = nList(i); [p1,p2] = goldbach(n) assert(isprime(p1) && isprime(p2) && (p1+p2==n)); end

ans = 2 3 5 7 11 13 17 19 23 m = 1×3 cell array {0×0 double} {0×0 double} {1×2 double} m = 1×5 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×7 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×9 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} ans = 1×4 cell array {1×2 double} {1×2 double} {1×2 double} {1×2 double} p1 = 5 p2 = 23 p1 = 11 p2 = 17 p1 = 17 p2 = 11 p1 = 23 p2 = 5 p1 = 23 p2 = 5 ans = 2 3 5 7 11 13 17 19 23 29 31 m = 1×2 cell array {0×0 double} {1×2 double} m = 1×3 cell array {0×0 double} {1×2 double} {1×2 double} m = 1×5 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} m = 1×7 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×9 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×10 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} m = 1×11 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} ans = 1×7 cell array {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} p1 = 3 p2 = 31 p1 = 5 p2 = 29 p1 = 11 p2 = 23 p1 = 17 p2 = 17 p1 = 23 p2 = 11 p1 = 29 p2 = 5 p1 = 31 p2 = 3 p1 = 31 p2 = 3 ans = 2 3 5 7 11 13 17 19 23 29 31 37 m = 1×2 cell array {0×0 double} {1×2 double} m = 1×5 cell array {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} m = 1×7 cell array {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×9 cell array {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×10 cell array {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} m = 1×12 cell array Columns 1 through 11 {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {0×0 double} Column 12 {1×2 double} ans = 1×6 cell array {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} p1 = 3 p2 = 37 p1 = 11 p2 = 29 p1 = 17 p2 = 23 p1 = 23 p2 = 17 p1 = 29 p2 = 11 p1 = 37 p2 = 3 p1 = 37 p2 = 3 ans = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 m = 1×2 cell array {0×0 double} {1×2 double} m = 1×3 cell array {0×0 double} {1×2 double} {1×2 double} m = 1×7 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {0×0 double} {1×2 double} m = 1×9 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×10 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} m = 1×13 cell array Columns 1 through 11 {0×0 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {0×0 double} Columns 12 through 13 {0×0 double} {1×2 double} m = 1×14 cell array Columns 1 through 11 {0×0 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {0×0 double} Columns 12 through 14 {0×0 double} {1×2 double} {1×2 double} ans = 1×7 cell array {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} p1 = 3 p2 = 43 p1 = 5 p2 = 41 p1 = 17 p2 = 29 p1 = 23 p2 = 23 p1 = 29 p2 = 17 p1 = 41 p2 = 5 p1 = 43 p2 = 3 p1 = 43 p2 = 3 ans = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 m = 1×3 cell array {0×0 double} {0×0 double} {1×2 double} m = 1×5 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×9 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {0×0 double} {1×2 double} m = 1×10 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {0×0 double} {1×2 double} {1×2 double} m = 1×13 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {0×0 double} {1×2 double} {1×2 double} {0×0 double} Columns 12 through 13 {0×0 double} {1×2 double} m = 1×15 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {0×0 double} {1×2 double} {1×2 double} {0×0 double} Columns 12 through 15 {0×0 double} {1×2 double} {0×0 double} {1×2 double} ans = 1×6 cell array {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} p1 = 5 p2 = 47 p1 = 11 p2 = 41 p1 = 23 p2 = 29 p1 = 29 p2 = 23 p1 = 41 p2 = 11 p1 = 47 p2 = 5 p1 = 47 p2 = 5 ans = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 m = 1×3 cell array {0×0 double} {0×0 double} {1×2 double} m = 1×5 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×7 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×10 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} m = 1×13 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} Columns 12 through 13 {0×0 double} {1×2 double} m = 1×15 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} Columns 12 through 15 {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×16 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} Columns 12 through 16 {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} ans = 1×7 cell array {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} p1 = 5 p2 = 53 p1 = 11 p2 = 47 p1 = 17 p2 = 41 p1 = 29 p2 = 29 p1 = 41 p2 = 17 p1 = 47 p2 = 11 p1 = 53 p2 = 5 p1 = 53 p2 = 5 ans = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 m = 1×2 cell array {0×0 double} {1×2 double} m = 1×3 cell array {0×0 double} {1×2 double} {1×2 double} m = 1×5 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} m = 1×7 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×9 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×13 cell array Columns 1 through 11 {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} Columns 12 through 13 {0×0 double} {1×2 double} m = 1×15 cell array Columns 1 through 11 {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} Columns 12 through 15 {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×16 cell arr...

2   Pass
nList = [18 20 22 100 102 114 1000 2000 36 3600]; for i = 1:length(nList) n = nList(i); [p1,p2] = goldbach(n) assert(isprime(p1) && isprime(p2) && (p1+p2==n)); end

ans = 2 3 5 7 11 13 17 m = 1×3 cell array {0×0 double} {0×0 double} {1×2 double} m = 1×4 cell array {0×0 double} {0×0 double} {1×2 double} {1×2 double} m = 1×5 cell array {0×0 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} m = 1×6 cell array {0×0 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} ans = 1×4 cell array {1×2 double} {1×2 double} {1×2 double} {1×2 double} p1 = 5 p2 = 13 p1 = 7 p2 = 11 p1 = 11 p2 = 7 p1 = 13 p2 = 5 p1 = 13 p2 = 5 ans = 2 3 5 7 11 13 17 19 m = 1×2 cell array {0×0 double} {1×2 double} m = 1×4 cell array {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×6 cell array {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×7 cell array {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} ans = 1×4 cell array {1×2 double} {1×2 double} {1×2 double} {1×2 double} p1 = 3 p2 = 17 p1 = 7 p2 = 13 p1 = 13 p2 = 7 p1 = 17 p2 = 3 p1 = 17 p2 = 3 ans = 2 3 5 7 11 13 17 19 m = 1×2 cell array {0×0 double} {1×2 double} m = 1×3 cell array {0×0 double} {1×2 double} {1×2 double} m = 1×5 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} m = 1×7 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×8 cell array {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} ans = 1×5 cell array {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} p1 = 3 p2 = 19 p1 = 5 p2 = 17 p1 = 11 p2 = 11 p1 = 17 p2 = 5 p1 = 19 p2 = 3 p1 = 19 p2 = 3 ans = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 m = 1×2 cell array {0×0 double} {1×2 double} m = 1×5 cell array {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} m = 1×7 cell array {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×10 cell array {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} m = 1×13 cell array Columns 1 through 11 {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} Columns 12 through 13 {0×0 double} {1×2 double} m = 1×15 cell array Columns 1 through 11 {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} Columns 12 through 15 {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×16 cell array Columns 1 through 11 {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} Columns 12 through 16 {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} m = 1×17 cell array Columns 1 through 11 {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} Columns 12 through 17 {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} m = 1×20 cell array Columns 1 through 11 {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} Columns 12 through 20 {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} m = 1×23 cell array Columns 1 through 11 {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} Columns 12 through 22 {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} Column 23 {1×2 double} m = 1×24 cell array Columns 1 through 11 {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} Columns 12 through 22 {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} Columns 23 through 24 {1×2 double} {1×2 double} m = 1×25 cell array Columns 1 through 11 {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} Columns 12 through 22 {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} Columns 23 through 25 {1×2 double} {1×2 double} {1×2 double} ans = 1×12 cell array Columns 1 through 11 {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} Column 12 {1×2 double} p1 = 3 p2 = 97 p1 = 11 p2 = 89 p1 = 17 p2 = 83 p1 = 29 p2 = 71 p1 = 41 p2 = 59 p1 = 47 p2 = 53 p1 = 53 p2 = 47 p1 = 59 p2 = 41 p1 = 71 p2 = 29 p1 = 83 p2 = 17 p1 = 89 p2 = 11 p1 = 97 p2 = 3 p1 = 97 p2 = 3 ans = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 m = 1×3 cell array {0×0 double} {0×0 double} {1×2 double} m = 1×6 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} m = 1×8 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} m = 1×9 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} m = 1×10 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} m = 1×11 cell array {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} m = 1×13 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} Columns 12 through 13 {0×0 double} {1×2 double} m = 1×14 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} Columns 12 through 14 {0×0 double} {1×2 double} {1×2 double} m = 1×17 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} Columns 12 through 17 {0×0 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} m = 1×18 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} Columns 12 through 18 {0×0 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {1×2 double} m = 1×20 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} Columns 12 through 20 {0×0 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} m = 1×21 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} Columns 12 through 21 {0×0 double} {1×2 double} {1×2 double} {0×0 double} {0×0 double} {1×2 double} {1×2 double} {0×0 double} {1×2 double} {1×2 double} m = 1×22 cell array Columns 1 through 11 {0×0 double} {0×0 double} {1×2 double} ...