Beispiel #1
0
unsigned long int nCr(unsigned long int n, unsigned long int r) {
	mpz_t a,b,c, answer;
	unsigned long int ret;
	mpz_init(answer);
	mpz_init(a);
	mpz_init(b);
	mpz_init(c);

	if(n>r) {
			mpz_fac_ui(a,n);
			mpz_fac_ui(b,r);
			mpz_fac_ui(c,n-r);
			mpz_mul(b,b,c);
			mpz_divexact(answer,a,b);
	} else {
		mpz_set_ui(answer,0);
	}

	ret = mpz_get_ui(answer);
	mpz_clear(answer);
	mpz_clear(a);
	mpz_clear(b);
	mpz_clear(c);
	return ret;
}
Beispiel #2
0
mpz_class choose(int n, int k)
{
	mpz_class n_fac;
	mpz_fac_ui(n_fac.get_mpz_t(), n);

	mpz_class k_fac;
	mpz_fac_ui(k_fac.get_mpz_t(), k);

	mpz_class nk_fac;
	mpz_fac_ui(nk_fac.get_mpz_t(), n - k);

	return n_fac / (k_fac * nk_fac);
}
Beispiel #3
0
int main(void)
{
    mpz_t a, b;
    mpz_init(a);
    mpz_init(b);

    /* 40C20 */
    mpz_fac_ui(a, 40);
    mpz_fac_ui(b, 20);
    mpz_mul(b, b, b);
    mpz_divexact(a, a, b);

    gmp_printf("%Zd\n", a);
    return 0;
}
Beispiel #4
0
/* Test all fac(n) cases, with 0 <= n <= limit.  */
void
fac_smallexaustive (unsigned int limit)
{
  mpz_t          f, r;
  unsigned long  n;
  mpz_init_set_si (f, 1);  /* 0! = 1 */
  mpz_init (r);

  for (n = 0; n < limit; n++)
    {
      mpz_fac_ui (r, n);

      if (mpz_cmp (f, r) != 0)
        {
          printf ("mpz_fac_ui(%lu) wrong\n", n);
          printf ("  got  "); mpz_out_str (stdout, 10, r); printf("\n");
          printf ("  want "); mpz_out_str (stdout, 10, f); printf("\n");
          abort ();
        }

      mpz_mul_ui (f, f, n+1);  /* (n+1)! = n! * (n+1) */
    }

  mpz_clear (f);
  mpz_clear (r);
}
Beispiel #5
0
void fmpz_fac_ui(fmpz_t f, ulong n)
{
    if (n < FLINT_NUM_TINY_FACTORIALS)
        fmpz_set_ui(f, flint_tiny_factorials[n]);
    else
        mpz_fac_ui(_fmpz_promote(f), n);
}
Beispiel #6
0
int main(int argc, char* argv[]) {
  uint32_t number = 100; /* Default */
  uint32_t i,sum;
  char     *str;
  mpz_t factorial;

  if(argc > 1 &&!strcmp(argv[1],"--help"))
    return print_help(argv[0]), EXIT_SUCCESS;
  if(argc > 1)
    sscanf(argv[1],"%u",&number);

  mpz_init(factorial);
  mpz_fac_ui(factorial,number);

  str = malloc((mpz_sizeinbase(factorial,10)+2) * sizeof *str);
  gmp_sprintf(str,"%Zd",factorial);

  for(i=sum=0; str[i]; ++i)
    sum += str[i] - ASCII_OFFSET;

  printf("%u\n",sum);
  free(str);
  mpz_clear(factorial);
  return EXIT_SUCCESS;
}
Beispiel #7
0
int main( void )
{
    
    mpz_t num, digit;
    unsigned long long sum;

    mpz_init( num );
    mpz_init( digit );
    sum = 0;

    /******************************************************
     *  void mpz_fac_ui (mpz_t rop, unsigned long int op)
     *  
     *  -> Sets 'rop'  to 'op!'
     *
     ******************************************************/
    mpz_fac_ui( num, 100 );
    

    while( mpz_sgn( num ) ) // while num > 0
    {
        sum += mpz_mod_ui( digit, num, 10 );
        mpz_fdiv_q_ui( num, num, 10 );
    }

    printf( "Answer: %llu\n", sum );

    mpz_clear( num );
    return 0;
}
Beispiel #8
0
int main (int argc,const char *argv[]){
	mpz_t result;
	mpz_t sum;
	mpz_t aux;

	mpz_init(result);
	mpz_init(sum);
	mpz_init(aux);

	mpz_fac_ui(result,100);

	while(mpz_sgn (result)>0){
		mpz_mod_ui(aux, result, 10);
		mpz_add(sum,sum,aux);
		mpz_div_ui (result, result, (unsigned long int) 10);
	}

	gmp_printf("%Zd\n", sum);

	/* free used memory */
	mpz_clear(sum);
	mpz_clear(aux);
	mpz_clear(result);
	return EXIT_SUCCESS;
}
Beispiel #9
0
num_t
num_int_one_op(optype_t op_type, num_t a)
{
	num_t r;

	r = num_new_z(N_TEMP, NULL);
	a = num_new_z(N_TEMP, a);

	switch (op_type) {
	case OP_UINV:
		mpz_com(Z(r), Z(a));
		break;

	case OP_FAC:
		if (!mpz_fits_ulong_p(Z(a))) {
			yyxerror
			    ("Argument to factorial needs to fit into an unsigned long C datatype");
			return NULL;
		}
		mpz_fac_ui(Z(r), mpz_get_ui(Z(a)));
		break;

	default:
		yyxerror("Unknown op in num_int_one_op");
	}

	return r;
}
Beispiel #10
0
int Problem020(void) {
	mpz_t fac;
	int sum, i, size;
	char *buf;
	char ish[2];

	ish[1] = '\0';
	mpz_init(fac);

	mpz_fac_ui(fac, 100);
	size = mpz_sizeinbase(fac,10) + 1;
	buf = malloc(sizeof(char) * (size));

	mpz_get_str(buf,10,fac);

	for(i=0,sum=0;i<size;i++) {
		ish[0] = *(buf+i);
		sum+=atoi(ish);
	}
	printf("%30d",sum);

	free(buf);
	mpz_clear(fac);
	return 0;
}
Beispiel #11
0
/* test gamma on some integral values (from Christopher Creutzig). */
static void
gamma_integer (void)
{
  mpz_t n;
  mpfr_t x, y;
  unsigned int i;

  mpz_init (n);
  mpfr_init2 (x, 149);
  mpfr_init2 (y, 149);

  for (i = 0; i < 100; i++)
    {
      mpz_fac_ui (n, i);
      mpfr_set_ui (x, i+1, GMP_RNDN);
      mpfr_gamma (y, x, GMP_RNDN);
      mpfr_set_z (x, n, GMP_RNDN);
      if (!mpfr_equal_p (x, y))
        {
          printf ("Error for gamma(%d)\n", i+1);
          printf ("expected "); mpfr_dump (x);
          printf ("got      "); mpfr_dump (y);
          exit (1);
        }
    }
  mpfr_clear (y);
  mpfr_clear (x);
  mpz_clear (n);
}
Beispiel #12
0
// Factorial
RCP<const Integer> factorial(unsigned long n)
{
    mpz_class f;

    mpz_fac_ui(f.get_mpz_t(), n);

    return integer(f);
}
//------------------------------------------------------------------------------
// Name: factorial
//------------------------------------------------------------------------------
knumber_base *knumber_integer::factorial() {

	if(sign() < 0) {
		delete this;
		return new knumber_error(knumber_error::ERROR_UNDEFINED);
	}

	mpz_fac_ui(mpz_, mpz_get_ui(mpz_));
	return this;
}
Beispiel #14
0
void checkWilson (mpz_t f, unsigned long n)
{
  unsigned long m;

  mpz_fac_ui (f, n - 1);
  m = mpz_fdiv_ui (f, n);
  if ( m != n - 1)
    {
      printf ("mpz_fac_ui(%lu) wrong\n", n - 1);
      printf ("  Wilson's theorem not verified: got %lu, expected %lu.\n",m ,n - 1);
      abort ();
    }
}
Beispiel #15
0
void QtGMP::fac()
{
    QString s("");
    mpz_t aa,bb;
    char *str=0;
    mpz_init(bb);
    mpz_init_set_str(aa,qPrintable(this->lineEdit2->text()),10);

    mpz_fac_ui(bb,mpz_get_ui(aa));
    // gmp_printf("B!:\t%Zd\nA!:\t%Zd\n\n\n",aa,bb);
    s.sprintf("B:\t%s\nB!:\t%s\n",mpz_get_str(str,10,aa),mpz_get_str(str,10,bb));
    this->textEdit->setText(s);
    mpz_clears(aa,bb,'\0');
}
Beispiel #16
0
void w3j_Delta_sq(mpq_t r, long j1, long j2, long j3)
{
  // n = (j1+j2-j3)! (j1-j2+j3)! (-j1+j2+j3)!
  // d = (j1+j2+j3+1)!

  mpz_t h;
  mpz_init(h);
  mpq_set_ui(r,1,1);

  mpz_fac_ui(h,j1+j2-j3);
  mpz_mul(mpq_numref(r),mpq_numref(r),h);
  mpz_fac_ui(h,j1+j2+j3+1);
  mpz_mul(mpq_denref(r),mpq_denref(r),h);
  mpq_canonicalize(r);
  mpz_fac_ui(h,j1-j2+j3);
  mpz_mul(mpq_numref(r),mpq_numref(r),h);
  mpz_fac_ui(h,j2+j3-j1);
  mpz_mul(mpq_numref(r),mpq_numref(r),h);

  mpq_canonicalize(r);

  mpz_clear(h);
}
Beispiel #17
0
static Variant HHVM_FUNCTION(gmp_fact,
                             const Variant& data) {
  mpz_t gmpReturn;

  if (data.isResource()) {
    mpz_t gmpData;
    if (!variantToGMPData(cs_GMP_FUNC_NAME_GMP_FACT, gmpData, data, 0, true)) {
      return false;
    }

    if (mpz_sgn(gmpData) < 0) {
      mpz_clear(gmpData);

      raise_warning(cs_GMP_INVALID_VALUE_MUST_BE_POSITIVE,
                    cs_GMP_FUNC_NAME_GMP_FACT);
      return false;
    }

    mpz_init(gmpReturn);
    mpz_fac_ui(gmpReturn, mpz_get_ui(gmpData));
    mpz_clear(gmpData);
  } else {
    if (data.toInt64() < 0) {
      raise_warning(cs_GMP_INVALID_VALUE_MUST_BE_POSITIVE,
                    cs_GMP_FUNC_NAME_GMP_FACT);
      return false;
    }

    mpz_init(gmpReturn);
    mpz_fac_ui(gmpReturn, data.toInt64());
  }
  Variant ret = NEWOBJ(GMPResource)(gmpReturn);

  mpz_clear(gmpReturn);

  return ret;
}
Beispiel #18
0
static void
test_int (void)
{
  unsigned long n0 = 1, n1 = 80, n;
  mpz_t f;
  mpfr_t x, y;
  mp_prec_t prec_f, p;
  int r;
  int inex1, inex2;

  mpz_init (f);
  mpfr_init (x);
  mpfr_init (y);

  mpz_fac_ui (f, n0 - 1);
  for (n = n0; n <= n1; n++)
    {
      mpz_mul_ui (f, f, n); /* f = n! */
      prec_f = mpz_sizeinbase (f, 2) - mpz_scan1 (f, 0);
      for (p = MPFR_PREC_MIN; p <= prec_f; p++)
        {
          mpfr_set_prec (x, p);
          mpfr_set_prec (y, p);
          for (r = 0; r < GMP_RND_MAX; r++)
            {
              inex1 = mpfr_fac_ui (x, n, (mp_rnd_t) r);
              inex2 = mpfr_set_z (y, f, (mp_rnd_t) r);
              if (mpfr_cmp (x, y))
                {
                  printf ("Error for n=%lu prec=%lu rnd=%s\n",
                          n, (unsigned long) p, mpfr_print_rnd_mode ((mp_rnd_t) r));
                  exit (1);
                }
              if ((inex1 < 0 && inex2 >= 0) || (inex1 == 0 && inex2 != 0)
                  || (inex1 > 0 && inex2 <= 0))
                {
                  printf ("Wrong inexact flag for n=%lu prec=%lu rnd=%s\n",
                          n, (unsigned long) p, mpfr_print_rnd_mode ((mp_rnd_t) r));
                  exit (1);
                }
            }
        }
    }

  mpz_clear (f);
  mpfr_clear (x);
  mpfr_clear (y);
}
Beispiel #19
0
static PyObject *
GMPy_MPZ_Function_Fac(PyObject *self, PyObject *other)
{
    MPZ_Object *result = NULL;
    unsigned long n;

    n = c_ulong_From_Integer(other);
    if (n == (unsigned long)(-1) && PyErr_Occurred()) {
        return NULL;
    }

    if ((result = GMPy_MPZ_New(NULL))) {
        mpz_fac_ui(result->z, n);
    }
    return (PyObject*)result;
}
Beispiel #20
0
APLVFP PrimFnMonQuoteDotVisV
    (APLVFP     aplVfpRht,
     LPPRIMSPEC lpPrimSpec)

