WeixiangYan/CodeTransOcean huggingface dataset:
Task: Strange unique prime triplets



C#: using System; using System.Collections.Generic; using static System.Console; using System.Linq; using DT = System.DateTime;

class Program { static void Main(string[] args) { string s;
  foreach (int lmt in new int[]{ 90, 300, 3000, 30000, 111000 }) {
    var pr = PG.Primes(lmt).Skip(1).ToList(); DT st = DT.Now;
    int d, f = 0; var r = new List<string>();
    int i = -1, m, h = (m = lmt / 3), j, k, pra, prab;
    while (i < 0) i = pr.IndexOf(h--); k = (j = i - 1) - 1;
    for (int a = 0; a <= k; a++) { pra = pr[a];
    for (int b = a + 1; b <= j; b++) { prab = pra + pr[b]; 
    for (int c = b + 1; c <= i; c++) {
      if (PG.flags[d = prab + pr[c]]) continue; f++;
      if (lmt < 100) r.Add(string.Format("{3,5} = {0,2} + {1,2} + {2,2}", pra, pr[b], pr[c], d)); } } }
    s = "s.u.p.t.s under "; r.Sort(); if (r.Count > 0) WriteLine("{0}{1}:\n{2}", s, m, string.Join("\n", r));
    if (lmt > 100) WriteLine("Count of {0}{1,6:n0}: {2,13:n0}  {3} sec", s, m, f, (DT.Now - st).ToString().Substring(6)); } } }

class PG { public static bool[] flags;
  public static IEnumerable<int> Primes(int lim) {
  flags = new bool[lim + 1]; int j = 2;
  for (int d = 3, sq = 4; sq <= lim; j++, sq += d += 2)
    if (!flags[j]) { yield return j;
      for (int k = sq; k <= lim; k += j) flags[k] = true; }
  for (; j <= lim; j++) if (!flags[j]) yield return j; } }





Python: from sympy import primerange

def strange_triplets(mx: int = 30) -> None:
    primes = list(primerange(0, mx))
    primes3 = set(primerange(0, 3 * mx))
    for i, n in enumerate(primes):
        for j, m in enumerate(primes[i + 1:], i + 1):
            for p in primes[j + 1:]:
                if n + m + p in primes3:
                    yield n, m, p

for c, (n, m, p) in enumerate(strange_triplets(), 1):
    print(f"{c:2}: {n:2}+{m:2}+{p:2} = {n + m + p}")

mx = 1_000
print(f"\nIf n, m, p < {mx:_} finds {sum(1 for _ in strange_triplets(mx)):_}")
Task: Strange unique prime triplets





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;
    }
}



Python: from sympy import primerange

def strange_triplets(mx: int = 30) -> None:
    primes = list(primerange(0, mx))
    primes3 = set(primerange(0, 3 * mx))
    for i, n in enumerate(primes):
        for j, m in enumerate(primes[i + 1:], i + 1):
            for p in primes[j + 1:]:
                if n + m + p in primes3:
                    yield n, m, p

for c, (n, m, p) in enumerate(strange_triplets(), 1):
    print(f"{c:2}: {n:2}+{m:2}+{p:2} = {n + m + p}")

mx = 1_000
print(f"\nIf n, m, p < {mx:_} finds {sum(1 for _ in strange_triplets(mx)):_}")
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)
}




Python: from sympy import primerange

def strange_triplets(mx: int = 30) -> None:
    primes = list(primerange(0, mx))
    primes3 = set(primerange(0, 3 * mx))
    for i, n in enumerate(primes):
        for j, m in enumerate(primes[i + 1:], i + 1):
            for p in primes[j + 1:]:
                if n + m + p in primes3:
                    yield n, m, p

for c, (n, m, p) in enumerate(strange_triplets(), 1):
    print(f"{c:2}: {n:2}+{m:2}+{p:2} = {n + m + p}")

mx = 1_000
print(f"\nIf n, m, p < {mx:_} finds {sum(1 for _ in strange_triplets(mx)):_}")
Task: Strange unique prime triplets







