Task: Strange unique prime triplets




Go: package main

import "fmt"

func isPrime(n int) bool {
    switch {
    case n < 2:
        return false
    case n%2 == 0:
        return n == 2
    case n%3 == 0:
        return n == 3
    default:
        d := 5
        for d*d <= n {
            if n%d == 0 {
                return false
            }
            d += 2
            if n%d == 0 {
                return false
            }
            d += 4
        }
        return true
    }
}

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 strangePrimes(n int, countOnly bool) int {
    c := 0
    f := "%2d: %2d + %2d + %2d = %2d\n"
    var s int

    for i := 3; i <= n-4; i += 2 {
        if isPrime(i) {
            for j := i + 2; j <= n-2; j += 2 {
                if isPrime(j) {
                    for k := j + 2; k <= n; k += 2 {
                        if isPrime(k) {
                            s = i + j + k
                            if isPrime(s) {
                                c++
                                if !countOnly {
                                    fmt.Printf(f, c, i, j, k, s)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return c
}

func main() {
    fmt.Println("Unique prime triples under 30 which sum to a prime:")
    strangePrimes(29, false)
    cs := commatize(strangePrimes(999, true))
    fmt.Printf("\nThere are %s unique prime triples under 1,000 which sum to a prime.\n", cs)
}


Java: import java.util.*;

public class StrangeUniquePrimeTriplets {
    public static void main(String[] args) {
        strangeUniquePrimeTriplets(30, true);
        strangeUniquePrimeTriplets(1000, false);
    }

    private static void strangeUniquePrimeTriplets(int limit, boolean verbose) {
        boolean[] sieve = primeSieve(limit * 3);
        List<Integer> primeList = new ArrayList<>();
        for (int p = 3; p < limit; p += 2) {
            if (sieve[p])
                primeList.add(p);
        }
        int n = primeList.size();
        
        int[] primes = new int[n];
        for (int i = 0; i < n; ++i)
            primes[i] = primeList.get(i);
        int count = 0;
        if (verbose)
            System.out.printf("Strange unique prime triplets < %d:\n", limit);
        for (int i = 0; i + 2 < n; ++i) {
            for (int j = i + 1; j + 1 < n; ++j) {
                int s = primes[i] + primes[j];
                for (int k = j + 1; k < n; ++k) {
                    int sum = s + primes[k];
                    if (sieve[sum]) {
                        ++count;
                        if (verbose)
                            System.out.printf("%2d + %2d + %2d = %2d\n", primes[i], primes[j], primes[k], sum);
                    }
                }
            }
        }
        System.out.printf("\nCount of strange unique prime triplets < %d is %d.\n", limit, count);
    }

    private static boolean[] primeSieve(int limit) {
        boolean[] sieve = new boolean[limit];
        Arrays.fill(sieve, true);
        if (limit > 0)
            sieve[0] = false;
        if (limit > 1)
            sieve[1] = false;
        for (int i = 4; i < limit; i += 2)
            sieve[i] = false;
        for (int p = 3; ; p += 2) {
            int q = p * p;
            if (q >= limit)
                break;
            if (sieve[p]) {
                int inc = 2 * p;
                for (; q < limit; q += inc)
                    sieve[q] = false;
            }
        }
        return sieve;
    }
}