Tổng số chu kỳ cho các ví dụ: 477.918.603
Cập nhật 1: Cập nhật để sử dụng phỏng đoán của Lemoine .
Cập nhật 2: Cập nhật để sử dụng Sàng của Eratosthenes thay vì ngây thơ tìm các số nguyên tố.
Chạy với:
python3 assemble.py 52489-prime-partitions.golf
python3 golf.py 52489-prime-partitions.bin x=<INPUT>
Chạy ví dụ:
$ python3 golf.py 52489-prime-partitions.bin x=10233
5
5
10223
Execution terminated after 194500 cycles with exit code 0.
Số chu kỳ cho ví dụ đầu vào:
Input    Cycles
9        191
12       282
95       1,666
337      5,792
1023749  21,429,225
20831531 456,481,447
Chúng tôi coi các (N+1)*8byte đầu tiên của heap, là một mảng chứa N+1các giá trị 64 bit. (Vì heap bị giới hạn về kích thước, điều này sẽ chỉ hoạt động N < 2^57). Giá trị của mục tại i*8chỉ ra thời tiết ilà một số nguyên tố:
Value Description
-1    Not a prime
0     Unknown
1     The largest prime found
n > 1 This is a prime and the next prime is n
Khi chúng ta hoàn thành việc xây dựng mảng, nó sẽ trông như thế [-1, -1, 3, 5, -1, 7, -1, 11, -1, -1, -1, 13, ...].
Chúng tôi sử dụng Sàng của Eratosthenes để xây dựng mảng.
Tiếp theo chương trình thực hiện như sau trong mã giả:
if is_prime(x):
    print x
else:
    if is_even(x):
        for p in primes:
            if is_prime(x - p):
                print p, x - p
                exit
    else:
        if is_prime(x - 2):
            print 2, x - 2
        else:
            for p in primes:
                if is_prime(x - 2 * p):
                    print p, p, 2 * p
                    exit
Điều này được đảm bảo để hoạt động vì phỏng đoán của Lemoine và phỏng đoán yếu của Goldbach . Phỏng đoán của Lemoine chưa được chứng minh, nhưng có lẽ đúng với các số dưới 2 ^ 57.
    call build_primes
    mov q, x
    call is_prime
    jnz print_prime, a
    and b, x, 1
    jz find_pair, b
    # Check if x - 2 is a prime
    sub q, x, 2
    call is_prime
    jnz print_prime_odd2, a
# Input: x, b
find_pair:
    mov p, 2
find_pair_loop:
    mov d, p
    jz find_pair_even, b
    add d, d, p
find_pair_even:
    sub q, x, d
    call is_prime
    jnz print_prime2_or_3, a
    shl i, p, 3
    lw p, i
    jmp find_pair_loop
print_prime2_or_3:
    jz print_prime2, b
    mov x, p
    call write_int_ln
print_prime2:
    mov x, p
    call write_int_ln
    mov x, q
    call print_prime
print_prime_odd2:
    mov p, 2
    call print_prime2
print_prime:
    call write_int_ln
    halt 0
# Input: x
# Memory layout: [-1, -1, 3, 5, -1, 7, -1, 11, ...]
# x: max integer
# p: current prime
# y: pointer to last found prime
# i: current integer
build_primes:
    sw 0, -1
    sw 8, -1
    sw 16, 1
    mov y, 16
    mov p, 2
build_primes_outer:
    mulu i, r, p, p
    jnz build_primes_final, r
    geu a, i, x
    jnz build_primes_final, a
build_primes_inner:
    shl m, i, 3
    sw m, -1
    add i, i, p
    geu a, i, x
    jz build_primes_inner, a
build_primes_next:
    inc p
    shl m, p, 3
    lw a, m
    jnz build_primes_next, a
    sw y, p
    mov y, m
    sw y, 1
    jmp build_primes_outer
build_primes_final:
    inc p
    geu a, p, x
    jnz build_primes_ret, a
    shl m, p, 3
    lw a, m
    jnz build_primes_final, a
    sw y, p
    mov y, m
    sw y, 1
    jmp build_primes_final
build_primes_ret:
    ret
# Input: q
# Output: a
is_prime:
    shl m, q, 3
    lw a, m
    neq a, a, -1
    ret a
write_int:
    divu x, m, x, 10
    jz write_int_done, x
    call write_int
write_int_done:
    add m, m, ord("0")
    sw -1, m
    ret
write_int_ln:
    call write_int
    mov m, ord("\n")
    sw -1, m
    ret