Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2025 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Functions which are common to different hash tables
10 : !> Derived from qs_fb_hash_table_types and qs_fb_hash_table_types (Mark Tucker, Jun 2016)
11 : ! **************************************************************************************************
12 : MODULE qs_hash_table_functions
13 :
14 : #include "./base/base_uses.f90"
15 : IMPLICIT NONE
16 :
17 : PRIVATE
18 :
19 : ! public methods
20 : PUBLIC :: hash_table_matching_prime
21 :
22 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_hash_table_functions'
23 :
24 : CONTAINS
25 :
26 : ! **************************************************************************************************
27 : !> \brief Find a prime number equal or larger than ii
28 : !> \param ii : input integer
29 : !> \return : the prime number
30 : ! **************************************************************************************************
31 26647 : PURE FUNCTION hash_table_matching_prime(ii) RESULT(res)
32 : INTEGER, INTENT(IN) :: ii
33 : INTEGER :: res
34 :
35 : ! even numbers are not prime, so no point testing them, so increment by 2 each time starting
36 : ! from an odd number greater or equal to ii (as noted in \brief)
37 26647 : res = ii + 1 - MOD(ii, 2)
38 :
39 40583 : DO WHILE (.NOT. is_positive_prime(res))
40 13936 : res = res + 2
41 : END DO
42 26647 : END FUNCTION hash_table_matching_prime
43 :
44 : ! **************************************************************************************************
45 : !> \brief Check if a number is a positive prime
46 : !> \param num : number to check
47 : !> \return : returns TRUE if num is a positive prime, FALSE otherwise
48 : !> \author Lianheng Tong (LT) lianheng.tong@kcl.ac.uk
49 : ! **************************************************************************************************
50 40583 : PURE FUNCTION is_positive_prime(num) RESULT(res)
51 : INTEGER, INTENT(IN) :: num
52 : LOGICAL :: res
53 :
54 : INTEGER :: ii
55 :
56 40583 : IF (num <= 3) THEN
57 : res = .FALSE.
58 : RETURN
59 : END IF
60 34228 : IF (MOD(num, 2) == 0 .OR. MOD(num, 3) == 0) THEN
61 : res = .FALSE.
62 : RETURN
63 : END IF
64 :
65 : ! all primes > 3 are of the form 6*kk +/- 1, kk=1,2,3...
66 : ! (although not all 6*kk +/- 1 is a prime);
67 : ! and we only have to check factors less than and equal to SQRT(num)
68 : ii = 5
69 29027 : DO WHILE (ii*ii <= num)
70 2380 : IF (MOD(num, ii) == 0 .OR. MOD(num, ii + 2) == 0) THEN
71 : res = .FALSE.
72 : RETURN
73 : END IF
74 1588 : ii = ii + 6
75 : END DO
76 : res = .TRUE.
77 : END FUNCTION is_positive_prime
78 :
79 : END MODULE qs_hash_table_functions
80 :
|