Python: from sympy import primerange

def strange_triplets(mx: int = 30) -> None:
    primes = list(primerange(0, mx))
    primes3 = set(primerange(0, 3 * mx))
    for i, n in enumerate(primes):
        for j, m in enumerate(primes[i + 1:], i + 1):
            for p in primes[j + 1:]:
                if n + m + p in primes3:
                    yield n, m, p

for c, (n, m, p) in enumerate(strange_triplets(), 1):
    print(f"{c:2}: {n:2}+{m:2}+{p:2} = {n + m + p}")

mx = 1_000
print(f"\nIf n, m, p < {mx:_} finds {sum(1 for _ in strange_triplets(mx)):_}")


VB: Imports DT = System.DateTime

Module Module1

    Iterator Function Primes(lim As Integer) As IEnumerable(Of Integer)
        Dim flags(lim) As Boolean

        Dim j = 2

        Dim d = 3
        Dim sq = 4
        While sq <= lim
            If Not flags(j) Then
                Yield j
                For k = sq To lim Step j
                    flags(k) = True
                Next
            End If

            j += 1
            d += 2
            sq += d
        End While

        While j <= lim
            If Not flags(j) Then
                Yield j
            End If
            j += 1
        End While
    End Function

    Sub Main()
        For Each lmt In {90, 300, 3000, 30000, 111000}
            Dim pr = Primes(lmt).Skip(1).ToList()
            Dim st = DT.Now
            Dim f = 0
            Dim r As New List(Of String)
            Dim i = -1
            Dim m = lmt \ 3
            Dim h = m
            While i < 0
                i = pr.IndexOf(h)
                h -= 1
            End While
            Dim j = i - 1
            Dim k = j - 1
            For a = 0 To k
                Dim pra = pr(a)
                For b = a + 1 To j
                    Dim prab = pra + pr(b)
                    For c = b + 1 To i
                        Dim d = prab + pr(c)
                        If Not pr.Contains(d) Then
                            Continue For
                        End If
                        f += 1
                        If lmt < 100 Then
                            r.Add(String.Format("{3,5} = {0,2} + {1,2} + {2,2}", pra, pr(b), pr(c), d))
                        End If
                    Next
                Next
            Next
            Dim s = "s.u.p.t.s under "
            r.Sort()
            If r.Count > 0 Then
                Console.WriteLine("{0}{1}:" + vbNewLine + "{2}", s, m, String.Join(vbNewLine, r))
            End If
            If lmt > 100 Then
                Console.WriteLine("Count of {0}{1,6:n0}: {2,13:n0}  {3} sec", s, m, f, (DT.Now - st).ToString().Substring(6))
            End If
        Next
    End Sub

End Module
Task: Strange unique prime triplets

C: #include <stdbool.h>
#include <stdio.h>
#include <string.h>

#define LIMIT 3000

void init_sieve(unsigned char sieve[], int limit) {
    int i, j;

    for (i = 0; i < limit; i++) {
        sieve[i] = 1;
    }
    sieve[0] = 0;
    sieve[1] = 0;

    for (i = 2; i < limit; i++) {
        if (sieve[i]) {
            for (j = i + i; j < limit; j += i) {
                sieve[j] = 0;
            }
        }
    }
}

void strange_unique_prime_triplets(unsigned char sieve[], int limit, bool verbose) {
    int count = 0, sum;
    int i, j, k, n, p;
    int pi, pj, pk;

    n = 0;
    for (i = 0; i < limit; i++) {
        if (sieve[i]) {
            n++;
        }
    }

    if (verbose) {
        printf("Strange unique prime triplets < %d:\n", limit);
    }

    for (i = 0; i + 2 < n; i++) {
        pi = 2;
        p = i;
        while (p > 0) {
            pi++;
            if (sieve[pi]) {
                p--;
            }
        }

        for (j = i + 1; j + 1 < n; j++) {
            pj = pi;
            p = j - i;
            while (p > 0) {
                pj++;
                if (sieve[pj]) {
                    p--;
                }
            }

            for (k = j + 1; k < n; k++) {
                pk = pj;
                p = k - j;
                while (p > 0) {
                    pk++;
                    if (sieve[pk]) {
                        p--;
                    }
                }

                sum = pi + pj + pk;
                if (sum < LIMIT && sieve[sum]) {
                    count++;
                    if (verbose) {
                        printf("%2d + %2d + %2d = %d\n", pi, pj, pk, sum);
                    }
                }
            }
        }
    }

    printf("Count of strange unique prime triplets < %d is %d.\n\n", limit, count);
}

