erifash Posted January 16, 2005 Share Posted January 16, 2005 Here is the smallest of the functions that check if a number is prime or not: Func _prime($num) If $num < 2 or $num <> Int($num) Then Return -1 For $i = $num To 2 Step -1 If $i <> $num Then If $num / $i = Int($num / $i) Then Return 0 EndIf Next Return 1 EndFunc Returns 1 if it is prime or 0 if it's not or -1 if there is an error. Unfortunately, the bigger the number is, the longer it will take... My UDFs:_FilePrint() | _ProcessGetName() | _Degree() and _Radian()My Scripts:Drive Lock - Computer Lock Using a Flash DriveAU3Chat - Simple Multiuser TCP ChatroomStringChunk - Split a String Into Equal PartsAutoProxy - Custom Webserver Link to comment Share on other sites More sharing options...
Valik Posted January 16, 2005 Share Posted January 16, 2005 (edited) I see three things...First, don't return -1 on error, set the @error macro. The reason is, a function like this is best used as:If _prime($num) Then ...The problem is, -1 is true. Only 0 is false in AutoIt. Thus, this will return "true" when you mean to return an error state.Second, instead of:$num <> Int($num)You can use:IsInt($num)Lastly, and possibly a performance improvement...Isn't:If $num / $i = Int($num / $i) Then Return 0The same as:If Not Mod($num, $i) Then Return 0Thats one less divide, and divide is a very expensive operation.Edit: Fixed mistake in code (Hopefully). Edited January 16, 2005 by Valik Link to comment Share on other sites More sharing options...
CyberSlug Posted January 16, 2005 Share Posted January 16, 2005 Also, the first index of your for loop can be Int(Sqrt($num)) instead of $num Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig! Link to comment Share on other sites More sharing options...
erifash Posted January 16, 2005 Author Share Posted January 16, 2005 (edited) Thanks guys. Is this the code you mean? Func _prime($num) If $num < 2 or not IsInt($num) Then Return 0 For $i = $num To 2 Step -1 If $i <> $num Then If not Mod($num, $i) Then Return 0 EndIf Next Return 1 EndFunc Edit: Fixed a small bug in the code. Edited January 16, 2005 by erifash My UDFs:_FilePrint() | _ProcessGetName() | _Degree() and _Radian()My Scripts:Drive Lock - Computer Lock Using a Flash DriveAU3Chat - Simple Multiuser TCP ChatroomStringChunk - Split a String Into Equal PartsAutoProxy - Custom Webserver Link to comment Share on other sites More sharing options...
ezzetabi Posted January 16, 2005 Share Posted January 16, 2005 I think it is better from 3 to the number. Many number can be divided by small numbers, like 3, 5, 7, 11... MsgBox(0, '', _prime(4999)) Func _prime($num) If $num < 2 Or Not IsInt($num) Then Return 0 If Not Mod($num, 2) Then Return 0 For $i = 3 To Int(Sqrt($num) + 1) Step 2 If $i <> $num Then If Not Mod($num, $i) Then Return 0 EndIf Next Return 1 EndFunc ;==>_prime Link to comment Share on other sites More sharing options...
ezzetabi Posted January 16, 2005 Share Posted January 16, 2005 (edited) in fact, try this: $t = TimerInit() For $c = 1 to 1000 _prime1($c) Next MsgBox(0,'','erifash ms ' & TimerDiff($t)) $t = TimerInit() For $c = 1 to 1000 _prime2($c) Next MsgBox(0,'','ezzetabi ms ' & TimerDiff($t)) Func _prime1($num);erifash If $num < 2 or not IsInt($num) Then Return 0 For $i = $num To 2 Step -1 If $i <> $num Then If not Mod($num, $i) Then Return 0 EndIf Next Return 1 EndFunc Func _prime2($num);ezzetabi If $num < 2 Or Not IsInt($num) Then Return 0 If Not Mod($num, 2) Then Return 0 For $i = 3 To Int(Sqrt($num) + 1) Step 2 If $i <> $num Then If Not Mod($num, $i) Then Return 0 EndIf Next Return 1 EndFunc;==>_prime Edited January 16, 2005 by ezzetabi Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now