Task: Equal prime and composite sums


C++: #include <primesieve.hpp>

#include <chrono>
#include <iomanip>
#include <iostream>
#include <locale>

class composite_iterator {
public:
    composite_iterator();
    uint64_t next_composite();

private:
    uint64_t composite;
    uint64_t prime;
    primesieve::iterator pi;
};

composite_iterator::composite_iterator() {
    composite = prime = pi.next_prime();
    for (; composite == prime; ++composite)
        prime = pi.next_prime();
}

uint64_t composite_iterator::next_composite() {
    uint64_t result = composite;
    while (++composite == prime)
        prime = pi.next_prime();
    return result;
}

int main() {
    std::cout.imbue(std::locale(""));
    auto start = std::chrono::high_resolution_clock::now();
    composite_iterator ci;
    primesieve::iterator pi;
    uint64_t prime_sum = pi.next_prime();
    uint64_t composite_sum = ci.next_composite();
    uint64_t prime_index = 1, composite_index = 1;
    std::cout << "Sum                   | Prime Index  | Composite Index\n";
    std::cout << "------------------------------------------------------\n";
    for (int count = 0; count < 11;) {
        if (prime_sum == composite_sum) {
            std::cout << std::right << std::setw(21) << prime_sum << " | "
                      << std::setw(12) << prime_index << " | " << std::setw(15)
                      << composite_index << '\n';
            composite_sum += ci.next_composite();
            prime_sum += pi.next_prime();
            ++prime_index;
            ++composite_index;
            ++count;
        } else if (prime_sum < composite_sum) {
            prime_sum += pi.next_prime();
            ++prime_index;
        } else {
            composite_sum += ci.next_composite();
            ++composite_index;
        }
    }
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> duration(end - start);
    std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
}



Go: package main

import (
    "fmt"
    "log"
    "rcu"
    "sort"
)

func ord(n int) string {
    if n < 0 {
        log.Fatal("Argument must be a non-negative integer.")
    }
    m := n % 100
    if m >= 4 && m <= 20 {
        return fmt.Sprintf("%sth", rcu.Commatize(n))
    }
    m %= 10
    suffix := "th"
    if m == 1 {
        suffix = "st"
    } else if m == 2 {
        suffix = "nd"
    } else if m == 3 {
        suffix = "rd"
    }
    return fmt.Sprintf("%s%s", rcu.Commatize(n), suffix)
}

func main() {
    limit := int(4 * 1e8)
    c := rcu.PrimeSieve(limit-1, true)
    var compSums []int
    var primeSums []int
    csum := 0
    psum := 0
    for i := 2; i < limit; i++ {
        if c[i] {
            csum += i
            compSums = append(compSums, csum)
        } else {
            psum += i
            primeSums = append(primeSums, psum)
        }
    }

    for i := 0; i < len(primeSums); i++ {
        ix := sort.SearchInts(compSums, primeSums[i])
        if ix < len(compSums) && compSums[ix] == primeSums[i] {
            cps := rcu.Commatize(primeSums[i])
            fmt.Printf("%21s - %12s prime sum, %12s composite sum\n", cps, ord(i+1), ord(ix+1))
        }
    }
}