factors.f90
Go to the documentation of this file.
1!#############################################################################
2!# #
3!# fosite - 3D hydrodynamical simulation program #
4!# module: factors.f90 #
5!# #
6!# Copyright (C) 2006-2010 #
7!# Tobias Illenseer <tillense@astrophysik.uni-kiel.de> #
8!# #
9!# This program is free software; you can redistribute it and/or modify #
10!# it under the terms of the GNU General Public License as published by #
11!# the Free Software Foundation; either version 2 of the License, or (at #
12!# your option) any later version. #
13!# #
14!# This program is distributed in the hope that it will be useful, but #
15!# WITHOUT ANY WARRANTY; without even the implied warranty of #
16!# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or #
17!# NON INFRINGEMENT. See the GNU General Public License for more #
18!# details. #
19!# #
20!# You should have received a copy of the GNU General Public License #
21!# along with this program; if not, write to the Free Software #
22!# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. #
23!# #
24!#############################################################################
25
26!----------------------------------------------------------------------------!
28!----------------------------------------------------------------------------!
29MODULE factors
30 IMPLICIT NONE
31 !--------------------------------------------------------------------------!
32 PRIVATE
33 ! number of prim numbers
34 INTEGER, PARAMETER :: numprims = 100
35 ! array of first "NUMPRIMS" prime numbers;
36 ! see http://primes.utm.edu/
37 INTEGER, PARAMETER, DIMENSION(NUMPRIMS) :: prims = &
38 (/ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, &
39 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, &
40 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, &
41 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, &
42 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, &
43 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, &
44 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, &
45 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, &
46 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, &
47 467, 479, 487, 491, 499, 503, 509, 521, 523, 541 /)
48 ! allows for prime factorization up to 299208
49 INTEGER, PARAMETER :: maxnum = 299208
50 !--------------------------------------------------------------------------!
51 PUBLIC :: &
52 ! some constants
54 ! methods
56 !--------------------------------------------------------------------------!
57
58CONTAINS
59
60 ! return the smallest prime factor of "number"
61 ! p=0 for erroneous input
62 PURE FUNCTION getfactor(number) RESULT(p)
63 IMPLICIT NONE
64 !------------------------------------------------------------------------!
65 INTEGER, INTENT(IN) :: number
66 INTEGER :: p
67 !------------------------------------------------------------------------!
68 INTEGER :: i
69 !------------------------------------------------------------------------!
70 p = 0 ! for errornous input return 0
71 IF ((number.GT.maxnum).OR.(number.LT.1)) RETURN
72 DO i=1,numprims
73 p = mod(number,prims(i))
74 IF (p.EQ.0) THEN
75 p = prims(i)
76 RETURN
77 END IF
78 END DO
79 p = number ! if there is no prim factor in the list, the number is
80 ! (a pretty large) prime number itself
81 END FUNCTION getfactor
82
83END MODULE factors
84
module for prime factorization
Definition: factors.f90:29
integer, parameter, public numprims
Definition: factors.f90:34
integer, dimension(numprims), parameter prims
Definition: factors.f90:37
pure integer function, public getfactor(number)
Definition: factors.f90:63
integer, parameter, public maxnum
Definition: factors.f90:49