INTEGER N1
1     WRITE(5,2) ' Enter a number: '
2     FORMAT( A,$ )
      READ(5,3) N1
3     FORMAT( I )
      CALL FACTOR( N1 )
      GOTO 1
      END

      SUBROUTINE FACTOR ( N )
C     Factor N
C     Test for factors of 2,3,5,... using NEXTPRIME function to get next prime.
      INTEGER N, NUMBER, PRIME
      NUMBER = N
      PRIME = 2					!Start with 2
10    CONTINUE
         IF (NUMBER .EQ. 0) RETURN
         IF ( MOD(NUMBER,PRIME) .EQ. 0 ) THEN	!prime is a factor
            WRITE(6,1) PRIME
            NUMBER = NUMBER / PRIME
            IF (NUMBER .EQ. 1) RETURN
         ELSE
            PRIME = NEXTPRIME(PRIME+1)		!Else test the next prime
         ENDIF
      GOTO 10
1     FORMAT(1X,'FACTOR ',I)
      END

      INTEGER FUNCTION NEXTPRIME( N )
C     Return next prime number .GE. N
C
C     For N .GE. 1,018,801 returns next number relatively prime to all
C     primes below 1009.
C     Note: 1009 is next prime after 997, and 1,018,801 = 1009^2.
C           The database may be extended if larger primes are desired.
C
C     Outline:
C         1.  If number .LE. 3 then return number.
C             Number is prime, zero, or negative.
C         2.  If number between 4 and 997 then search PRIME array
C             for next prime .GE. number.  An approximating polynomial
C             is used to give us a good initial guess for the index I
C             so that PRIME(I) is very close to the prime we seek.
C             The approximating polynomial is:
C
C                 I = -0.3034277E-04*X^2 + 0.1918667*X + 8.0918350
C
C              The optimum coefficients for this polynomial were obtained
C              using the OPTIMIZE program in the [.OPTIMIZE] directory.
C              Look there for further details.  Thus, this function is right
C              on the money 452 times, is low by one 224 times, is low by two
C              40 times, and is low by three 1 time.  It's high by one 185
C              times, high by two 68 times, high by three 12 times, high by
C              four 7 times, high by five 4 times, and high by 6 one time.
C
C         3.  Else number is greater than 997.  Do normal search for next prime.
C             3a.  Increment NUMBER to an odd number.  Since even numbers are
C                  not prime we skip over even numbers.
C             3b.  Test if NUMBER is evenly divisible by any prime in the
C                  PRIME array less than SQRT(NUMBER).
C                  If NUMBER MOD PRIME(I) = 0, then NUMBER is not prime.

      INTEGER N, NUMBER
      INTEGER NUMPRIMES
      PARAMETER (NUMPRIMES = 168)
      INTEGER PRIME(NUMPRIMES)
      DATA PRIME /
     *   2,   3,   5,   7,  11,  13,  17,  19,  23,  29,  31,  37,  41,
     *  43,  47,  53,  59,  61,  67,  71,  73,  79,  83,  89,  97, 101,
     * 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
     * 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
     * 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
     * 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
     * 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467,
     * 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569,
     * 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643,
     * 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
     * 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823,
     * 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
     * 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 /

      NUMBER = N

      IF (NUMBER .LE. 3) THEN				!if number .le. 3 then return number
          NEXTPRIME = NUMBER
          RETURN
      ENDIF

      IF (NUMBER .LE. 997) THEN				!just search thru PRIME array
         I = (-0.3034277E-04*NUMBER + 0.1918667)*NUMBER + 8.0918350	!(see comments above)
         I = MIN(I,NUMPRIMES)				!don't let I go above 997
         DO WHILE (PRIME(I) .LT. NUMBER)		!Search upward for first prime greater than NUMBER
            I = I + 1
         ENDDO
         DO WHILE (PRIME(I) .GE. NUMBER)		!Search downward for first prime less than NUMBER,
            I = I - 1
         ENDDO
         NEXTPRIME = PRIME(I+1)				!then take next prime.
         RETURN

      ELSE						!Else normal search for next prime

         IF ( MOD(NUMBER,2) .EQ. 0 ) NUMBER = NUMBER + 1 !rule out even numbers.  They are not prime
         I = 2						 !start with second prime since we only test odd numbers.
         DO WHILE ( PRIME(I)*PRIME(I) .LE. NUMBER )	 !test for all primes .LE. SQRT(NUMBER)
            IF ( MOD(NUMBER,PRIME(I)) .EQ. 0 ) THEN	 !it's not prime
               NUMBER = NUMBER + 2			 !even numbers aren't prime
               I = 2					 !start over again with new number
            ELSE
               I = I + 1				 !test with next prime
               IF ( I .GT. NUMPRIMES) GOTO 10		 !exit loop.  NUMBER is relatively prime to first 168 primes.
            ENDIF
         ENDDO
10       NEXTPRIME = NUMBER
         RETURN

      ENDIF

      END

Back