halleck.c:


/* We need standard IO */
#include <stdio.h>

/* We need an integer square root */
#include "isqrt.c"

/* factor a number by Halleck's method.  Generating relation that   */
/* generated this was  N = pq =(S-A)(S+B) where S is the integer  */
/* Square root. */
main () {
  long n, p, q, s, t;

  printf ("Factor a positive number by Halleck's method\n");
  printf ("N = \n"); scanf ("%ld", &n);
  printf ("Factoring %ld\n", n);
  if (n<1) { printf ("N must be greater than zero\n"); return 1;}

  s = isqrt(n);
  p = s; q = s; t = p*q - n;

  while (t!=0) if (t<0) { t += p; q++; }
               else     { t -= q; p--; }

  printf ("%ld*%ld=%ld\n",p,q,n);
}

Go to ...


This page is http://www.cc.utah.edu/~nahaj/factoring/halleck.c.html
© Copyright 2003 by John Halleck, All Rights Reserved.
This snapshot was last modified on December 24th, 2013