#include #include #include using namespace std; // Wheel Factoriztion. // ******************* // Prime Used : [ 2 ] // Jumps 50% of the composite. // C/C++ Compatible // Please include in C bool CheckPrimality(int Numeric) { int RootN = sqrt(Numeric); if(Numeric % 2 == 0) return false; for(int i = 3; i <= RootN; i += 2) if(Numeric % i == 0) return false; return true; } // C/C++ Compatible // Round Function int Round(double f) { if(f > 0) return (int) (f + 0.5); else return (int) (f - 0.5); } // C/C++ Compatible // Calculate the Machine Epsilon double MEpsilon() { double Test, e, TE = 1.0; do { TE /= 2; Test = TE + 1; Test -= 1; if(Test > 0) e = Test; } while(Test > 0); return e; } // Fermat's Method of Factorization. // ********************************* // To know how to factorize a number // with this algorithm, see this function // really carefully! // C++ Standard - [Complete] // ISO/IEC 14882:2003 void FermatFactorize_Print(int Numeric) { int p, q, m, n, i = Round(sqrt(Numeric)); while(true) { double a = fabs(fmod(q = sqrt(i - Numeric), 1.0)); if(a <= MEpsilon() * a) p = static_cast (a); i++; } assert((m = p - q) * (n = p + q) == Numeric); if(CheckPrimality(m)) cout << ' ' << m << ' '; else FermatFactorize_Print(m); if(CheckPrimality(n)) cout << ' ' << n << ' '; else FermatFactorize_Print(n); } int main() { FermatFactorize_Print(6077); return 0; }