Blame tests/mpz/t-fac_ui.c

Packit 5c3484
/* Exercise mpz_fac_ui and mpz_2fac_ui.
Packit 5c3484
Packit 5c3484
Copyright 2000-2002, 2012, 2015 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library test suite.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is free software; you can redistribute it
Packit 5c3484
and/or modify it under the terms of the GNU General Public License as
Packit 5c3484
published by the Free Software Foundation; either version 3 of the License,
Packit 5c3484
or (at your option) any later version.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is distributed in the hope that it will be
Packit 5c3484
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5c3484
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 5c3484
Public License for more details.
Packit 5c3484
Packit 5c3484
You should have received a copy of the GNU General Public License along with
Packit 5c3484
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include <stdio.h>
Packit 5c3484
#include <stdlib.h>
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "tests.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* Usage: t-fac_ui [x|num]
Packit 5c3484
Packit 5c3484
   With no arguments testing goes up to the initial value of "limit" below.
Packit 5c3484
   With a number argument tests are carried that far, or with a literal "x"
Packit 5c3484
   tests are continued without limit (this being meant only for development
Packit 5c3484
   purposes).  */
Packit 5c3484
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
main (int argc, char *argv[])
Packit 5c3484
{
Packit 5c3484
  unsigned long  n, m;
Packit 5c3484
  unsigned long  limit = 2222;
Packit 5c3484
  mpz_t          df[2], f, r;
Packit 5c3484
Packit 5c3484
  tests_start ();
Packit 5c3484
Packit 5c3484
  if (argc > 1 && argv[1][0] == 'x')
Packit 5c3484
    limit = ULONG_MAX;
Packit 5c3484
  else if (argc > 1)
Packit 5c3484
    limit = atoi (argv[1]);
Packit 5c3484
Packit 5c3484
  /* for small limb testing */
Packit 5c3484
  limit = MIN (limit, MP_LIMB_T_MAX);
Packit 5c3484
Packit 5c3484
  mpz_init_set_ui (df[0], 1);  /* 0!! = 1 */
Packit 5c3484
  mpz_init_set_ui (df[1], 1);  /* -1!! = 1 */
Packit 5c3484
  mpz_init_set_ui (f, 1);  /* 0! = 1 */
Packit 5c3484
  mpz_init (r);
Packit 5c3484
Packit 5c3484
  for (n = 0, m = 0; n < limit; n++)
Packit 5c3484
    {
Packit 5c3484
      mpz_fac_ui (r, n);
Packit 5c3484
      MPZ_CHECK_FORMAT (r);
Packit 5c3484
Packit 5c3484
      if (mpz_cmp (f, r) != 0)
Packit 5c3484
        {
Packit 5c3484
          printf ("mpz_fac_ui(%lu) wrong\n", n);
Packit 5c3484
          printf ("  got  "); mpz_out_str (stdout, 10, r); printf("\n");
Packit 5c3484
          printf ("  want "); mpz_out_str (stdout, 10, f); printf("\n");
Packit 5c3484
          abort ();
Packit 5c3484
        }
Packit 5c3484
Packit 5c3484
      mpz_2fac_ui (r, n);
Packit 5c3484
      MPZ_CHECK_FORMAT (r);
Packit 5c3484
Packit 5c3484
      if (mpz_cmp (df[m], r) != 0)
Packit 5c3484
        {
Packit 5c3484
          printf ("mpz_2fac_ui(%lu) wrong\n", n);
Packit 5c3484
          printf ("  got  "); mpz_out_str (stdout, 10, r); printf("\n");
Packit 5c3484
          printf ("  want "); mpz_out_str (stdout, 10, df[m]); printf("\n");
Packit 5c3484
          abort ();
Packit 5c3484
        }
Packit 5c3484
Packit 5c3484
      m ^= 1;
Packit 5c3484
      mpz_mul_ui (df[m], df[m], n+1);  /* (n+1)!! = (n-1)!! * (n+1) */
Packit 5c3484
      mpz_mul_ui (f, f, n+1);  /* (n+1)! = n! * (n+1) */
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  n = 2097169; /* a prime = 1 mod 4*/
Packit 5c3484
  if (n / 2 > MP_LIMB_T_MAX)
Packit 5c3484
    n = 131041; /* a smaller prime :-) */
Packit 5c3484
  mpz_fac_ui (f, n / 2); /* ((n-1)/2)! */
Packit 5c3484
  m = mpz_fdiv_ui (f, n); /* ((n-1)/2)! mod n*/
Packit 5c3484
  mpz_set_ui (f, m);
Packit 5c3484
  mpz_mul_ui (f, f, m); /* (((n-1)/2)!)^2 */
Packit 5c3484
  m = mpz_fdiv_ui (f, n); /* (((n-1)/2)!)^2 mod n*/
Packit 5c3484
  if ( m != n - 1)
Packit 5c3484
    {
Packit 5c3484
      printf ("mpz_fac_ui(%lu) wrong\n", n / 2);
Packit 5c3484
      printf (" al-Haytham's theorem not verified: got %lu, expected %lu.\n", m, n - 1);
Packit 5c3484
      abort ();
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  mpz_clear (df[0]);
Packit 5c3484
  mpz_clear (df[1]);
Packit 5c3484
  mpz_clear (f);
Packit 5c3484
  mpz_clear (r);
Packit 5c3484
Packit 5c3484
  tests_end ();
Packit 5c3484
Packit 5c3484
  exit (0);
Packit 5c3484
}