Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
prime.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file prime.c
5 
6  \version V1.0
7  \date 15.05.15
8  \brief Routines to identify prime numbers
9 
10  \copyright (c) Dr. Andrew C. R. Martin, UCL, 2015
11  \author Dr. Andrew C. R. Martin
12  \par
13  Institute of Structural & Molecular Biology,
14  University College London,
15  Gower Street,
16  London.
17  WC1E 6BT.
18  \par
19  andrew@bioinf.org.uk
20  andrew.martin@ucl.ac.uk
21 
22 **************************************************************************
23 
24  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
25  according to the conditions laid out in the accompanying file
26  COPYING.DOC.
27 
28  The code may be modified as required, but any modifications must be
29  documented so that the person responsible can be identified.
30 
31  The code may not be sold commercially or included as part of a
32  commercial product except as described in the file COPYING.DOC.
33 
34 **************************************************************************
35 
36  Description:
37  ============
38 
39 **************************************************************************
40 
41  Usage:
42  ======
43 
44 **************************************************************************
45 
46  Revision History:
47  =================
48 - V1.0 15.05.15 Original By: ACRM
49 
50 *************************************************************************/
51 /* Doxygen
52  -------
53  #GROUP General Programming
54  #SUBGROUP Maths
55 
56  #FUNCTION blIsPrime()
57  Tests whether a number is prime
58 
59  #FUNCTION blFindNextPrime()
60  Finds the next prime
61 
62 *************************************************************************/
63 /* Includes
64 */
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <math.h>
68 #include <limits.h>
69 #include "SysDefs.h"
70 #include "MathUtil.h"
71 
72 /************************************************************************/
73 /* Defines and macros
74 */
75 
76 /************************************************************************/
77 /* Globals
78 */
79 
80 /************************************************************************/
81 /* Prototypes
82 */
83 
84 /************************************************************************/
85 /*>ULONG blFindNextPrime(ULONG primeNum, BOOL above)
86 ----------------------------------------------------
87 *//**
88  \param[in] primeNum Input number (prime or not)
89  \param[in] above Always return a number above the input
90  \return The next prime
91 
92  Returns a prime above (or equal to if 'above' is FALSE) the input
93  number
94 
95 - 15.05.15 Original By: ACRM
96 */
97 ULONG blFindNextPrime(ULONG primeNum, BOOL above)
98 {
99  /* Check we have some chance of finding a prime
100  Largest 64-bit ULONG prime is
101  18446744073709551557 = ULONG_MAX - 58
102  Largest 32-bit ULONG prime is
103  4294967291 = ULONG - 4
104  */
105  if(ULONG_MAX == 4294967295UL) /* 32-bit */
106  {
107  if(primeNum > (ULONG_MAX-4))
108  return(0);
109  }
110  else /* 64-bit */
111  {
112  if(primeNum > (ULONG_MAX-59))
113  return(0);
114  }
115 
116  if(above)
117  primeNum++;
118 
119  /* If the number is even and not 2, make it odd */
120  if(((primeNum%2) == 0) && (primeNum != 2))
121  primeNum++;
122 
123  /* Look at each odd number until we find a prime */
124  while(!blIsPrime(primeNum))
125  primeNum+=2;
126 
127  return(primeNum);
128 }
129 
130 
131 /************************************************************************/
132 /*>BOOL blIsPrime(ULONG input)
133 ------------------------------
134 *//**
135  \param[in] input Number to test
136  \return Is this number a prime?
137 
138  Tests whether the input number is a prime
139 
140 - 15.05.15 Original By: ACRM
141 */
143 {
144  BOOL prime = TRUE;
145 
146  /* numbers between
147  4,294,967,293 and 4,294,967,295 (32bit)
148  and
149  18,446,744,073,709,551,613 and 18,446,744,073,709,551,615 (64bit)
150  are not prime
151  */
152 
153  if(input > (ULONG_MAX-2))
154  return(FALSE);
155 
156  if(input == 2)
157  return(TRUE);
158 
159  /* Return if it's 0, 1 or even */
160  if(input%2 == 0 || input <= 1)
161  {
162  prime = FALSE;
163  }
164  else
165  {
166  ULONG i;
167 
168  /* Test by dividing by each odd number from 3 up to the square
169  root of the input number
170  */
171  for(i=3; i<=(ULONG)sqrt((double)input); i+=2)
172  {
173  if(input%i == 0)
174  {
175  prime = FALSE;
176  break;
177  }
178  }
179  }
180  return prime;
181 }
182 
183 #ifdef TEST
184 /************************************************************************/
185 int main(int argc, char **argv)
186 {
187  ULONG input = 1;
188  ULONG nextPrime;
189 
190  if(argc!=2)
191  return(1);
192 
193  if(sscanf(argv[1], "%lu", &input))
194  {
195  nextPrime = blFindNextPrime(input, FALSE);
196 
197  if(input == nextPrime)
198  {
199  printf("The input number was prime %lu\n",
200  input);
201  }
202  else if(nextPrime == 0)
203  {
204  printf("The input number was out of range %lu\n",
205  input);
206  }
207  else
208  {
209  printf("The prime number following %lu is %lu\n",
210  input, nextPrime);
211  }
212  }
213  return(0);
214 }
215 #endif
int main(int argc, char **argv)
Definition: test.c:4
short BOOL
Definition: SysDefs.h:64
#define FALSE
Definition: macros.h:223
unsigned long ULONG
Definition: SysDefs.h:66
BOOL blIsPrime(ULONG input)
Definition: prime.c:142
#define TRUE
Definition: macros.h:219
Prototypes, etc. for maths utility routines.
System-type variable type definitions.
ULONG blFindNextPrime(ULONG primeNum, BOOL above)
Definition: prime.c:97