Ejemplo n.º 1
0
// This algorithm factors a as a product of powers of elements of P if possible; otherwise it
// proclaims failure.
//
// Algorithm 20.1  [PDF page 25](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [findfactor test](test-findfactor.html) for basic usage.
int find_factor(mpz_array *out, const mpz_t a0, const mpz_t a, mpz_t *p, size_t from, size_t to) {
	mpz_t m, c, y, b, c2;
	size_t n = to - from;
	unsigned int r = 1;

	// If #P = 1: Find p ∈ P. Compute (n, c) ← reduce(p,a) by Algorithm 19.2. If
	// c != 1, proclaim failure and stop. Otherwise print (p,n) and stop
	if (n == 0) {
		mpz_init(m);
		mpz_init(c);
		reduce(m, c, p[from], a);
		if (mpz_cmp_ui(c, 1) != 0) {
			r = 0;
		} else {
			if (mpz_cmp(a0, p[from]) != 0) {
				mpz_init(y);
				mpz_fdiv_q(y, a0, p[from]);
				array_add(out, a0);
				array_add(out, p[from]);
				array_add(out, y);
				mpz_clear(y);
				r = 0;
			}
		}
		mpz_clear(m);
		mpz_clear(c);
		return r;
	}
	// Select Q ⊆ P with #Q = b#P/2c.

	// Compute y ← prod Q
	mpz_init(y);
	prod(y, p, from, to - n/2 - 1);

	// Compute (b, c) ← (ppi,ppo)(a, y)
	mpz_init(b);
	mpz_init(c2);
	ppi_ppo(b, c2, a, y);

	// Apply Algorithm 20.1 to (b,Q) recursively. If Algorithm 20.1 fails, proclaim
	// failure and stop.
	if (!find_factor(out, a0, b, p, from, to - n/2 - 1)) {
		r = 0;
	// Apply Algorithm 20.1 to (c,P−Q) recursively. If Algorithm 20.1 fails, proclaim
	// failure and stop.
	} else if (!find_factor(out, a0, c2, p, to - n/2, to)) {
		r = 0;
	}

	// Free the memory.
	mpz_clear(y);
	mpz_clear(b);
	mpz_clear(c2);
	return r;
}
Ejemplo n.º 2
0
gammatone::core::base<Scalar, GainPolicy>::
base(const Scalar& sample_frequency,
     const Scalar& center_frequency,
     const Scalar& bandwidth)
  : m_tau( 2.0*M_PI / sample_frequency ),
    m_factor( find_factor(sample_frequency, center_frequency, bandwidth) )
{}
Ejemplo n.º 3
0
int get_prime_factors(uint64_t n, uint64_t* factors){
	int k=0;
	if(n==1) factors[k++]=1; 
	int racine = sqrt(n);
	while(n!=1)
	{
		decomp dec = find_factor(n);
		if(dec.size!=0)
		{
			int i;
			for(i=0;i<dec.size;i++)
			{
				factors[k++]=dec.factors[i];
			}
		}else{
			uint64_t i=2;
			while(n%i!=0  && i<=racine)
			{
				i++;
			}
			if(i>racine)
			{
				factors[k++]=n;
				n=1; 
			}
			else
			{ 
				n/=i;
				factors[k++]=i;
			}
		}
	}
	return k;
}
Ejemplo n.º 4
0
main()
{ 
	long long  i,j,k,l,m,n,temp;
	long long  shit[900],f;
	double x;
	find_primer();
	while (scanf("%lld%lld",&n,&m)!=EOF)
 	{
		memset(f**k,0,sizeof(f**k));
		memset(shit,0,sizeof(shit));
		k=find_factor(m);
		for (i=n;i>1;i--)
		for (j=0;j<k;j++)
		{
			temp=i;
			while (temp%factor[j]==0)
			{
				temp/=factor[j];
				shit[factor[j]]++;
			}
		}
		int min=shit[factor[0]]/f**k[factor[0]];
		for (i=0;i<k;i++)
		{
			l=shit[factor[i]]/f**k[factor[i]];
			if (l<min) min=l;
		}
		x=0;
		for (i=2;i<=n;i++)
			x+=log((double)i);
		f=(long long)(x/log((double)m)+1e-4)+1;
		printf("%d %lld\n",min,f);
	}
	return 0;
}
Ejemplo n.º 5
0
// #### array verison
int array_find_factor(mpz_array *out, const mpz_t a, mpz_array *p) {
	if (p->used > 0)
		return find_factor(out, a, a, p->array, 0, p->used-1);
	else {
		fprintf(stderr, "array_printfactors on empty array\n");
		return 0;
	}
}
Ejemplo n.º 6
0
int main()
{
	scanf("%I64d%I64d",&n,&m);
	find_factor(m);
	ans=pow(m,n);
	temp=1;
	for(i=1;i<=top;i++)
	{
		sum=0;
		find(1,0,i);
		temp*=-1;
		ans+=temp*sum;
	}
	printf("%I64d\n",ans);
	return 0;
}
Ejemplo n.º 7
0
float find_SS(Letter *customer, Letter *product) {

    if (customer->letter == 0 || product->letter == 0)
        return 0;
    else {
        float SS;
        if (product->letter % 2 == 0)
            SS = customer->vowel * 1.5;
        else
            SS = customer->consonant;

        if (find_factor(customer->letter, product->letter) != 1)
            SS = SS * 1.5;
        return SS;
    }
}
Ejemplo n.º 8
0
static bool split_dims(unsigned int D, unsigned int N, long dims[N + 1], long (*ostrs[D])[N + 1], float blocking[N])
{
	if (0 == N)
		return false;

	long f;
	if ((dims[N - 1] > 1024) && (1 < (f = find_factor(dims[N - 1], blocking[N - 1])))) {
//	if ((dims[N - 1] > 64) && (1 < (f = find_factor(dims[N - 1], blocking[N - 1])))) {
#if 1
		//printf("Split with %ld=%ldx%ld\n", dims[N - 1], f, dims[N - 1] / f);

		dims[N] = dims[N - 1] / f;
		dims[N - 1] = f;

		for (unsigned int j = 0; j < D; j++)
			(*ostrs[j])[N] = (*ostrs[j])[N - 1] * f;

		blocking[N] = 1.;
#else
		dims[N] = 1;
		for (unsigned int j = 0; j < D; j++)
			(*ostrs[j])[N] = 0;
#endif
		return true;
	}

	// could not split, make room and try lower dimensions

	dims[N] = dims[N - 1];
	blocking[N] = blocking[N - 1];

	for (unsigned int j = 0; j < D; j++)
		(*ostrs[j])[N] = (*ostrs[j])[N - 1];

	if (split_dims(D, N - 1, dims, ostrs, blocking))
		return true;

	dims[N - 1] = dims[N];

	for (unsigned int j = 0; j < D; j++)
		(*ostrs[j])[N - 1] = (*ostrs[j])[N];

	blocking[N - 1] = blocking[N];

	return false;
}
Ejemplo n.º 9
0
SEXP data_frame_from_apop_data(apop_data *in){
    if (!in) return R_NilValue;
    int numeric_rows = !!(in->vector) 
                        + (in->matrix ? in->matrix->size2 : 0)
                        + !!(in->weights);
    int text_rows = in->textsize[1];
    SEXP out, onerow;
    PROTECT(out = allocVector(VECSXP, numeric_rows + text_rows));
    int col_ct = 0;
    int firstcol = in->vector ? -1 : 0;
    int lastcol = in->matrix ? in->matrix->size2 : 0;
    for (int i= firstcol; i < lastcol; i++){
        int len = (i == -1) ? in->vector->size : in->matrix->size1;
        apop_data *factorpage = find_factor(onerow, in, i);
        if (factorpage){
            SET_VECTOR_ELT(out, col_ct++, (onerow = allocVector(INTSXP, len)));
            for (int j=0; j< len; j++) INTEGER(onerow)[j] = apop_data_get(in, j, i);
            set_factor(onerow, factorpage);
        } else {
            SET_VECTOR_ELT(out, col_ct++, (onerow = allocVector(REALSXP, len)));
            for (int j=0; j< len; j++) REAL(onerow)[j] = apop_data_get(in, j, i);
        }
    }
    for (int i= 0; i < text_rows; i++){
        int len = in->textsize[0];
        SEXP onerow;
        SET_VECTOR_ELT(out, col_ct++, (onerow =  allocVector(STRSXP, len)));
        for (int j=0; j< len; j++) 
            SET_STRING_ELT(onerow, j, mkChar(in->text[j][i])); //Do I need strdup?
    }
    if (in->weights){
        int len =  in->weights->size;
        SET_VECTOR_ELT(out, col_ct++, (onerow = allocVector(REALSXP, len)));
        for (int j=0; j< len; j++) 
            REAL(onerow)[j] = gsl_vector_get(in->weights, j);
    }
    handle_names(in, out);
    UNPROTECT(1);
    return out;
}