Challenges

# Solve Goldbach's Conjecture

+7
−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.

Why does this post require moderator attention?
Why should this post be closed?

+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?
Why does this post require moderator attention?

+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!

Why does this post require moderator attention?

+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?
Why does this post require moderator attention?

+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


Why does this post require moderator attention?