FactorFound.cc

Go to the documentation of this file.
00001 
00007 void TFoundFactor::output(std::ostream &ostr) const
00008 {
00009   ostr << x;
00010   if (e!=1) ostr << "^" << e;
00011   if (comment!="") ostr << " " << comment;
00012   else
00013    {
00014      if (!mpz_probab_prime_p(x,probab_prime_checks)) ostr << " [composite]";
00015    }
00016 }
00017 
00018 void TFoundFactors::PrettyPrint(std::ostream &os) const
00019 {
00020   os << endl << "Factorization of " << regarding << ": " << endl;
00021   if (mpz_sizeinbase(regarding_numeric_value,10)<200)
00022    os << "[= " << regarding_numeric_value << "]" << endl;
00023 
00024   mpz_t x,y;
00025   mpz_init_set_ui(x,1); mpz_init(y);
00026 
00027   bool flag = false;
00028   for (TFoundFactors::const_iterator p=begin(); p!=end(); ++p)
00029    {
00030      if (flag) os << "* "; else os << "= ";
00031      p->output(os); os << endl;
00032      unsigned int exponent;
00033      p->get_factor(y,exponent); mpz_pow_ui(y,y,exponent);
00034      mpz_mul(x,x,y);
00035      flag=true;
00036    }
00037 
00038   // perform a sanity check
00039   if (mpz_cmp(x,regarding_numeric_value)) os << "The factorization is incomplete!" << endl;
00040 
00041   mpz_clear(x); mpz_clear(y);
00042 }
00043 
00044 void TFoundFactors::AutoSave() const
00045 {
00046   std::ofstream ofstr(FoundFactorsFile.c_str());
00047   ofstr << regarding << endl;
00048   ofstr << regarding_numeric_value << endl;
00049   Save(ofstr);
00050 }
00051 
00052 void TFoundFactors::AutoLoad()
00053 {
00054 #ifdef VERBOSE_INFO
00055   cout << "Autoload FoundFactors" << endl;
00056 #endif
00057   std::ifstream ifstr(FoundFactorsFile.c_str());
00058   char buf [32768];
00059   ifstr.getline(buf,sizeof(buf),'\n'); regarding=buf;
00060   ifstr >> regarding_numeric_value;
00061   Load(ifstr);
00062 }
00063 
00064 
00065 const char* myMAL(void)
00066 {
00067   // returns empty string on first call, and  " * " otherwise.
00068   // Task: Prevent costly checks for suppressing the trivial factor 1
00069   // as the fist resp. last factor.
00070 
00071 #ifdef NOTIFY_PARENT
00072   // send SIGUSR2 to the parent to notify that a new
00073   // factor has found
00074   kill(getppid(),SIGUSR2); // send SIGUSR2 to the parent process
00075 #endif
00076 
00077   static bool first_call = true;
00078   if (first_call)
00079    {
00080      first_call=false;
00081      return "";
00082    }
00083   else return " * ";
00084 }
00085 
00086 std::string MAL(const mpz_t factor, const unsigned int exponent=1)
00087 {
00088   FoundFactors.insert(TFoundFactor(factor,exponent));
00089   FoundFactors.AutoSave();
00090   ostringstream helper;
00091   helper << myMAL() << factor;
00092   if (exponent>1) helper << "^" << exponent;
00093   return helper.str();
00094 }
00095 
00096 std::string MAL(const mpz_t factor, const unsigned int exponent, const std::ostringstream &comment)
00097 {
00098   FoundFactors.insert(TFoundFactor(factor,exponent,comment.str()));
00099   FoundFactors.AutoSave();
00100   ostringstream helper;
00101   helper << myMAL() << factor;
00102   if (exponent>1) helper << "^" << exponent;
00103   return helper.str()+comment.str();
00104 }
00105 
00106 
00107 std::string MAL(const mpz_t factor, const std::ostringstream &comment)
00108 {
00109   FoundFactors.insert(TFoundFactor(factor,1,comment.str()));
00110   FoundFactors.AutoSave();
00111   ostringstream helper;
00112   helper << myMAL() << factor;
00113   return helper.str()+comment.str();
00114 }
00115 
00116 std::string MAL(const unsigned long int factor, const unsigned int exponent=1)
00117 {
00118   FoundFactors.insert(TFoundFactor(factor,exponent));
00119   FoundFactors.AutoSave();
00120   ostringstream helper;
00121   helper << myMAL() << factor;
00122   if (exponent>1) helper << "^" << exponent;
00123   return helper.str();
00124 }

Generated on Wed Nov 7 23:29:25 2007 for Qsieve by  doxygen 1.5.4