WeixiangYan/CodeTransOcean huggingface dataset:
Task: Primorial numbers

C: #include <inttypes.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <gmp.h>


int es_check(uint32_t *sieve, uint64_t n)
{
    if ((n != 2 && !(n & 1)) || (n < 2))
        return 0;
    else
        return !(sieve[n >> 6] & (1 << (n >> 1 & 31)));
}

uint32_t *es_sieve(const uint64_t nth, uint64_t *es_size)
{
    *es_size = nth * log(nth) + nth * (log(log(nth)) - 0.9385f) + 1;
    uint32_t *sieve = calloc((*es_size >> 6) + 1, sizeof(uint32_t));

    for (uint64_t i = 3; i < sqrt(*es_size) + 1; i += 2)
        if (!(sieve[i >> 6] & (1 << (i >> 1 & 31))))
            for (uint64_t j = i * i; j < *es_size; j += (i << 1))
                sieve[j >> 6] |= (1 << (j >> 1 & 31));

    return sieve;
}

size_t mpz_number_of_digits(const mpz_t op)
{
    char *opstr = mpz_get_str(NULL, 10, op);
    const size_t oplen = strlen(opstr);
    free(opstr);
    return oplen;
}

#define PRIMORIAL_LIMIT 1000000

int main(void)
{
    
    uint64_t sieve_size;
    uint32_t *sieve = es_sieve(PRIMORIAL_LIMIT, &sieve_size);

    mpz_t primorial;
    mpz_init_set_ui(primorial, 1);

    uint64_t prime_count = 0;
    int print = 1;
    double unused;

    for (uint64_t i = 2; i < sieve_size && prime_count <= PRIMORIAL_LIMIT; ++i) {
        if (print) {
            if (prime_count < 10)
                gmp_printf("Primorial(%" PRIu64 ") = %Zd\n", prime_count, primorial);
            
            else if (!modf(log10(prime_count), &unused))
                printf("Primorial(%" PRIu64 ") has %zu digits\n", prime_count, mpz_number_of_digits(primorial));
            print = 0;
        }

        if (es_check(sieve, i)) {
            mpz_mul_ui(primorial, primorial, i);
            prime_count++;
            print = 1;
        }

    }

    free(sieve);
    mpz_clear(primorial);
    return 0;
}







Python: from pyprimes import nprimes
from functools import reduce


primelist = list(nprimes(1000001))    

def primorial(n):
    return reduce(int.__mul__, primelist[:n], 1)

if __name__ == '__main__':
    print('First ten primorals:', [primorial(n) for n in range(10)])
    for e in range(7):
        n = 10**e
        print('primorial(%i) has %i digits' % (n, len(str(primorial(n)))))
Task: Primorial numbers


C++: using System;

class Program {

    static int l;

    static int[] gp(int n) {
        var c = new bool[n]; var r = new int[(int)(1.28 * n)];
        l = 0; r[l++] = 2; r[l++] = 3; int j, d, lim = (int)Math.Sqrt(n);
        for (int i = 9; i < n; i += 6) c[i] = true; 
        for (j = 5, d = 4; j < lim; j += (d = 6 - d)) if (!c[j]) { r[l++] = j;
            for (int k = j * j, ki = j << 1; k < n; k += ki) c[k] = true; }
        for ( ; j < n; j += (d = 6 - d)) if (!c[j]) r[l++] = j; return r; }

    static void Main(string[] args) {
        var sw = System.Diagnostics.Stopwatch.StartNew();
        var res = gp(15485864); sw.Stop();
        double gpt = sw.Elapsed.TotalMilliseconds, tt;
        var s = new string[19]; int si = 0;
        s[si++] = String.Format("primes gen time: {0} ms", gpt); sw.Restart();
        s[si++] = "    Nth Primorial";
        double y = 0; int x = 1, i = 0, lmt = 10;
        s[si++] = String.Format("{0,7} {1}", 0, x);
        while (true) {
            if (i < 9) s[si++] = String.Format("{0,7} {1}", i + 1, x *= res[i]);
            if (i == 8) s[si++] = "    Nth Digits     Time";
            y += Math.Log10(res[i]);
            if (++i == lmt) {
                s[si++] = String.Format("{0,7} {1,-7}  {2,7} ms", lmt, 1 + (int)y,
                    sw.Elapsed.TotalMilliseconds);
                if ((lmt *= 10) > (int)1e6) break;    }    }
        sw.Stop();
        Console.WriteLine("{0}\n     Tabulation: {1} ms", string.Join("\n", s),
            tt = sw.Elapsed.TotalMilliseconds);
        Console.Write("          Total:{0} ms", gpt + tt);    }    }



