Jump to content

IsPrime()


James
 Share

Recommended Posts

A simple math function I did for my sisters homework.

Func IsPrime($io_number)
    Local $Num
    $Num = Abs($io_number)
    If $Num < 2 And $Num > -2 Then Return 1
    For $a = 2 To Int(($Num / 2) + 1)
        If Mod($Num, $a) = 0 Then Return 0
    Next
    Return 1
EndFunc   ;==>IsPrime

-James

Link to comment
Share on other sites

A simple math function I did for my sisters homework.

Func IsPrime($io_number)
    Local $Num
    $Num = Abs($io_number)
    If $Num < 2 And $Num > -2 Then Return 1
    For $a = 2 To Int(($Num / 2) + 1)
        If Mod($Num, $a) = 0 Then Return 0
    Next
    Return 1
EndFunc   ;==>IsPrime

-James

For big numbers this will probably be quite slow, so you could save some time by ignoring even numbers after 2

Func IsPrime($io_number)
    Local $Num
    $Num = Abs($io_number)
    If $Num < 2 And $Num > -2 Then Return 1
    
    If Mod($Num, 2) = 0 Then Return 0
    
    For $a = 3 To Int(($Num / 2) + 1) Step 2
        If Mod($Num, $a) = 0 Then Return 0
    Next
    Return 1
EndFunc  ;==>IsPrime

Not tried it though!

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...