{
    APLMPI mpzRes = {0};
    APLVFP mpfRes = {0};

    // Check for indeterminates:  !N for integer N < 0
    if (mpfr_integer_p (&aplVfpRht)
     && mpfr_cmp_ui (&aplVfpRht, 0) < 0)
        return *mpfr_QuadICValue (&aplVfpRht,       // No left arg
                                   ICNDX_QDOTn,
                                  &aplVfpRht,
                                  &mpfRes,
                                   FALSE);
    // Check for PosInfinity
    if (IsMpfPosInfinity (&aplVfpRht))
        return mpfPosInfinity;

    // If the arg is an integer,
    //   and it fits in a ULONG, ...
    if (mpfr_integer_p (&aplVfpRht)
     && mpfr_fits_uint_p (&aplVfpRht, MPFR_RNDN))
    {
        mpz_init   (&mpzRes);
        mpfr_init0 (&mpfRes);

        mpz_fac_ui (&mpzRes, mpfr_get_ui (&aplVfpRht, MPFR_RNDN));
        mpfr_set_z (&mpfRes, &mpzRes, MPFR_RNDN);

        Myz_clear (&mpzRes);
    } else
    {
        // Initialize the result
        mpfr_init_set (&mpfRes, &aplVfpRht, MPFR_RNDN);
        mpfr_add_ui   (&mpfRes, &mpfRes, 1, MPFR_RNDN);

        // Let MPFR handle it
        mpfr_gamma (&mpfRes, &mpfRes, MPFR_RNDN);
#ifdef DEBUG
        mpfr_free_cache ();
#endif
    } // End IF/ELSE

    return mpfRes;
} // End PrimFnMonQuoteDotVisV
Beispiel #21
0
int main(void)
{
    mpz_t a, r;
    mpz_init(a);
    mpz_init(r);

    int digsum = 0;
    mpz_fac_ui(a, 100);

    while (mpz_cmp_ui(a, 0)) {
        mpz_tdiv_qr_ui(a, r, a, 10);
        digsum += mpz_get_ui(r);
    }

    printf("%d\n", digsum);
    return 0;
}
mpz_class PrimeSwing::Factorial( int _number )
{
    mpz_class result;

    // For very small n the 'NaiveFactorial' is OK.
    if (_number < 20)
    {
        mpz_fac_ui(result.get_mpz_t(), _number);
        return result;
    }

    PrimeSieve sieve(_number);
    RecFactorial(result, _number, sieve);
    result <<= ( _number - UtilityFunctions::BitCount(_number) );

    return result;
}
Beispiel #23
0
int main(void)
{
  mpz_t n;
  char *str;
  int i;
  int sum = 0;

  mpz_init(n);
  mpz_fac_ui(n, 100);
  str = mpz_get_str(NULL, 10, n);
  for (i = 0; str[i]; i++) {
    sum += str[i]-'0';
  }
  printf("%d\n", sum);

  free(str);
  mpz_clear(n);

  return 0;
}
Beispiel #24
0
int main() {
    bool outcomes[N];
    mpz_class sum = 0;

    for (int l = N / 2 + 1; l <= N; ++l) {
	std::fill_n(outcomes, l, false); // blue discs
	std::fill_n(outcomes + l, N - l, true);

	do {
	    mpz_class p = 1;
	    for (int i = 0; i < N; ++i)
		if (outcomes[i]) p *= i + 1;
	    sum += p;
	} while (std::next_permutation(outcomes, outcomes + N));
    }

    mpz_class prod;
    mpz_fac_ui(prod.get_mpz_t(), N + 1);

    std::cout << prod / sum << "\n";
    return 0;
}
Beispiel #25
0
APLRAT PrimFnMonQuoteDotRisR
    (APLRAT     aplRatRht,
     LPPRIMSPEC lpPrimSpec)

