WeixiangYan/CodeTransOcean huggingface dataset:
Task: Self numbers


C++: #include <array>
#include <iomanip>
#include <iostream>

const int MC = 103 * 1000 * 10000 + 11 * 9 + 1;
std::array<bool, MC + 1> SV;

void sieve() {
    std::array<int, 10000> dS;
    for (int a = 9, i = 9999; a >= 0; a--) {
        for (int b = 9; b >= 0; b--) {
            for (int c = 9, s = a + b; c >= 0; c--) {
                for (int d = 9, t = s + c; d >= 0; d--) {
                    dS[i--] = t + d;
                }
            }
        }
    }
    for (int a = 0, n = 0; a < 103; a++) {
        for (int b = 0, d = dS[a]; b < 1000; b++, n += 10000) {
            for (int c = 0, s = d + dS[b] + n; c < 10000; c++) {
                SV[dS[c] + s++] = true;
            }
        }
    }
}

int main() {
    sieve();

    std::cout << "The first 50 self numbers are:\n";
    for (int i = 0, count = 0; count <= 50; i++) {
        if (!SV[i]) {
            count++;
            if (count <= 50) {
                std::cout << i << ' ';
            } else {
                std::cout << "\n\n       Index     Self number\n";
            }
        }
    }
    for (int i = 0, limit = 1, count = 0; i < MC; i++) {
        if (!SV[i]) {
            if (++count == limit) {
                
                std::cout << std::setw(12) << count << "   " << std::setw(13) << i << '\n';
                limit *= 10;
            }
        }
    }

    return 0;
}
Task: Increasing gaps between consecutive Niven numbers


C++: #include <cstdint>
#include <iomanip>
#include <iostream>



uint64_t digit_sum(uint64_t n, uint64_t sum) {
    ++sum;
    while (n > 0 && n % 10 == 0) {
        sum -= 9;
        n /= 10;
    }
    return sum;
}

inline bool divisible(uint64_t n, uint64_t d) {
    if ((d & 1) == 0 && (n & 1) == 1)
        return false;
    return n % d == 0;
}

int main() {
    
    std::cout.imbue(std::locale(""));

    uint64_t previous = 1, gap = 0, sum = 0;
    int niven_index = 0, gap_index = 1;

    std::cout << "Gap index  Gap    Niven index    Niven number\n";
    for (uint64_t niven = 1; gap_index <= 32; ++niven) {
        sum = digit_sum(niven, sum);
        if (divisible(niven, sum)) {
            if (niven > previous + gap) {
                gap = niven - previous;
                std::cout << std::setw(9) << gap_index++
                    << std::setw(5) << gap
                    << std::setw(15) << niven_index
                    << std::setw(16) << previous << '\n';
            }
            previous = niven;
            ++niven_index;
        }
    }
    return 0;
}
Task: Minimum multiple of m where digital sum equals m


C++: #include <iomanip>
#include <iostream>

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

int main() {
    for (int n = 1; n <= 70; ++n) {
        for (int m = 1;; ++m) {
            if (digit_sum(m * n) == n) {
                std::cout << std::setw(8) << m << (n % 10 == 0 ? '\n' : ' ');
                break;
            }
        }
    }
}
Task: Pythagorean quadruples


C++: #include <iostream>
#include <vector>

constexpr int N = 2200;
constexpr int N2 = 2 * N * N;

int main() {
    using namespace std;

    vector<bool> found(N + 1);
    vector<bool> aabb(N2 + 1);

    int s = 3;

    for (int a = 1; a < N; ++a) {
        int aa = a * a;
        for (int b = 1; b < N; ++b) {
            aabb[aa + b * b] = true;
        }
    }

    for (int c = 1; c <= N; ++c) {
        int s1 = s;
        s += 2;
        int s2 = s;
        for (int d = c + 1; d <= N; ++d) {
            if (aabb[s1]) {
                found[d] = true;
            }
            s1 += s2;
            s2 += 2;
        }
    }

    cout << "The values of d <= " << N << " which can't be represented:" << endl;
    for (int d = 1; d <= N; ++d) {
        if (!found[d]) {
            cout << d << " ";
        }
    }
    cout << endl;

    return 0;
}
Task: Largest difference between adjacent primes


C++: #include <iostream>
#include <locale>

#include <primesieve.hpp>

int main() {
    std::cout.imbue(std::locale(""));
    const uint64_t limit = 10000000000;
    uint64_t max_diff = 0;
    primesieve::iterator pi;
    uint64_t p1 = pi.next_prime();
    for (uint64_t p = 10;;) {
        uint64_t p2 = pi.next_prime();
        if (p2 >= p) {
            std::cout << "Largest gap between primes under " << p << " is "
                      << max_diff << ".\n";
            if (p == limit)
                break;
            p *= 10;
        }
        if (p2 - p1 > max_diff)
            max_diff = p2 - p1;
        p1 = p2;
    }
}
Task: Ulam numbers


C++: #include <algorithm>
#include <chrono>
#include <iostream>
#include <vector>

int ulam(int n) {
    std::vector<int> ulams{1, 2};
    std::vector<int> sieve{1, 1};
    for (int u = 2; ulams.size() < n; ) {
        sieve.resize(u + ulams[ulams.size() - 2], 0);
        for (int i = 0; i < ulams.size() - 1; ++i)
            ++sieve[u + ulams[i] - 1];
        auto it = std::find(sieve.begin() + u, sieve.end(), 1);
        if (it == sieve.end())
            return -1;
        u = (it - sieve.begin()) + 1;
        ulams.push_back(u);
    }
    return ulams[n - 1];
}

int main() {
    auto start = std::chrono::high_resolution_clock::now();
    for (int n = 1; n <= 100000; n *= 10)
        std::cout << "Ulam(" << n << ") = " << ulam(n) << '\n';
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> duration(end - start);
    std::cout << "Elapsed time: " << duration.count() << " seconds\n";
}
Task: Smallest power of 6 whose decimal expansion contains n


C++: #include <iostream>
#include <iomanip>
#include <string>
#include <gmpxx.h>

std::string smallest_six(unsigned int n) {
    mpz_class pow = 1;
    std::string goal = std::to_string(n);
    
    while (pow.get_str().find(goal) == std::string::npos) {
        pow *= 6;
    }
    
    return pow.get_str();
}

int main() {
    for (unsigned int i=0; i<22; i++) {
        std::cout << std::setw(2) << i << ": " 
                  << smallest_six(i) << std::endl;
    }
    return 0;
}
Task: Type detection


C++: #include <iostream>

template <typename T>
auto typeString(const T&) {
    return typeid(T).name();
}

class C {};
struct S {};

int main() {
    std::cout << typeString(1) << '\n';
    std::cout << typeString(1L) << '\n';
    std::cout << typeString(1.0f) << '\n';
    std::cout << typeString(1.0) << '\n';
    std::cout << typeString('c') << '\n';
    std::cout << typeString("string") << '\n';
    std::cout << typeString(C{}) << '\n';
    std::cout << typeString(S{}) << '\n';
    std::cout << typeString(nullptr) << '\n';
}
Task: Safe primes and unsafe primes


C++: #include <algorithm>
#include <iostream>
#include <iterator>
#include <locale>
#include <vector>
#include "prime_sieve.hpp"

const int limit1 = 1000000;
const int limit2 = 10000000;

class prime_info {
public:
    explicit prime_info(int max) : max_print(max) {}
    void add_prime(int prime);
    void print(std::ostream& os, const char* name) const;
private:
    int max_print;
    int count1 = 0;
    int count2 = 0;
    std::vector<int> primes;
};

void prime_info::add_prime(int prime) {
    ++count2;
    if (prime < limit1)
        ++count1;
    if (count2 <= max_print)
        primes.push_back(prime);
}

void prime_info::print(std::ostream& os, const char* name) const {
    os << "First " << max_print << " " << name << " primes: ";
    std::copy(primes.begin(), primes.end(), std::ostream_iterator<int>(os, " "));
    os << '\n';
    os << "Number of " << name << " primes below " << limit1 << ": " << count1 << '\n';
    os << "Number of " << name << " primes below " << limit2 << ": " << count2 << '\n';
}

int main() {
    
    prime_sieve sieve(limit2);

    
    std::cout.imbue(std::locale(""));

    
    prime_info safe_primes(35);
    prime_info unsafe_primes(40);
    for (int p = 2; p < limit2; ++p) {
        if (sieve.is_prime(p)) {
            if (sieve.is_prime((p - 1)/2))
                safe_primes.add_prime(p);
            else
                unsafe_primes.add_prime(p);
        }
    }
    safe_primes.print(std::cout, "safe");
    unsafe_primes.print(std::cout, "unsafe");
    return 0;
}
Task: Numbers whose count of divisors is prime


C++: #include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>

int divisor_count(int n) {
    int total = 1;
    for (; (n & 1) == 0; n >>= 1)
        ++total;
    for (int p = 3; p * p <= n; p += 2) {
        int count = 1;
        for (; n % p == 0; n /= p)
            ++count;
        total *= count;
    }
    if (n > 1)
        total *= 2;
    return total;
}

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 main(int argc, char** argv) {
    int limit = 1000;
    switch (argc) {
    case 1:
        break;
    case 2:
        limit = std::strtol(argv[1], nullptr, 10);
        if (limit <= 0) {
            std::cerr << "Invalid limit\n";
            return EXIT_FAILURE;
        }
        break;
    default:
        std::cerr << "usage: " << argv[0] << " [limit]\n";
        return EXIT_FAILURE;
    }
    int width = static_cast<int>(std::ceil(std::log10(limit)));
    int count = 0;
    for (int i = 1;; ++i) {
        int n = i * i;
        if (n >= limit)
            break;
        int divisors = divisor_count(n);
        if (divisors != 2 && is_prime(divisors))
            std::cout << std::setw(width) << n << (++count % 10 == 0 ? '\n' : ' ');
    }
    std::cout << "\nCount: " << count << '\n';
    return EXIT_SUCCESS;
}
Task: Giuga numbers


C++: #include <iostream>


bool is_giuga(unsigned int n) {
    unsigned int m = n / 2;
    auto test_factor = [&m, n](unsigned int p) -> bool {
        if (m % p != 0)
            return true;
        m /= p;
        return m % p != 0 && (n / p - 1) % p == 0;
    };
    if (!test_factor(3) || !test_factor(5))
        return false;
    static constexpr unsigned int wheel[] = {4, 2, 4, 2, 4, 6, 2, 6};
    for (unsigned int p = 7, i = 0; p * p <= m; ++i) {
        if (!test_factor(p))
            return false;
        p += wheel[i & 7];
    }
    return m == 1 || (n / m - 1) % m == 0;
}

int main() {
    std::cout << "First 5 Giuga numbers:\n";
    
    for (unsigned int i = 0, n = 6; i < 5; n += 4) {
        if (is_giuga(n)) {
            std::cout << n << '\n';
            ++i;
        }
    }
}
Task: Anadromes


C++: #include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> words;
    std::ifstream in("words.txt");
    if (!in) {
        std::cerr << "Cannot open file words.txt.\n";
        return EXIT_FAILURE;
    }
    std::string line;
    while (getline(in, line)) {
        if (line.size() > 6)
            words.push_back(line);
    }
    sort(words.begin(), words.end());
    for (const std::string& word : words) {
        std::string reversed(word.rbegin(), word.rend());
        if (reversed > word &&
            binary_search(words.begin(), words.end(), reversed)) {
            std::cout << std::setw(8) << word << " <-> " << reversed << '\n';
        }
    }
    return EXIT_SUCCESS;
}
Task: Magic numbers


C++: #include <array>
#include <iostream>
#include <numeric>
#include <vector>

#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::uint128_t;

class magic_number_generator {
public:
    magic_number_generator() : magic_(10) {
        std::iota(magic_.begin(), magic_.end(), 0);
    }
    bool next(uint128_t& n);

public:
    std::vector<uint128_t> magic_;
    size_t index_ = 0;
    int digits_ = 2;
};

bool magic_number_generator::next(uint128_t& n) {
    if (index_ == magic_.size()) {
        std::vector<uint128_t> magic;
        for (uint128_t m : magic_) {
            if (m == 0)
                continue;
            uint128_t n = 10 * m;
            for (int d = 0; d < 10; ++d, ++n) {
                if (n % digits_ == 0)
                    magic.push_back(n);
            }
        }
        index_ = 0;
        ++digits_;
        magic_ = std::move(magic);
    }
    if (magic_.empty())
        return false;
    n = magic_[index_++];
    return true;
}

std::array<int, 10> get_digits(uint128_t n) {
    std::array<int, 10> result = {};
    for (; n > 0; n /= 10)
        ++result[static_cast<int>(n % 10)];
    return result;
}

int main() {
    int count = 0, dcount = 0;
    uint128_t magic = 0, p = 10;
    std::vector<int> digit_count;

    std::array<int, 10> digits0 = {1,1,1,1,1,1,1,1,1,1};
    std::array<int, 10> digits1 = {0,1,1,1,1,1,1,1,1,1};
    std::vector<uint128_t> pandigital0, pandigital1;

    for (magic_number_generator gen; gen.next(magic);) {
        if (magic >= p) {
            p *= 10;
            digit_count.push_back(dcount);
            dcount = 0;
        }
        auto digits = get_digits(magic);
        if (digits == digits0)
            pandigital0.push_back(magic);
        else if (digits == digits1)
            pandigital1.push_back(magic);
        ++count;
        ++dcount;
    }
    digit_count.push_back(dcount);

    std::cout << "There are " << count << " magic numbers.\n\n";
    std::cout << "The largest magic number is " << magic << ".\n\n";

    std::cout << "Magic number count by digits:\n";
    for (int i = 0; i < digit_count.size(); ++i)
        std::cout << i + 1 << '\t' << digit_count[i] << '\n';

    std::cout << "\nMagic numbers that are minimally pandigital in 1-9:\n";
    for (auto m : pandigital1)
        std::cout << m << '\n';

    std::cout << "\nMagic numbers that are minimally pandigital in 0-9:\n";
    for (auto m : pandigital0)
        std::cout << m << '\n';
}
Task: Super-d numbers


C++: #include <iostream>
#include <sstream>
#include <vector>

uint64_t ipow(uint64_t base, uint64_t exp) {
    uint64_t result = 1;
    while (exp) {
        if (exp & 1) {
            result *= base;
        }
        exp >>= 1;
        base *= base;
    }
    return result;
}

int main() {
    using namespace std;

    vector<string> rd{ "22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999" };

    for (uint64_t ii = 2; ii < 5; ii++) {
        cout << "First 10 super-" << ii << " numbers:\n";
        int count = 0;

        for (uint64_t j = 3; ; j++) {
            auto k = ii * ipow(j, ii);
            auto kstr = to_string(k);
            auto needle = rd[(size_t)(ii - 2)];
            auto res = kstr.find(needle);
            if (res != string::npos) {
                count++;
                cout << j << ' ';
                if (count == 10) {
                    cout << "\n\n";
                    break;
                }
            }
        }
    }

    return 0;
}
Task: Special variables


C++: #include <iostream>

struct SpecialVariables
{
    int i = 0;

    SpecialVariables& operator++()
    {
        
        
        
        this->i++;  

        
        return *this;
    }

};

int main()
{
    SpecialVariables sv;
    auto sv2 = ++sv;     
    std::cout << " sv :" << sv.i << "\n sv2:" << sv2.i << "\n";
}
Task: Arithmetic-geometric mean_Calculate Pi


C++: #include <gmpxx.h>
#include <chrono>

using namespace std;
using namespace chrono;
 
void agm(mpf_class& rop1, mpf_class& rop2, const mpf_class& op1,
        const mpf_class& op2)
{
    rop1 = (op1 + op2) / 2;
    rop2 = op1 * op2;
    mpf_sqrt(rop2.get_mpf_t(), rop2.get_mpf_t());
}
 
int main(void)
{
    auto st = steady_clock::now();
    mpf_set_default_prec(300000);
    mpf_class x0, y0, resA, resB, Z;
 
    x0 = 1;
    y0 = 0.5;
    Z  = 0.25;
    mpf_sqrt(y0.get_mpf_t(), y0.get_mpf_t());
 
    int n = 1;
    for (int i = 0; i < 8; i++) {
        agm(resA, resB, x0, y0);
        Z -= n * (resA - x0) * (resA - x0);
        n *= 2;
 
        agm(x0, y0, resA, resB);
        Z -= n * (x0 - resA) * (x0 - resA);
        n *= 2;
    }
 
    x0 = x0 * x0 / Z;
    printf("Took %f ms for computation.\n", duration<double>(steady_clock::now() - st).count() * 1000.0);
    st = steady_clock::now();
    gmp_printf ("%.89412Ff\n", x0.get_mpf_t());
    printf("Took %f ms for output.\n", duration<double>(steady_clock::now() - st).count() * 1000.0);
    return 0;
}
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);    }    }
Task: Random number generator (included)


C++: #include <iostream>
#include <string>
#include <random>
 
int main()
{
    std::random_device rd;
    std::uniform_int_distribution<int> dist(1, 10);
    std::mt19937 mt(rd());
    
    std::cout << "Random Number (hardware): " << dist(rd) << std::endl;
    std::cout << "Mersenne twister (hardware seeded): " << dist(mt) << std::endl;
}
Task: Cuban primes


C++: #include <iostream> 
#include <vector> 
#include <chrono>
#include <climits>
#include <cmath>

using namespace std;

vector <long long> primes{ 3, 5 };

int main()
{
	cout.imbue(locale(""));
	const int cutOff = 200, bigUn = 100000,
	          chunks = 50, little = bigUn / chunks;
    const char tn[] = " cuban prime";
	cout << "The first " << cutOff << tn << "s:" << endl;
	int c = 0;
	bool showEach = true;
	long long u = 0, v = 1;
	auto st = chrono::system_clock::now();

	for (long long i = 1; i <= LLONG_MAX; i++)
	{
		bool found = false;
		long long mx = (long long)(ceil(sqrt(v += (u += 6))));
		for (long long item : primes)
		{
			if (item > mx) break;
			if (v % item == 0) { found = true; break; }
		}
		if (!found)
		{
			c += 1; if (showEach)
			{
				for (long long z = primes.back() + 2; z <= v - 2; z += 2)
				{
					bool fnd = false;
					for (long long item : primes)
					{
						if (item > mx) break;
						if (z % item == 0) { fnd = true; break; }
					}
					if (!fnd) primes.push_back(z);
				}
				primes.push_back(v); cout.width(11); cout << v;
				if (c % 10 == 0) cout << endl;
				if (c == cutOff)
				{
					showEach = false;
					cout << "\nProgress to the " << bigUn << "th" << tn << ": ";
				}
			}
			if (c % little == 0) { cout << "."; if (c == bigUn) break; }
		}
	}
	cout << "\nThe " << c << "th" << tn << " is " << v;
	chrono::duration<double> elapsed_seconds = chrono::system_clock::now() - st;
	cout << "\nComputation time was " << elapsed_seconds.count() << " seconds" << endl;
	return 0;
}
Task: Lychrel numbers


C++: #include <iostream>
#include <map>
#include <vector>
#include <gmpxx.h>

using integer = mpz_class;

integer reverse(integer n) {
    integer rev = 0;
    while (n > 0) {
        rev = rev * 10 + (n % 10);
        n /= 10;
    }
    return rev;
}

void print_vector(const std::vector<integer>& vec) {
    if (vec.empty())
        return;
    auto i = vec.begin();
    std::cout << *i++;
    for (; i != vec.end(); ++i)
        std::cout << ", " << *i;
    std::cout << '\n';
}

int main() {
    std::map<integer, std::pair<bool, integer>> cache;
    std::vector<integer> seeds, related, palindromes;
    for (integer n = 1; n <= 10000; ++n) {
        std::pair<bool, integer> p(true, n);
        std::vector<integer> seen;
        integer rev = reverse(n);
        integer sum = n;
        for (int i = 0; i < 500; ++i) {
            sum += rev;
            rev = reverse(sum);
            if (rev == sum) {
                p.first = false;
                p.second = 0;
                break;
            }
            auto iter = cache.find(sum);
            if (iter != cache.end()) {
                p = iter->second;
                break;
            }
            seen.push_back(sum);
        }
        for (integer s : seen)
            cache.emplace(s, p);
        if (!p.first)
            continue;
        if (p.second == n)
            seeds.push_back(n);
        else
            related.push_back(n);
        if (n == reverse(n))
            palindromes.push_back(n);
    }
    std::cout << "number of seeds: " << seeds.size() << '\n';
    std::cout << "seeds: ";
    print_vector(seeds);
    std::cout << "number of related: " << related.size() << '\n';
    std::cout << "palindromes: ";
    print_vector(palindromes);
    return 0;
}