CFLib.org – Common Function Library Project

IsPrimeNumber(inNum)

Last updated November 21, 2001

author

Douglas Williams

Version: 1 | Requires: CF5 | Library: MathLib

Description:
Returns True if the specified number is a prime number. A prime number is a positive integer that has two postive integer factors (the number is evenly divisible by 1 and itself). Note that 1 is not a prime number.

Return Values:
Returns a Boolean value

Example:

<cfset mynum = 11>
<cfset mynum2 = 15>

<cfoutput>
Given mynum = #mynum#<br>
IsPrimeNumber returns #IsPrimeNumber(mynum)#<br>
Given mynum2 = #mynum2#<br>
IsPrimeNumber returns #IsPrimeNumber(mynum2)# 
</cfoutput>

Parameters:

Name Description Required
inNum Any integer greater than one that you wish to test for prime. Yes

Full UDF Source:

/**
 * Returns True if the specified number is a prime number.
 * Minor edits by Rob Brooks-Bilson (rbils@amkor.com).  Edit by Steve Ford (steve.ford@enline.com) to fix misidentification of 4 as a prime number.  Algorithm improved by Shakti Shrivastava (divine_shammer@yahoo.com) -check up to sqr root of integer.  Further refined by Sierra Bufe (sierra@brighterfusion.com).
 * 
 * @param inNum      Any integer greater than one that  you wish to test for prime. 
 * @return Returns a Boolean value 
 * @author Douglas Williams (klenzade@i-55.com) 
 * @version 1.2, November 21, 2001 
 */
function IsPrimeNumber(inNum)
{
  var i=0;
  if (inNum lt 2){
    return False;
    break;
  }
  for (i=2; i LTE (sqr(inNum)); i=i+1) {
    if (NOT inNum MOD i) {
      return False;
      break;
    }
  }
  return True;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson