Task: Primes whose sum of digits is 25


C++: #include <algorithm>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <string>

#include <gmpxx.h>

bool is_probably_prime(const mpz_class& n) {
    return mpz_probab_prime_p(n.get_mpz_t(), 3) != 0;
}

bool is_prime(int n) {
    if (n < 2)
        return false;
    if (n % 2 == 0)
        return n == 2;
    if (n % 3 == 0)
        return n == 3;
    for (int p = 5; p * p <= n; p += 4) {
        if (n % p == 0)
            return false;
        p += 2;
        if (n % p == 0)
            return false;
    }
    return true;
}

int digit_sum(int n) {
    int sum = 0;
    for (; n > 0; n /= 10)
        sum += n % 10;
    return sum;
}

int count_all(const std::string& str, int rem) {
    int count = 0;
    if (rem == 0) {
        switch (str.back()) {
        case '1':
        case '3':
        case '7':
        case '9':
            if (is_probably_prime(mpz_class(str)))
                ++count;
            break;
        default:
            break;
        }
    } else {
        for (int i = 1; i <= std::min(9, rem); ++i)
            count += count_all(str + char('0' + i), rem - i);
    }
    return count;
}

int main() {
    std::cout.imbue(std::locale(""));
    const int limit = 5000;
    std::cout << "Primes < " << limit << " whose digits sum to 25:\n";
    int count = 0;
    for (int p = 1; p < limit; ++p) {
        if (digit_sum(p) == 25 && is_prime(p)) {
            ++count;
            std::cout << std::setw(6) << p << (count % 10 == 0 ? '\n' : ' ');
        }
    }
    std::cout << '\n';

    auto start = std::chrono::steady_clock::now();
    count = count_all("", 25);
    auto end = std::chrono::steady_clock::now();
    std::cout << "\nThere are " << count
              << " primes whose digits sum to 25 and include no zeros.\n";
    std::cout << "Time taken: "
              << std::chrono::duration<double>(end - start).count() << "s\n";
    return 0;
}



Go: package main

import (
    "fmt"
    big "github.com/ncw/gmp"
    "time"
)


func sieve(limit int) []bool {
    limit++
    
    c := make([]bool, limit) 
    c[0] = true
    c[1] = true
    
    p := 3 
    for {
        p2 := p * p
        if p2 >= limit {
            break
        }
        for i := p2; i < limit; i += 2 * p {
            c[i] = true
        }
        for {
            p += 2
            if !c[p] {
                break
            }
        }
    }
    return c
}

func sumDigits(n int) int {
    sum := 0
    for n > 0 {
        sum += n % 10
        n /= 10
    }
    return sum
}

func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}


func countAll(p string, rem, res int) int {
    if rem == 0 {
        b := p[len(p)-1]
        if b == '1' || b == '3' || b == '7' || b == '9' {
            z := new(big.Int)
            z.SetString(p, 10)
            if z.ProbablyPrime(1) {
                res++
            }
        }
    } else {
        for i := 1; i <= min(9, rem); i++ {
            res = countAll(p+fmt.Sprintf("%d", i), rem-i, res)
        }
    }
    return res
}

func commatize(n int) string {
    s := fmt.Sprintf("%d", n)
    if n < 0 {
        s = s[1:]
    }
    le := len(s)
    for i := le - 3; i >= 1; i -= 3 {
        s = s[0:i] + "," + s[i:]
    }
    if n >= 0 {
        return s
    }
    return "-" + s
}

func main() {
    start := time.Now()
    c := sieve(4999)
    var primes25 []int
    for i := 997; i < 5000; i += 2 {
        if !c[i] && sumDigits(i) == 25 {
            primes25 = append(primes25, i)
        }
    }
    fmt.Println("The", len(primes25), "primes under 5,000 whose digits sum to 25 are:")
    fmt.Println(primes25)
    n := countAll("", 25, 0)
    fmt.Println("\nThere are", commatize(n), "primes whose digits sum to 25 and include no zeros.")
    fmt.Printf("\nTook %s\n", time.Since(start))
}