version 1.1.1.2, 2000/09/09 14:13:19 |
version 1.1.1.3, 2003/08/25 16:06:03 |
|
|
/* Factoring with Pollard's rho method. |
/* Factoring with Pollard's rho method. |
|
|
Copyright (C) 1995, 1997, 1998, 1999, 2000 Free Software Foundation, Inc. |
Copyright 1995, 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, |
|
Inc. |
|
|
This program is free software; you can redistribute it and/or modify it |
This program is free software; you can redistribute it and/or modify it under |
under the terms of the GNU General Public License as published by the |
the terms of the GNU General Public License as published by the Free Software |
Free Software Foundation; either version 2, or (at your option) any |
Foundation; either version 2, or (at your option) any later version. |
later version. |
|
|
|
This program is distributed in the hope that it will be useful, but |
This program is distributed in the hope that it will be useful, but WITHOUT ANY |
WITHOUT ANY WARRANTY; without even the implied warranty of |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
PARTICULAR PURPOSE. See the GNU General Public License for more details. |
General Public License for more details. |
|
|
|
You should have received a copy of the GNU General Public License along |
You should have received a copy of the GNU General Public License along with |
with this program; see the file COPYING. If not, write to the Free |
this program; see the file COPYING. If not, write to the Free Software |
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
02111-1307, USA. */ |
|
|
|
#include <stdlib.h> |
#include <stdlib.h> |
#include <stdio.h> |
#include <stdio.h> |
Line 27 int flag_verbose = 0; |
|
Line 25 int flag_verbose = 0; |
|
|
|
static unsigned add[] = {4, 2, 4, 2, 4, 6, 2, 6}; |
static unsigned add[] = {4, 2, 4, 2, 4, 6, 2, 6}; |
|
|
#if defined (__hpux) || defined (__alpha) || defined (__svr4__) || defined (__SVR4) |
|
/* HPUX lacks random(). DEC OSF/1 1.2 random() returns a double. */ |
|
long mrand48 (); |
|
static long |
|
random () |
|
{ |
|
return mrand48 (); |
|
} |
|
#else |
|
/* Glibc stdlib.h has "int32_t random();" which, on i386 at least, conflicts |
|
with a redeclaration as "long". */ |
|
#ifndef __GLIBC__ |
|
long random (); |
|
#endif |
|
#endif |
|
|
|
void |
void |
factor_using_division (mpz_t t, unsigned int limit) |
factor_using_division (mpz_t t, unsigned int limit) |
{ |
{ |
|
|
if (!mpz_probab_prime_p (g, 3)) |
if (!mpz_probab_prime_p (g, 3)) |
{ |
{ |
do |
do |
a_int = random (); |
{ |
|
mp_limb_t a_limb; |
|
mpn_random (&a_limb, (mp_size_t) 1); |
|
a_int = (int) a_limb; |
|
} |
while (a_int == -2 || a_int == 0); |
while (a_int == -2 || a_int == 0); |
|
|
if (flag_verbose) |
if (flag_verbose) |
Line 285 factor (mpz_t t, unsigned long p) |
|
Line 271 factor (mpz_t t, unsigned long p) |
|
{ |
{ |
unsigned int division_limit; |
unsigned int division_limit; |
|
|
|
if (mpz_sgn (t) == 0) |
|
return; |
|
|
/* Set the trial division limit according the size of t. */ |
/* Set the trial division limit according the size of t. */ |
division_limit = mpz_sizeinbase (t, 2); |
division_limit = mpz_sizeinbase (t, 2); |
if (division_limit > 1000) |
if (division_limit > 1000) |
Line 324 main (int argc, char *argv[]) |
|
Line 313 main (int argc, char *argv[]) |
|
argc--; |
argc--; |
} |
} |
|
|
p = 0; |
mpz_init (t); |
for (i = 1; i < argc; i++) |
if (argc > 1) |
{ |
{ |
if (!strncmp (argv[i], "-Mp", 3)) |
p = 0; |
|
for (i = 1; i < argc; i++) |
{ |
{ |
p = atoi (argv[i] + 3); |
if (!strncmp (argv[i], "-Mp", 3)) |
mpz_init_set_ui (t, 1); |
{ |
mpz_mul_2exp (t, t, p); |
p = atoi (argv[i] + 3); |
mpz_sub_ui (t, t, 1); |
mpz_set_ui (t, 1); |
|
mpz_mul_2exp (t, t, p); |
|
mpz_sub_ui (t, t, 1); |
|
} |
|
else if (!strncmp (argv[i], "-2kp", 4)) |
|
{ |
|
p = atoi (argv[i] + 4); |
|
continue; |
|
} |
|
else |
|
{ |
|
mpz_set_str (t, argv[i], 0); |
|
} |
|
|
|
if (mpz_cmp_ui (t, 0) == 0) |
|
puts ("-"); |
|
else |
|
{ |
|
factor (t, p); |
|
puts (""); |
|
} |
} |
} |
else if (!strncmp (argv[i], "-2kp", 4)) |
} |
|
else |
|
{ |
|
for (;;) |
{ |
{ |
p = atoi (argv[i] + 4); |
mpz_inp_str (t, stdin, 0); |
continue; |
if (feof (stdin)) |
} |
break; |
else |
mpz_out_str (stdout, 10, t); printf (" = "); |
{ |
factor (t, 0); |
mpz_init_set_str (t, argv[i], 0); |
|
} |
|
|
|
if (mpz_cmp_ui (t, 0) == 0) |
|
puts ("-"); |
|
else |
|
{ |
|
factor (t, p); |
|
puts (""); |
puts (""); |
} |
} |
} |
} |
exit (0); |
|
} |
|
|
|
void |
exit (0); |
dmp (mpz_t x) |
|
{ |
|
mpz_out_str (stdout, 10, x); |
|
puts (""); |
|
} |
} |