int main() {
    unsigned char sieve[LIMIT];

    init_sieve(sieve, LIMIT);

    strange_unique_prime_triplets(sieve, 30, true);
    strange_unique_prime_triplets(sieve, 1000, false);

    return 0;
}







Python: from sympy import primerange

def strange_triplets(mx: int = 30) -> None:
    primes = list(primerange(0, mx))
    primes3 = set(primerange(0, 3 * mx))
    for i, n in enumerate(primes):
        for j, m in enumerate(primes[i + 1:], i + 1):
            for p in primes[j + 1:]:
                if n + m + p in primes3:
                    yield n, m, p

for c, (n, m, p) in enumerate(strange_triplets(), 1):
    print(f"{c:2}: {n:2}+{m:2}+{p:2} = {n + m + p}")

mx = 1_000
print(f"\nIf n, m, p < {mx:_} finds {sum(1 for _ in strange_triplets(mx)):_}")
Task: First 9 prime Fibonacci number

C: #include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

bool isPrime(uint64_t n) {
    if (n < 2) return false;
    if (!(n%2)) return n == 2;
    if (!(n%3)) return n == 3;
    uint64_t d = 5;
    while (d*d <= n) {
        if (!(n%d)) return false;
        d += 2;
        if (!(n%d)) return false;
        d += 4;
    }
    return true;
}

int main() {
    uint64_t f1 = 1, f2 = 1, f3;
    int count = 0, limit = 12; 
    printf("The first %d prime Fibonacci numbers are:\n", limit);
    while (count < limit) {
        f3 = f1 + f2;
        if (isPrime(f3)) {
            printf("%ld ", f3);
            count++;
        }
        f1 = f2;
        f2 = f3;
    }
    printf("\n");
    return 0;
}



C#: #include <chrono>
#include <iostream>
#include <sstream>
#include <utility>
#include <primesieve.hpp>
#include <gmpxx.h>

using big_int = mpz_class;

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

class prime_fibonacci_generator {
public:
    prime_fibonacci_generator();
    std::pair<uint64_t, big_int> next();
private:
    big_int next_fibonacci();
    primesieve::iterator p_;
    big_int f0_ = 0;
    big_int f1_ = 1;
    uint64_t n_ = 0;
};

prime_fibonacci_generator::prime_fibonacci_generator() {
    for (int i = 0; i < 2; ++i)
        p_.next_prime();
}

std::pair<uint64_t, big_int> prime_fibonacci_generator::next() {
    for (;;) {
        if (n_ > 4) {
            uint64_t p = p_.next_prime();
            for (; p > n_; ++n_)
                next_fibonacci();
        }
        ++n_;
        big_int f = next_fibonacci();
        if (is_probably_prime(f))
            return {n_ - 1, f};
    }
}

big_int prime_fibonacci_generator::next_fibonacci() {
    big_int result = f0_;
    big_int f = f0_ + f1_;
    f0_ = f1_;
    f1_ = f;
    return result;
}

std::string to_string(const big_int& n) {
    std::string str = n.get_str();
    if (str.size() > 40) {
        std::ostringstream os;
        os << str.substr(0, 20) << "..." << str.substr(str.size() - 20) << " ("
           << str.size() << " digits)";
        return os.str();
    }
    return str;
}

int main() {
    auto start = std::chrono::high_resolution_clock::now();
    prime_fibonacci_generator gen;
    for (int i = 1; i <= 26; ++i) {
        auto [n, f] = gen.next();
        std::cout << i << ": F(" << n << ") = " << to_string(f) << '\n';
    }
    auto finish = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> ms(finish - start);
    std::cout << "elapsed time: " << ms.count() << " seconds\n";
}
Task: First 9 prime Fibonacci number

C: #include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

bool isPrime(uint64_t n) {
    if (n < 2) return false;
    if (!(n%2)) return n == 2;
    if (!(n%3)) return n == 3;
    uint64_t d = 5;
    while (d*d <= n) {
        if (!(n%d)) return false;
        d += 2;
        if (!(n%d)) return false;
        d += 4;
    }
    return true;
}

int main() {
    uint64_t f1 = 1, f2 = 1, f3;
    int count = 0, limit = 12; 
    printf("The first %d prime Fibonacci numbers are:\n", limit);
    while (count < limit) {
        f3 = f1 + f2;
        if (isPrime(f3)) {
            printf("%ld ", f3);
            count++;
        }
        f1 = f2;
        f2 = f3;
    }
    printf("\n");
    return 0;
}


C++: #include <chrono>
#include <iostream>
#include <sstream>
#include <utility>
#include <primesieve.hpp>
#include <gmpxx.h>

using big_int = mpz_class;

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

class prime_fibonacci_generator {
public:
    prime_fibonacci_generator();
    std::pair<uint64_t, big_int> next();
private:
    big_int next_fibonacci();
    primesieve::iterator p_;
    big_int f0_ = 0;
    big_int f1_ = 1;
    uint64_t n_ = 0;
};

prime_fibonacci_generator::prime_fibonacci_generator() {
    for (int i = 0; i < 2; ++i)
        p_.next_prime();
}

std::pair<uint64_t, big_int> prime_fibonacci_generator::next() {
    for (;;) {
        if (n_ > 4) {
            uint64_t p = p_.next_prime();
            for (; p > n_; ++n_)
                next_fibonacci();
        }
        ++n_;
        big_int f = next_fibonacci();
        if (is_probably_prime(f))
            return {n_ - 1, f};
    }
}

big_int prime_fibonacci_generator::next_fibonacci() {
    big_int result = f0_;
    big_int f = f0_ + f1_;
    f0_ = f1_;
    f1_ = f;
    return result;
}

std::string to_string(const big_int& n) {
    std::string str = n.get_str();
    if (str.size() > 40) {
        std::ostringstream os;
        os << str.substr(0, 20) << "..." << str.substr(str.size() - 20) << " ("
           << str.size() << " digits)";
        return os.str();
    }
    return str;
}

int main() {
    auto start = std::chrono::high_resolution_clock::now();
    prime_fibonacci_generator gen;
    for (int i = 1; i <= 26; ++i) {
        auto [n, f] = gen.next();
        std::cout << i << ": F(" << n << ") = " << to_string(f) << '\n';
    }
    auto finish = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> ms(finish - start);
    std::cout << "elapsed time: " << ms.count() << " seconds\n";
}
Task: First 9 prime Fibonacci number

C: #include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

bool isPrime(uint64_t n) {
    if (n < 2) return false;
    if (!(n%2)) return n == 2;
    if (!(n%3)) return n == 3;
    uint64_t d = 5;
    while (d*d <= n) {
        if (!(n%d)) return false;
        d += 2;
        if (!(n%d)) return false;
        d += 4;
    }
    return true;
}

int main() {
    uint64_t f1 = 1, f2 = 1, f3;
    int count = 0, limit = 12; 
    printf("The first %d prime Fibonacci numbers are:\n", limit);
    while (count < limit) {
        f3 = f1 + f2;
        if (isPrime(f3)) {
            printf("%ld ", f3);
            count++;
        }
        f1 = f2;
        f2 = f3;
    }
    printf("\n");
    return 0;
}




Go: package main

import "fmt"

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

func main() {
    f1 := uint64(1)
    f2 := f1
    count := 0
    limit := 12 
    fmt.Printf("The first %d prime Fibonacci numbers are:\n", limit)
    for count < limit {
        f3 := f1 + f2
        if isPrime(f3) {
            fmt.Printf("%d ", f3)
            count++
        }
        f1 = f2
        f2 = f3
    }
    fmt.Println()
}
Task: First 9 prime Fibonacci number

