KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Determining if a number is a prime number
PRODUCT: 4D | VERSION: 15 | PLATFORM: Mac & Win
Published On: October 5, 2017

Here is a utility method that will determine if a number is prime:

// ---------------------------------------------------------------------------
// Name: IS_PRIME_NUMBER
// Description: Method will if the Longint number is a prime number.
//
// Input:
// $1 (LONGINT) - Number to check if it is Prime.
//
// Output:
// $0 (LONGINT) - (True- Prime, False- Not a Prime)
// ---------------------------------------------------------------------------
C_LONGINT($1;$num;$working)
C_BOOLEAN($0;$status)

$num:=$1

$status:=true

For ($i;$num-1;2;-1)
  $working:=Mod($num;$i)
  If ($working=0)
    $status:=false
    $i:=1
  End if
End for

$0:=$status


Here is an example of a prime number:

C_LONGINT($num)
C_BOOLEAN($result)

$num:=997

$result:=IS_PRIME_NUMBER($num) // True


Here is an example of NOT a prime number:

$num:=8

$result:=IS_PRIME_NUMBER($num) // False