{
    APLRAT mpqRes = {0};
    UINT   uRht;

    // Check for indeterminates:  !N for integer N < 0
    if (mpq_integer_p (&aplRatRht)
     && mpq_cmp_ui (&aplRatRht, 0, 1) < 0)
        return *mpq_QuadICValue (&aplRatRht,        // No left arg
                                  ICNDX_QDOTn,
                                 &aplRatRht,
                                 &mpqRes,
                                  FALSE);
    // Check for PosInfinity
    if (IsMpqPosInfinity (&aplRatRht))
        return mpqPosInfinity;

    // If the denominator is 1,
    //   and the numerator fts in a UINT, ...
    if (mpq_integer_p (&aplRatRht)
     && mpz_fits_slong_p (mpq_numref (&aplRatRht)) NE 0)
    {
        // Initialize the result to 0/1
        mpq_init (&mpqRes);

        // Extract the numerator
        uRht = (UINT) mpz_get_si (mpq_numref (&aplRatRht));

        // Compute the factorial
        mpz_fac_ui (mpq_numref (&mpqRes), uRht);
    } else
        RaiseException (EXCEPTION_RESULT_VFP, 0, 0, NULL);

    return mpqRes;
} // End PrimFnMonQuoteDotRisR
Beispiel #26
0
/* Evaluate the expression E and put the result in R.  */
void
mpz_eval_expr (mpz_ptr r, expr_t e)
{
  mpz_t lhs, rhs;

  switch (e->op)
    {
    case LIT:
      mpz_set (r, e->operands.val);
      return;
    case PLUS:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_add (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case MINUS:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_sub (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case MULT:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_mul (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case DIV:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_fdiv_q (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case MOD:
      mpz_init (rhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_abs (rhs, rhs);
      mpz_eval_mod_expr (r, e->operands.ops.lhs, rhs);
      mpz_clear (rhs);
      return;
    case REM:
      /* Check if lhs operand is POW expression and optimize for that case.  */
      if (e->operands.ops.lhs->op == POW)
	{
	  mpz_t powlhs, powrhs;
	  mpz_init (powlhs);
	  mpz_init (powrhs);
	  mpz_init (rhs);
	  mpz_eval_expr (powlhs, e->operands.ops.lhs->operands.ops.lhs);
	  mpz_eval_expr (powrhs, e->operands.ops.lhs->operands.ops.rhs);
	  mpz_eval_expr (rhs, e->operands.ops.rhs);
	  mpz_powm (r, powlhs, powrhs, rhs);
	  if (mpz_cmp_si (rhs, 0L) < 0)
	    mpz_neg (r, r);
	  mpz_clear (powlhs);
	  mpz_clear (powrhs);
	  mpz_clear (rhs);
	  return;
	}

      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_fdiv_r (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#if __GNU_MP_VERSION >= 2
    case INVMOD:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_invert (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#endif
    case POW:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      if (mpz_cmpabs_ui (lhs, 1) <= 0)
	{
	  /* For 0^rhs and 1^rhs, we just need to verify that
	     rhs is well-defined.  For (-1)^rhs we need to
	     determine (rhs mod 2).  For simplicity, compute
	     (rhs mod 2) for all three cases.  */
	  expr_t two, et;
	  two = malloc (sizeof (struct expr));
	  two -> op = LIT;
	  mpz_init_set_ui (two->operands.val, 2L);
	  makeexp (&et, MOD, e->operands.ops.rhs, two);
	  e->operands.ops.rhs = et;
	}

      mpz_eval_expr (rhs, e->operands.ops.rhs);
      if (mpz_cmp_si (rhs, 0L) == 0)
	/* x^0 is 1 */
	mpz_set_ui (r, 1L);
      else if (mpz_cmp_si (lhs, 0L) == 0)
	/* 0^y (where y != 0) is 0 */
	mpz_set_ui (r, 0L);
      else if (mpz_cmp_ui (lhs, 1L) == 0)
	/* 1^y is 1 */
	mpz_set_ui (r, 1L);
      else if (mpz_cmp_si (lhs, -1L) == 0)
	/* (-1)^y just depends on whether y is even or odd */
	mpz_set_si (r, (mpz_get_ui (rhs) & 1) ? -1L : 1L);
      else if (mpz_cmp_si (rhs, 0L) < 0)
	/* x^(-n) is 0 */
	mpz_set_ui (r, 0L);
      else
	{
	  unsigned long int cnt;
	  unsigned long int y;
	  /* error if exponent does not fit into an unsigned long int.  */
	  if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0)
	    goto pow_err;

	  y = mpz_get_ui (rhs);
	  /* x^y == (x/(2^c))^y * 2^(c*y) */
#if __GNU_MP_VERSION >= 2
	  cnt = mpz_scan1 (lhs, 0);
#else
	  cnt = 0;
#endif
	  if (cnt != 0)
	    {
	      if (y * cnt / cnt != y)
		goto pow_err;
	      mpz_tdiv_q_2exp (lhs, lhs, cnt);
	      mpz_pow_ui (r, lhs, y);
	      mpz_mul_2exp (r, r, y * cnt);
	    }
	  else
	    mpz_pow_ui (r, lhs, y);
	}
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    pow_err:
      error = "result of `pow' operator too large";
      mpz_clear (lhs); mpz_clear (rhs);
      longjmp (errjmpbuf, 1);
    case GCD:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_gcd (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1
    case LCM:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_lcm (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#endif
    case AND:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_and (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case IOR:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_ior (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1
    case XOR:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_xor (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#endif
    case NEG:
      mpz_eval_expr (r, e->operands.ops.lhs);
      mpz_neg (r, r);
      return;
    case NOT:
      mpz_eval_expr (r, e->operands.ops.lhs);
      mpz_com (r, r);
      return;
    case SQRT:
      mpz_init (lhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      if (mpz_sgn (lhs) < 0)
	{
	  error = "cannot take square root of negative numbers";
	  mpz_clear (lhs);
	  longjmp (errjmpbuf, 1);
	}
      mpz_sqrt (r, lhs);
      return;
#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1
    case ROOT:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      if (mpz_sgn (rhs) <= 0)
	{
	  error = "cannot take non-positive root orders";
	  mpz_clear (lhs); mpz_clear (rhs);
	  longjmp (errjmpbuf, 1);
	}
      if (mpz_sgn (lhs) < 0 && (mpz_get_ui (rhs) & 1) == 0)
	{
	  error = "cannot take even root orders of negative numbers";
	  mpz_clear (lhs); mpz_clear (rhs);
	  longjmp (errjmpbuf, 1);
	}

      {
	unsigned long int nth = mpz_get_ui (rhs);
	if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0)
	  {
	    /* If we are asked to take an awfully large root order, cheat and
	       ask for the largest order we can pass to mpz_root.  This saves
	       some error prone special cases.  */
	    nth = ~(unsigned long int) 0;
	  }
	mpz_root (r, lhs, nth);
      }
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#endif
    case FAC:
      mpz_eval_expr (r, e->operands.ops.lhs);
      if (mpz_size (r) > 1)
	{
	  error = "result of `!' operator too large";
	  longjmp (errjmpbuf, 1);
	}
      mpz_fac_ui (r, mpz_get_ui (r));
      return;
#if __GNU_MP_VERSION >= 2
    case POPCNT:
      mpz_eval_expr (r, e->operands.ops.lhs);
      { long int cnt;
	cnt = mpz_popcount (r);
	mpz_set_si (r, cnt);
      }
      return;
    case HAMDIST:
      { long int cnt;
	mpz_init (lhs); mpz_init (rhs);
	mpz_eval_expr (lhs, e->operands.ops.lhs);
	mpz_eval_expr (rhs, e->operands.ops.rhs);
	cnt = mpz_hamdist (lhs, rhs);
	mpz_clear (lhs); mpz_clear (rhs);
	mpz_set_si (r, cnt);
      }
      return;
#endif
    case LOG2:
      mpz_eval_expr (r, e->operands.ops.lhs);
      { unsigned long int cnt;
	if (mpz_sgn (r) <= 0)
	  {
	    error = "logarithm of non-positive number";
	    longjmp (errjmpbuf, 1);
	  }
	cnt = mpz_sizeinbase (r, 2);
	mpz_set_ui (r, cnt - 1);
      }
      return;
    case LOG:
      { unsigned long int cnt;
	mpz_init (lhs); mpz_init (rhs);
	mpz_eval_expr (lhs, e->operands.ops.lhs);
	mpz_eval_expr (rhs, e->operands.ops.rhs);
	if (mpz_sgn (lhs) <= 0)
	  {
	    error = "logarithm of non-positive number";
	    mpz_clear (lhs); mpz_clear (rhs);
	    longjmp (errjmpbuf, 1);
	  }
	if (mpz_cmp_ui (rhs, 256) >= 0)
	  {
	    error = "logarithm base too large";
	    mpz_clear (lhs); mpz_clear (rhs);
	    longjmp (errjmpbuf, 1);
	  }
	cnt = mpz_sizeinbase (lhs, mpz_get_ui (rhs));
	mpz_set_ui (r, cnt - 1);
	mpz_clear (lhs); mpz_clear (rhs);
      }
      return;
    case FERMAT:
      {
	unsigned long int t;
	mpz_init (lhs);
	mpz_eval_expr (lhs, e->operands.ops.lhs);
	t = (unsigned long int) 1 << mpz_get_ui (lhs);
	if (mpz_cmp_ui (lhs, ~(unsigned long int) 0) > 0 || t == 0)
	  {
	    error = "too large Mersenne number index";
	    mpz_clear (lhs);
	    longjmp (errjmpbuf, 1);
	  }
	mpz_set_ui (r, 1);
	mpz_mul_2exp (r, r, t);
	mpz_add_ui (r, r, 1);
	mpz_clear (lhs);
      }
      return;
    case MERSENNE:
      mpz_init (lhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      if (mpz_cmp_ui (lhs, ~(unsigned long int) 0) > 0)
	{
	  error = "too large Mersenne number index";
	  mpz_clear (lhs);
	  longjmp (errjmpbuf, 1);
	}
      mpz_set_ui (r, 1);
      mpz_mul_2exp (r, r, mpz_get_ui (lhs));
      mpz_sub_ui (r, r, 1);
      mpz_clear (lhs);
      return;
    case FIBONACCI:
      { mpz_t t;
	unsigned long int n, i;
	mpz_init (lhs);
	mpz_eval_expr (lhs, e->operands.ops.lhs);
	if (mpz_sgn (lhs) <= 0 || mpz_cmp_si (lhs, 1000000000) > 0)
	  {
	    error = "Fibonacci index out of range";
	    mpz_clear (lhs);
	    longjmp (errjmpbuf, 1);
	  }
	n = mpz_get_ui (lhs);
	mpz_clear (lhs);

#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1
	mpz_fib_ui (r, n);
#else
	mpz_init_set_ui (t, 1);
	mpz_set_ui (r, 1);

	if (n <= 2)
	  mpz_set_ui (r, 1);
	else
	  {
	    for (i = 3; i <= n; i++)
	      {
		mpz_add (t, t, r);
		mpz_swap (t, r);
	      }
	  }
	mpz_clear (t);
#endif
      }
      return;
    case RANDOM:
      {
	unsigned long int n;
	mpz_init (lhs);
	mpz_eval_expr (lhs, e->operands.ops.lhs);
	if (mpz_sgn (lhs) <= 0 || mpz_cmp_si (lhs, 1000000000) > 0)
	  {
	    error = "random number size out of range";
	    mpz_clear (lhs);
	    longjmp (errjmpbuf, 1);
	  }
	n = mpz_get_ui (lhs);
	mpz_clear (lhs);
	mpz_urandomb (r, rstate, n);
      }
      return;
    case NEXTPRIME:
      {
	mpz_eval_expr (r, e->operands.ops.lhs);
	mpz_nextprime (r, r);
      }
      return;
    case BINOM:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      {
	unsigned long int k;
	if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0)
	  {
	    error = "k too large in (n over k) expression";
	    mpz_clear (lhs); mpz_clear (rhs);
	    longjmp (errjmpbuf, 1);
	  }
	k = mpz_get_ui (rhs);
	mpz_bin_ui (r, lhs, k);
      }
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case TIMING:
      {
	int t0;
	t0 = cputime ();
	mpz_eval_expr (r, e->operands.ops.lhs);
	printf ("time: %d\n", cputime () - t0);
      }
      return;
    default:
      abort ();
    }
}
Beispiel #27
0
int
main (int argc, char *argv[])
{
  unsigned long  n, m;
  unsigned long  limit = 2222;
  mpz_t          df[2], f, r;

  tests_start ();

  if (argc > 1 && argv[1][0] == 'x')
    limit = ULONG_MAX;
  else if (argc > 1)
    limit = atoi (argv[1]);

  /* for small limb testing */
  limit = MIN (limit, MP_LIMB_T_MAX);

  mpz_init_set_ui (df[0], 1);  /* 0!! = 1 */
  mpz_init_set_ui (df[1], 1);  /* -1!! = 1 */
  mpz_init_set_ui (f, 1);  /* 0! = 1 */
  mpz_init (r);

  for (n = 0, m = 0; n < limit; n++)
    {
      mpz_fac_ui (r, n);
      MPZ_CHECK_FORMAT (r);

      if (mpz_cmp (f, r) != 0)
        {
          printf ("mpz_fac_ui(%lu) wrong\n", n);
          printf ("  got  "); mpz_out_str (stdout, 10, r); printf("\n");
          printf ("  want "); mpz_out_str (stdout, 10, f); printf("\n");
          abort ();
        }

      mpz_2fac_ui (r, n);
      MPZ_CHECK_FORMAT (r);

      if (mpz_cmp (df[m], r) != 0)
        {
          printf ("mpz_2fac_ui(%lu) wrong\n", n);
          printf ("  got  "); mpz_out_str (stdout, 10, r); printf("\n");
          printf ("  want "); mpz_out_str (stdout, 10, df[m]); printf("\n");
          abort ();
        }

      m ^= 1;
      mpz_mul_ui (df[m], df[m], n+1);  /* (n+1)!! = (n-1)!! * (n+1) */
      mpz_mul_ui (f, f, n+1);  /* (n+1)! = n! * (n+1) */
    }

  n = 1048573; /* a prime */
  if (n > MP_LIMB_T_MAX)
    n = 65521; /* a smaller prime :-) */
  mpz_fac_ui (f, n - 1);
  m = mpz_fdiv_ui (f, n);
  if ( m != n - 1)
    {
      printf ("mpz_fac_ui(%lu) wrong\n", n - 1);
      printf ("  Wilson's theorem not verified: got %lu, expected %lu.\n",m ,n - 1);
      abort ();
    }

  mpz_clear (df[0]);
  mpz_clear (df[1]);
  mpz_clear (f);
  mpz_clear (r);

  tests_end ();

  exit (0);
}
Beispiel #28
0
static void
test_special2z (int (*mpfr_func)(mpfr_ptr, mpz_srcptr, mpfr_srcptr, mpfr_rnd_t),
               void (*mpz_func)(mpz_ptr, mpz_srcptr, mpz_srcptr),
               const char *op)
{
  mpfr_t x1, x2;
  mpz_t  z1, z2;
  int res;

  mpfr_inits2 (128, x1, x2, (mpfr_ptr) 0);
  mpz_init (z1); mpz_init(z2);
  mpz_fac_ui (z1, 19); /* 19!+1 fits perfectly in a 128 bits mantissa */
  mpz_add_ui (z1, z1, 1);
  mpz_fac_ui (z2, 20); /* 20!+1 fits perfectly in a 128 bits mantissa */
  mpz_add_ui (z2, z2, 1);

  res = mpfr_set_z(x1, z1, MPFR_RNDN);
  if (res)
    {
      printf("Special2z %s: set_z1 error\n", op);
      exit(1);
    }
  mpfr_set_z (x2, z2, MPFR_RNDN);
  if (res)
    {
      printf("Special2z %s: set_z2 error\n", op);
      exit(1);
    }

  /* (19!+1) * (20!+1) fits in a 128 bits number */
  res = mpfr_func(x1, z1, x2, MPFR_RNDN);
  if (res)
    {
      printf("Special2z %s: wrong inexact flag.\n", op);
      exit(1);
    }
  mpz_func(z1, z1, z2);
  res = mpfr_set_z (x2, z1, MPFR_RNDN);
  if (res)
    {
      printf("Special2z %s: set_z2 error\n", op);
      exit(1);
    }
  if (mpfr_cmp(x1, x2))
    {
      printf("Special2z %s: results differ.\nx1=", op);
      mpfr_print_binary(x1);
      printf("\nx2=");
      mpfr_print_binary(x2);
      printf ("\nZ2=");
      mpz_out_str (stdout, 2, z1);
      putchar('\n');
      exit(1);
    }

  mpz_set_ui (z1, 0);
  mpz_set_ui (z2, 1);
  mpfr_set_ui (x2, 1, MPFR_RNDN);
  res = mpfr_func(x1, z1, x2, MPFR_RNDN);
  mpz_func (z1, z1, z2);
  mpfr_set_z (x2, z1, MPFR_RNDN);
  if (mpfr_cmp(x1, x2))
    {
      printf("Special2z %s: results differ(2).\nx1=", op);
      mpfr_print_binary(x1);
      printf("\nx2=");
      mpfr_print_binary(x2);
      putchar('\n');
      exit(1);
    }

  mpz_clear (z1); mpz_clear(z2);
  mpfr_clears (x1, x2, (mpfr_ptr) 0);
}
Beispiel #29
0
Integer factorial(unsigned long n) {
	Integer result;
	mpz_fac_ui(result.backend().data(), n);
	return result;
}
Beispiel #30
0
int
main (int argc, char *argv[])
{
  mpz_t ref[MULTIFAC_WHEEL], ref2[MULTIFAC_WHEEL2], res;
  unsigned long n, j, m, m2;
  unsigned long limit = 2222, step = 1;

  tests_start ();

  if (argc > 1 && argv[1][0] == 'x')
    limit = ULONG_MAX;
  else if (argc > 1)
    limit = atoi (argv[1]);

  /* for small limb testing */
  limit = MIN (limit, MP_LIMB_T_MAX);

  for (m = 0; m < MULTIFAC_WHEEL; m++)
    mpz_init_set_ui(ref [m],1);
  for (m2 = 0; m2 < MULTIFAC_WHEEL2; m2++)
    mpz_init_set_ui(ref2 [m2],1);

  mpz_init (res);

  m = 0;
  m2 = 0;
  for (n = 0; n <= limit;)
    {
      mpz_mfac_uiui (res, n, MULTIFAC_WHEEL);
      MPZ_CHECK_FORMAT (res);
      if (mpz_cmp (ref[m], res) != 0)
        {
          printf ("mpz_mfac_uiui(%lu,%d) wrong\n", n, MULTIFAC_WHEEL);
          printf ("  got  "); mpz_out_str (stdout, 10, res); printf("\n");
          printf ("  want "); mpz_out_str (stdout, 10, ref[m]); printf("\n");
          abort ();
        }
      mpz_mfac_uiui (res, n, MULTIFAC_WHEEL2);
      MPZ_CHECK_FORMAT (res);
      if (mpz_cmp (ref2[m2], res) != 0)
        {
          printf ("mpz_mfac_uiui(%lu,%d) wrong\n", n, MULTIFAC_WHEEL2);
          printf ("  got  "); mpz_out_str (stdout, 10, res); printf("\n");
          printf ("  want "); mpz_out_str (stdout, 10, ref2[m2]); printf("\n");
          abort ();
        }
      if (n + step <= limit)
	for (j = 0; j < step; j++) {
	  n++; m++; m2++;
	  if (m >= MULTIFAC_WHEEL) m -= MULTIFAC_WHEEL;
	  if (m2 >= MULTIFAC_WHEEL2) m2 -= MULTIFAC_WHEEL2;
	  mpz_mul_ui (ref[m], ref[m], n); /* Compute a reference, with current library */
	  mpz_mul_ui (ref2[m2], ref2[m2], n); /* Compute a reference, with current library */
	}
      else n += step;
    }
  mpz_fac_ui (ref[0], n);
  mpz_mfac_uiui (res, n, 1);
  MPZ_CHECK_FORMAT (res);
  if (mpz_cmp (ref[0], res) != 0)
    {
      printf ("mpz_mfac_uiui(%lu,1) wrong\n", n);
      printf ("  got  "); mpz_out_str (stdout, 10, res); printf("\n");
      printf ("  want "); mpz_out_str (stdout, 10, ref[0]); printf("\n");
      abort ();
    }

  mpz_2fac_ui (ref[0], n);
  mpz_mfac_uiui (res, n, 2);
  MPZ_CHECK_FORMAT (res);
  if (mpz_cmp (ref[0], res) != 0)
    {
      printf ("mpz_mfac_uiui(%lu,1) wrong\n", n);
      printf ("  got  "); mpz_out_str (stdout, 10, res); printf("\n");
      printf ("  want "); mpz_out_str (stdout, 10, ref[0]); printf("\n");
      abort ();
    }

  n++;
  mpz_2fac_ui (ref[0], n);
  mpz_mfac_uiui (res, n, 2);
  MPZ_CHECK_FORMAT (res);
  if (mpz_cmp (ref[0], res) != 0)
    {
      printf ("mpz_mfac_uiui(%lu,2) wrong\n", n);
      printf ("  got  "); mpz_out_str (stdout, 10, res); printf("\n");
      printf ("  want "); mpz_out_str (stdout, 10, ref[0]); printf("\n");
      abort ();
    }

  for (m = 0; m < MULTIFAC_WHEEL; m++)
    mpz_clear (ref[m]);
  for (m2 = 0; m2 < MULTIFAC_WHEEL2; m2++)
    mpz_clear (ref2[m2]);
  mpz_clear (res);

  tests_end ();

  exit (0);
}