C: #include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

bool isPrime(uint64_t n) {
    if (n < 2) return false;
    if (!(n%2)) return n == 2;
    if (!(n%3)) return n == 3;
    uint64_t d = 5;
    while (d*d <= n) {
        if (!(n%d)) return false;
        d += 2;
        if (!(n%d)) return false;
        d += 4;
    }
    return true;
}

int main() {
    uint64_t f1 = 1, f2 = 1, f3;
    int count = 0, limit = 12; 
    printf("The first %d prime Fibonacci numbers are:\n", limit);
    while (count < limit) {
        f3 = f1 + f2;
        if (isPrime(f3)) {
            printf("%ld ", f3);
            count++;
        }
        f1 = f2;
        f2 = f3;
    }
    printf("\n");
    return 0;
}





Java: import java.math.BigInteger;

public class PrimeFibonacciGenerator {
    private PrimeGenerator primeGen = new PrimeGenerator(10000, 200000);
    private BigInteger f0 = BigInteger.ZERO;
    private BigInteger f1 = BigInteger.ONE;
    private int index = 0;

    public static void main(String[] args) {
        PrimeFibonacciGenerator gen = new PrimeFibonacciGenerator();
        long start = System.currentTimeMillis();
        for (int i = 1; i <= 26; ++i) {
            BigInteger f = gen.next();
            System.out.printf("%d: F(%d) = %s\n", i, gen.index - 1, toString(f));
        }
        long finish = System.currentTimeMillis();
        System.out.printf("elapsed time: %g seconds\n", (finish - start)/1000.0);
    }

    private PrimeFibonacciGenerator() {
        for (int i = 0; i < 2; ++i)
            primeGen.nextPrime();
    }

    private BigInteger next() {
        for (;;) {
            if (index > 4) {
                int p = primeGen.nextPrime();
                for (; p > index; ++index)
                    nextFibonacci();
            }
            ++index;
            BigInteger f = nextFibonacci();
            if (f.isProbablePrime(30))
                return f;
        }
    }

    private BigInteger nextFibonacci() {
        BigInteger result = f0;
        BigInteger f = f0.add(f1);
        f0 = f1;
        f1 = f;
        return result;
    }

    private static String toString(BigInteger f) {
        String str = f.toString();
        if (str.length() > 40) {
            StringBuilder s = new StringBuilder(str.substring(0, 20));
            s.append("...");
            s.append(str.substring(str.length() - 20));
            s.append(" (");
            s.append(str.length());
            s.append(" digits)");
            str = s.toString();
        }
        return str;
    }
}
Task: First 9 prime Fibonacci number

C: #include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

bool isPrime(uint64_t n) {
    if (n < 2) return false;
    if (!(n%2)) return n == 2;
    if (!(n%3)) return n == 3;
    uint64_t d = 5;
    while (d*d <= n) {
        if (!(n%d)) return false;
        d += 2;
        if (!(n%d)) return false;
        d += 4;
    }
    return true;
}

int main() {
    uint64_t f1 = 1, f2 = 1, f3;
    int count = 0, limit = 12; 
    printf("The first %d prime Fibonacci numbers are:\n", limit);
    while (count < limit) {
        f3 = f1 + f2;
        if (isPrime(f3)) {
            printf("%ld ", f3);
            count++;
        }
        f1 = f2;
        f2 = f3;
    }
    printf("\n");
    return 0;
}







Python: from math import sqrt
from time import time

n = 12
start = time()


def prime(x):
    if x < 2:
        return False
    if x == 2 or x == 3:
        return True
    if x % 2 == 0:
        return False
    for i in range(3, int(sqrt(x)) + 1, 2):
        if x % i == 0:
            return False
    return True


a, b, = 1, 1
fibn = 3
f = []
while len(f) < n:
    a, b, = b, a + b
    if prime(b):
        f.append(b)
        print("fib(%d): %s (%s s)" % (fibn, b, time() - start))
    fibn += 1