Task: Pierpont primes


C++: #include <cassert>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
#include <gmpxx.h>

using big_int = mpz_class;

bool is_prime(const big_int& n) {
    return mpz_probab_prime_p(n.get_mpz_t(), 25);
}

template <typename integer>
class n_smooth_generator {
public:
    explicit n_smooth_generator(size_t n);
    integer next();
    size_t size() const {
        return results_.size();
    }
private:
    std::vector<size_t> primes_;
    std::vector<size_t> index_;
    std::vector<integer> results_;
    std::vector<integer> queue_;
};

template <typename integer>
n_smooth_generator<integer>::n_smooth_generator(size_t n) {
    for (size_t p = 2; p <= n; ++p) {
        if (is_prime(p)) {
            primes_.push_back(p);
            queue_.push_back(p);
        }
    }
    index_.assign(primes_.size(), 0);
    results_.push_back(1);
}

template <typename integer>
integer n_smooth_generator<integer>::next() {
    integer last = results_.back();
    for (size_t i = 0, n = primes_.size(); i < n; ++i) {
        if (queue_[i] == last)
            queue_[i] = results_[++index_[i]] * primes_[i];
    }
    results_.push_back(*min_element(queue_.begin(), queue_.end()));
    return last;
}

void print_vector(const std::vector<big_int>& numbers) {
    for (size_t i = 0, n = numbers.size(); i < n; ++i) {
        std::cout << std::setw(9) << numbers[i];
        if ((i + 1) % 10 == 0)
            std::cout << '\n';
    }
    std::cout << '\n';
}

int main() {
    const int max = 250;
    std::vector<big_int> first, second;
    int count1 = 0;
    int count2 = 0;
    n_smooth_generator<big_int> smooth_gen(3);
    big_int p1, p2;

    while (count1 < max || count2 < max) {
        big_int n = smooth_gen.next();
        if (count1 < max && is_prime(n + 1)) {
            p1 = n + 1;
            if (first.size() < 50)
                first.push_back(p1);
            ++count1;
        }
        if (count2 < max && is_prime(n - 1)) {
            p2 = n - 1;
            if (second.size() < 50)
                second.push_back(p2);
            ++count2;
        }
    }
    
    std::cout << "First 50 Pierpont primes of the first kind:\n";
    print_vector(first);
    std::cout << "First 50 Pierpont primes of the second kind:\n";
    print_vector(second);
    
    std::cout << "250th Pierpont prime of the first kind: " << p1 << '\n';
    std::cout << "250th Pierpont prime of the second kind: " << p2 << '\n';
    return 0;
}






Python: import random


def is_Prime(n):
    
    if n!=int(n):
        return False
    n=int(n)
    
    if n==0 or n==1 or n==4 or n==6 or n==8 or n==9:
        return False

    if n==2 or n==3 or n==5 or n==7:
        return True
    s = 0
    d = n-1
    while d%2==0:
        d>>=1
        s+=1
    assert(2**s * d == n-1)

    def trial_composite(a):
        if pow(a, d, n) == 1:
            return False
        for i in range(s):
            if pow(a, 2**i * d, n) == n-1:
                return False
        return True  

    for i in range(8):
        a = random.randrange(2, n)
        if trial_composite(a):
            return False

    return True

def pierpont(ulim, vlim, first):
    p = 0
    p2 = 1
    p3 = 1
    pp = []
    for v in xrange(vlim):
        for u in xrange(ulim):
            p = p2 * p3
            if first:
                p = p + 1
            else:
                p = p - 1
            if is_Prime(p):
                pp.append(p)
            p2 = p2 * 2
        p3 = p3 * 3
        p2 = 1
    pp.sort()
    return pp

def main():
    print "First 50 Pierpont primes of the first kind:"
    pp = pierpont(120, 80, True)
    for i in xrange(50):
        print "%8d " % pp[i],
        if (i - 9) % 10 == 0:
            print
    print "First 50 Pierpont primes of the second kind:"
    pp2 = pierpont(120, 80, False)
    for i in xrange(50):
        print "%8d " % pp2[i],
        if (i - 9) % 10 == 0:
            print
    print "250th Pierpont prime of the first kind:", pp[249]
    print "250th Pierpont prime of the second kind:", pp2[249]

main()