largestPrimeFactor.f95 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ! ----------------------------------------------------------
  2. ! Programmer: Jonathan Landrum
  3. ! Date: 5 September 2012
  4. ! ----------------------------------------------------------
  5. ! Program: primeFactors.exe
  6. ! Purpose: Calculates the largest prime factor of a
  7. ! given number.
  8. ! Assumptions: 1.) Input from the user is in integer
  9. ! form. Text input will cause the
  10. ! program to fail. Real number input
  11. ! will be truncated into integers.
  12. ! ----------------------------------------------------------
  13. PROGRAM primeFactors
  14. IMPLICIT NONE
  15. INTEGER,PARAMETER :: k32 = selected_int_kind(32)
  16. INTEGER(kind=k32) :: input = 0, factor, output
  17. LOGICAL :: isPrime
  18. WRITE (*,*) '*************************************************'
  19. WRITE (*,*) '* *'
  20. WRITE (*,*) '* FORTRAN LARGEST PRIME FACTOR CALCULATOR *'
  21. WRITE (*,*) '* *'
  22. WRITE (*,*) '*************************************************'
  23. WRITE (*,*)
  24. WRITE (*,*) 'Copyright (c) 2012 Jonathan Landrum'
  25. WRITE (*,*)
  26. WRITE (*,*) 'Calculates the largest prime factor of a number.'
  27. WRITE (*,*) '-------------------------------------------------'
  28. WRITE (*,*)
  29. DO WHILE (input < 2)
  30. WRITE (*,'(a)',advance='no') ' Enter a number to calculate: '
  31. READ (*,*) input
  32. WRITE (*,*)
  33. END DO
  34. factor = 2
  35. IF (isPrime(input)) THEN
  36. output = input
  37. ELSE
  38. DO WHILE (input .NE. factor)
  39. DO WHILE ((MOD(input,factor) == 0) .AND. (input .NE. factor))
  40. IF (input .NE. factor) THEN
  41. input = input / factor
  42. END IF
  43. END DO
  44. IF (input .NE. factor) THEN
  45. factor = factor + 1
  46. DO WHILE (isPrime(factor) .EQV. .FALSE.)
  47. factor = factor + 1
  48. END DO
  49. END IF
  50. output = factor
  51. END DO
  52. END IF
  53. WRITE (*,*) 'The largest prime factor is ', output
  54. WRITE (*,*)
  55. WRITE (*,*) '-------------------------------------------------'
  56. WRITE (*,*) '\\//_ Live long and prosper.'
  57. END PROGRAM primeFactors
  58. ! ----------------------------------------------------------
  59. ! FUNCTION isPrime:
  60. ! Determines if the input is a prime number. Returns T or F.
  61. ! ----------------------------------------------------------
  62. LOGICAL FUNCTION isPrime(n)
  63. IMPLICIT NONE
  64. INTEGER,PARAMETER :: k32 = selected_int_kind(32)
  65. INTEGER(kind=k32) :: n, i
  66. IF (n <= 1) THEN ! 1 is not prime
  67. isPrime = .FALSE.
  68. ELSE IF ((n == 2).OR.(n == 3)) THEN ! Hardcode 2 and 3
  69. isPrime = .TRUE.
  70. ELSE IF (MOD(n,2) == 0) THEN ! Get rid of evens
  71. isPrime = .FALSE.
  72. ! All other cases are out, so now we check to see if
  73. ! n is divisible by the odd numbers from 3 on.
  74. ELSE
  75. i = 3
  76. isPrime = .TRUE. ! Assume it's prime, then prove
  77. DO ! If i^2 is not a root of n, or if n%i = 0
  78. IF (i*i > n .OR. MOD(n,i) == 0) EXIT !(Won't be larger than the square)
  79. i = i + 2 ! Iterate the counter by 2 to get odds
  80. END DO
  81. ! Record the answer
  82. IF (i*i > n) THEN
  83. isPrime = .TRUE.
  84. ELSE
  85. isPrime = .FALSE.
  86. END IF
  87. END IF ! We have our answer
  88. END FUNCTION isPrime