nahajdef.c:


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

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

/* factor a number by Nahaj's method. */
/* Generating relation is N = pq = ss+r = (s-a)(s+a+2d) */
/*  DN wrt a = -(2(a+d)+1)                              */
/*  DN wrt d =   2(s-a)                                 */
main () {
  long n, p, q, d, a, s, t;

  printf ("Factor a positive odd number by Nahaj'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;}
  if (n%2==0) { printf ("N must be odd\n"); return 1;}

  s = isqrt(n);
  d = 0; a = 0; t = (s-a)*(s+a+2*d) - n;

  while (t!=0) if (t<0) { t += 2*(s-a);   d++;}
               else     { t -= 2*(a+d)+1; a++;}

  p = s-a; q = s+a+2*d;
  printf ("%ld*%ld=%ld\n",p,q,n);
}

Go to ...


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