Domino Code Fragment

Code Name*
IsPrime Function
Date*
04/29/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.18.191.211.66
Description*
Given a positive integer, determines if that integer is a prime number (A number is considered to be prime if it has no factors other than plus or minus itself and plus or minus one). Returns a Boolean value of True or False.
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:
IsPrime Function

Function IsPrime(nTheNumber As Integer) As Integer

Given a positive integer, determines if that integer is a prime number (A number is considered to be prime if it has no factors other than plus or minus itself and plus or minus one). Returns a Boolean value of True or False.

Function Syntax

IsPrime( nTheNumber )

Formal Parameters

nTheNumber

Positive Integer.

Return value

The IsPrime function's return value is a Boolean True if nTheNumber is a prime number or False if nTheNumber is not a prime number.

Function

'-- Function Signature Line... nTheNumber = the Formal Parameter... Return type = Boolean
Function
IsPrime(nTheNumber As Integer) As Integer

'-- bSoFarPrime = boolean value
Dim bSoFarPrime As Integer
'-- nCandidate used to see if there exist a factor of nTheNumber
Dim nCandidate As Integer

bSoFarPrime = True '** innocent until proven guilty **
nCandidate = 2 '** first number (factor) past 2 to test **

'-- ** calculate last candidate once and use it often in the while test **
lastCandidate = Fix(Sqr(nTheNumber))
Do While (nCandidate <= lastCandidate)
'-- ** nCandidate is a factor of nTheNumber **
If nTheNumber Mod nCandidate = 0 Then bSoFarPrime = False
If nCandidate = 2 Then
'-- assign to three so following +2 steps will all be odd
nCandidate = 3
Else
nCandidate = nCandidate + 2

End If
Loop
isPrime = bSoFarPrime '** Return the Answer **
End Function

Usage

The valid value for nTheNumber is any positive integer.

Example

This example asks the user for input of a positive integer. IsPrime determines whether the integer is a prime number. The return is assigned to a variable of type boolean.

Sub Click(Source As Button)
'-- nNumber = actual Parameter to be passed to IsPrime function
Dim nNumber As Integer
Dim bCheck As Integer
nNumber = Inputbox ("Enter your positive Number", "Prime Number Checker")
bCheck = IsPrime(nNumber)
If bCheck = False Then
Messagebox Cstr(nNumber),
MB_ICONEXCLAMATION, "This is not Prime number"
Else
Messagebox Cstr(nNumber),
MB_ICONINFORMATION, "This is a Prime Number"
End If
End Sub