Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Solve Goldbach's Conjecture

+8
−0

Goldbach's Conjecture states that every even whole number greater than 2 is the sum of 2 prime numbers. Your task is to return those 2 prime numbers, given an even whole number as input. There are often multiple solutions - any solution will do.

Input/Output Examples

These examples only show one of potentially many possible outputs.

4 -> 2, 2
6 -> 3, 3
24 -> 5, 19
120 -> 7, 113
1000 -> 3, 997

This is code-golf, so shortest answer in each language wins.

P.S. If no one finds a test case that has no solution, I'll consider the problem solved by engineer's induction.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

10 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+0
−0

Python 3.8, 112 bytes

def f(x):y=[i for i in range(2,x)if not[j for j in range(2,i)if i%j<1]];return[(q,x-q)for q in y if x-q in y][0]

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Husk, 9 bytes

ḟo=⁰Σπ2İp

Try it online!

Explanation

ḟo=⁰Σπ2İp
       İp take the infinite list of primes
     π2   cartesian power 2 (all possible pairs)
ḟo        first pair which satisfies:
    Σ     sum
  =       equals
   ⁰      input?
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

JavaScript (Node.js), 87 bytes

f=(a,b=2,c=a-b,d=(a,b=2)=>b<a?a%b&&d(a,b+1):1,e=a=>d(++a)?a:e(a))=>d(c)?[b,c]:f(a,e(b))

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Japt, 13 bytes

o ï æ@¶Xx*Xej

Try it

o ï æ@¶Xx*Xej     :Implicit input of integer U
o                 :Range [0,U)
  ï               :Cartesian product
    æ             :First pair X that returns true
     @            :When passed through the following function
      ¶           :  Test U for equality with
       Xx         :  X Reduced by addition
         *        :  Multiplied by
          Xe      :  Is every element in X
            j     :    Prime?
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

APL (Dyalog Unicode), 32 bytes (SBCS)

{⊃(⊢(/⍨)⍵=+/¨),∘.,⍨(⊢~∘.×)⍨1↓⍳⍵}

Try it online!

{⊃(⊢(/⍨)⍵=+/¨),∘.,⍨(⊢~∘.×)⍨1↓⍳⍵}
                               ⍳⍵ ⍝ Range from 1 to n
                             1↓   ⍝ Drop the 1st number (make 2 to n)
                    (⊢~∘.×)⍨     ⍝ Filter prime numbers
                           ⍨     ⍝ Use range as both arguments for train
                       ∘.×       ⍝ Outer product - multiply all numbers 
                                 ⍝ in [2..n] by every other number in [2..n],
                                 ⍝ giving all composite numbers up to n
                      ~          ⍝ Remove those composite numbers from
                    ⊢           ⍝ that same range (2 to n)
               ∘.,⍨             ⍝ Cartesian product with itself
              ,                 ⍝ Flatten into vector of prime pairs
  (⊢(/⍨)⍵=+/¨)                 ⍝ Filter the ones that sum to n
           +/¨                  ⍝ Map each pair to its sum
        ⍵=                      ⍝ Check if it equals n
     (/⍨)                       ⍝ Keep elements where the pair equals n in
  ⊢                            ⍝ that same vector of prime pairs
 ⊃                             ⍝ Get the first pair that works

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

J, 46 char

({.(+/m)#a),{:(+/"1 m=:x=+/~a)#a=:p:i._1 p:x=:

Sample runs

({.(+/m)#a),{:(+/"1 m=:x=+/~a)#a=:p:i._1 p:x=:4

2 2

({.(+/m)#a),{:(+/"1 m=:x=+/~a)#a=:p:i._1 p:x=:6

3 3

({.(+/m)#a),{:(+/"1 m=:x=+/~a)#a=:p:i._1 p:x=:24

5 19

({.(+/m)#a),{:(+/"1 m=:x=+/~a)#a=:p:i._1 p:x=:120

7 113

({.(+/m)#a),{:(+/"1 m=:x=+/~a)#a=:p:i._1 p:x=:1000

3 997

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

J, 50 bytes

{{({~[:y&(i.&1@:=)[:>+/&.>),{@(,&<)~p:i.p:^:_1 y}}

Try it online!

A nasty DD solution.

{{({~[:y&(i.&1@:=)[:>+/&.>),{@(,&<)~p:i.p:^:_1 y}}
                                    p:i.p:^:_1 NB. primes less than y
                            {@(,&<)            NB. cartesian product, 2d array of boxed pairs
                           ,                   NB. flatten boxes
  ({~[:y&(i.&1@:=)[:>+/&.>)                    NB. monadic hook
                  [:>+/&.>                     NB. sum each pair and unbox
     [:y&(i.&1@:=)                             NB. first occurrence of y in list of sums
   {~                                          NB. flip arguments to index boxed pairs
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Ruby, 69 bytes

require'prime';->n{Prime.first(n).then{_1.product _1}.find{_1+_2==n}}

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

MATL, 19 bytes

XH:YqtZ*!tsH=f1Z)Z)

Try it online!

I'm still a novice at MATL, so I am all ears to any improvements.

XH:YqtZ*!tsH=f1Z)Z)
XH                  - copy implicit input to H clipboard
  :                 - range 1..n
   Yq               - vectorized n-th prime
     t              - duplicate
      Z*            - cartesian product
        !           - transpose
         t          - duplicate
          s         - sum columns
           H=       - paste from clipboard H and vectorized equal
             f      - find indices of nonzeros
              1Z)   - first index returned from f
                 Z) - use that index to find first pair from Z*!
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Ruby, 62 bytes

require'prime';f=->n,k=0{n.prime?&&k.prime?? [n,k]:f[n-1,k+1]}

Attempt This Online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »