Task: First 9 prime Fibonacci number


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




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