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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson