// fermat.cpp -- implementation of Fermat's method for factoring. Based on // the method given as Algorithm C in Donald E. Knuths, // "The Art of Computer Programming", vol. 2, Seminumerical // Algorithms, Addison-Wesley, pp.371-2 // #include #include int main() { unsigned int N,xp,yp,sq; int r; cout << "Input a positive, odd integer: "; cin >> N; sq = (int)sqrt(N); xp = 2*sq + 1; yp = 1; r = sq*sq - N; do { while (r > 0) { r -= yp; yp += 2; } while (r < 0) { r += xp; xp += 2; } } while (r != 0); cout << (xp-yp)/2 << " " << (xp+yp-2)/2 << endl; }