home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: sci.math
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!hellgate.utah.edu!fcom.cc.utah.edu!news
- From: NAHAJ@CC.UTAH.EDU (John B 'Nahaj' Halleck, UUCC)
- Subject: Q on new(?) simple factoring algorithm.
- Message-ID: <1992Dec20.053421.2258@fcom.cc.utah.edu>
- Sender: news@fcom.cc.utah.edu
- Organization: UNIVERSITY OF UTAH COMPUTER CENTER
- Date: Sun, 20 Dec 92 05:34:21 GMT
- X-News-Reader: VMS NEWS 1.24
- Lines: 69
-
- While investigating Fermat-like factoring algorithms, I derived the following
- algorithm. (Sorry folks, it's run time is the same as fermat's, no major
- breakthrough.) It, like Fermat's method, has only additions and tests in
- the loop, but produces it's factors directly instead of generating quantities
- that are used to [trivially] produce the factors.
-
- I am unable to find this one in the literature, nor am I able to think of
- any reason it ought not to be prefered to fermat's method.
-
- SO.... Could some kind soul please refer me to where and by what name this
- algorithm is already known? (And where to read up on it?)
- Given the normal implimentation of Fermat's method (For example Knuth's Vol 2
- code) also has an integer square root in the set up, could someone give
- me a reason that this method should not always be prefered to Fermat's?
-
- Please e-mail answers, as I don't really read this group.
- I'll post a summary.
-
- - John Halleck
- nahaj@cc.utah.edu
-
-
- ---- Simple form of the algorithm (as C code) without any of the obvious ---
- ---- speedup's applied -----------------------------------------------------
-
- #include <math.h>
- long isqrt(long x) { return (long) sqrt((double) x); }
- /* This is a hack, a real integer square root would be much better */
-
-
- #include <stdio.h>
- main () {
- long p, q, n, guess;
-
- printf ("Input a positive non-zero number to be factored\n");
- scanf ("%ld",&n); printf ("We will try to factor %ld\n",n);
-
- q=p=isqrt(n); guess = p*q;
-
- while (guess != n) {
- if (guess>n) { guess -= p; q--; }
- else { guess += q; p++; }
- }
-
- printf ("two possibly non-prime factors are %ld, %ld.\n",q,p);
- return 0;
- }
-
-
-
-
-
-
-
- ------------- For the curious, why does this work? ------
-
- A number to be factored can be represented in the form SS+R
- where S is the integer square root. The two factors are of the form:
- (S+A)*(S-B) = p*q = n
- multiplying out the expression gives:
- SS + SA - SB -AB = n
- Adding one to A increases the expression on the left hand side by (S-B) which
- is positive because S>A when we are talking about the factors.
- Adding one to B decreasses the value of the LHS by (S+A).
- So incrementing A increases our value, incrementing B decreases our value,
- So we just go up and down till we hit the number...
- The termination argument is about the same as it is for Fermat's method.
-
- And yes, I know that there are lots of obvious speedups...
-