Go: package main

import (
	"fmt"
	"math/big"
	"time"

	"github.com/jbarham/primegen.go"
)

func main() {
	start := time.Now()
	pg := primegen.New()
	var i uint64
	p := big.NewInt(1)
	tmp := new(big.Int)
	for i <= 9 {
		fmt.Printf("primorial(%v) = %v\n", i, p)
		i++
		p = p.Mul(p, tmp.SetUint64(pg.Next()))
	}
	for _, j := range []uint64{1e1, 1e2, 1e3, 1e4, 1e5, 1e6} {
		for i < j {
			i++
			p = p.Mul(p, tmp.SetUint64(pg.Next()))
		}
		fmt.Printf("primorial(%v) has %v digits", i, len(p.String()))
		fmt.Printf("\t(after %v)\n", time.Since(start))
	}
}
Task: Primorial numbers


C++: using System;

class Program {

    static int l;

    static int[] gp(int n) {
        var c = new bool[n]; var r = new int[(int)(1.28 * n)];
        l = 0; r[l++] = 2; r[l++] = 3; int j, d, lim = (int)Math.Sqrt(n);
        for (int i = 9; i < n; i += 6) c[i] = true; 
        for (j = 5, d = 4; j < lim; j += (d = 6 - d)) if (!c[j]) { r[l++] = j;
            for (int k = j * j, ki = j << 1; k < n; k += ki) c[k] = true; }
        for ( ; j < n; j += (d = 6 - d)) if (!c[j]) r[l++] = j; return r; }

    static void Main(string[] args) {
        var sw = System.Diagnostics.Stopwatch.StartNew();
        var res = gp(15485864); sw.Stop();
        double gpt = sw.Elapsed.TotalMilliseconds, tt;
        var s = new string[19]; int si = 0;
        s[si++] = String.Format("primes gen time: {0} ms", gpt); sw.Restart();
        s[si++] = "    Nth Primorial";
        double y = 0; int x = 1, i = 0, lmt = 10;
        s[si++] = String.Format("{0,7} {1}", 0, x);
        while (true) {
            if (i < 9) s[si++] = String.Format("{0,7} {1}", i + 1, x *= res[i]);
            if (i == 8) s[si++] = "    Nth Digits     Time";
            y += Math.Log10(res[i]);
            if (++i == lmt) {
                s[si++] = String.Format("{0,7} {1,-7}  {2,7} ms", lmt, 1 + (int)y,
                    sw.Elapsed.TotalMilliseconds);
                if ((lmt *= 10) > (int)1e6) break;    }    }
        sw.Stop();
        Console.WriteLine("{0}\n     Tabulation: {1} ms", string.Join("\n", s),
            tt = sw.Elapsed.TotalMilliseconds);
        Console.Write("          Total:{0} ms", gpt + tt);    }    }






Python: from pyprimes import nprimes
from functools import reduce


primelist = list(nprimes(1000001))    

def primorial(n):
    return reduce(int.__mul__, primelist[:n], 1)

if __name__ == '__main__':
    print('First ten primorals:', [primorial(n) for n in range(10)])
    for e in range(7):
        n = 10**e
        print('primorial(%i) has %i digits' % (n, len(str(primorial(n)))))
Task: Primorial numbers




Go: package main

import (
	"fmt"
	"math/big"
	"time"

	"github.com/jbarham/primegen.go"
)

func main() {
	start := time.Now()
	pg := primegen.New()
	var i uint64
	p := big.NewInt(1)
	tmp := new(big.Int)
	for i <= 9 {
		fmt.Printf("primorial(%v) = %v\n", i, p)
		i++
		p = p.Mul(p, tmp.SetUint64(pg.Next()))
	}
	for _, j := range []uint64{1e1, 1e2, 1e3, 1e4, 1e5, 1e6} {
		for i < j {
			i++
			p = p.Mul(p, tmp.SetUint64(pg.Next()))
		}
		fmt.Printf("primorial(%v) has %v digits", i, len(p.String()))
		fmt.Printf("\t(after %v)\n", time.Since(start))
	}
}




Python: from pyprimes import nprimes
from functools import reduce


primelist = list(nprimes(1000001))    

def primorial(n):
    return reduce(int.__mul__, primelist[:n], 1)

if __name__ == '__main__':
    print('First ten primorals:', [primorial(n) for n in range(10)])
    for e in range(7):
        n = 10**e
        print('primorial(%i) has %i digits' % (n, len(str(primorial(n)))))