
#include "fxttypes.h"
#include "mod/mtypes.h"
#include "mod/isqrt.h"
#include "mod/primes.h"
#include "ds/bitarray.h"

//#include "jjassert.h"
//#include "fxtio.h"

#include <cmath>


static bitarray * oddprime_bitarray = 0;
ulong small_prime_limit = 64*1024;


class init_small_primes
{
    // (no data)
public:
    init_small_primes(ulong n)
    {
//        if ( oddprime_bitarray )  delete oddprime_bitarray;
        oddprime_bitarray = make_oddprime_bitarray(n, oddprime_bitarray);
    }

    ~init_small_primes()
    {
        delete oddprime_bitarray;
    }
};
// -------------------------

// automatic initialization:
static init_small_primes foo(small_prime_limit);


bool
is_small_prime(ulong n, const bitarray *ba/*=0*/)
// Return if n is a small prime.
// Return false if table of primes is not big enough.
{
    if ( 0==(n&1) )  return  (2==n); // n even: 2 is prime, else composite
    if ( n<=1 )  return  0;  // zero or one

    if ( 0==ba )  ba = oddprime_bitarray;
    ulong nh = n/2;
    if ( nh >= ba->n_ )  return false;
    return  ba->test( nh );
}
// -------------------------


ulong
next_small_prime(ulong n, const bitarray *ba/*=0*/)
// Return next prime >= n.
// Return zero if table of primes is not big enough.
{
    if ( n<=2 )  return 2;

    if ( 0==ba )  ba = oddprime_bitarray;
    ulong nh = n/2;

    n = ba->next_set( nh );
    if ( n==(ba->n_) )  return 0;
    return  2 * n + 1;
}
// -------------------------
