/\/\1|<3 Posted December 26, 2004 Posted December 26, 2004 (edited) here it is: Func IsPrimeNumber($num) If IsNumber($num) = 0 Or $num = 0 Then Return -1 Else Local $pnum = 0 Local $npnum = 0 Local $i $i = 2 While $i < Int($num) $ch = $num / $i $i = $i + 1 If $ch = Int($ch) Then $npnum = 1 Else $pnum = 1 EndIf WEnd If $num = 2 Then Return 1 ElseIf $npnum = 1 And $num <> 2 Then Return 0 ElseIf $pnum = 1 And $npnum = 0 Then Return 1 EndIf EndIf EndFunc it checks if a number is a prime number. if it is, the function returns 1, if not it returns 0. if you insert a string or 0 (which is not a real number) it returns -1 Mike Edited December 26, 2004 by /\/\1|<3
phillip123adams Posted December 26, 2004 Posted December 26, 2004 here it is:Func IsPrimeNumber($num) If IsNumber($num) = 0 Or $num = 0 Then Return -1 Else Local $pnum = 0 Local $npnum = 0 $i = 2 While $i < Int($num) $ch = $num / $i $i = $i + 1 If $ch = Int($ch) Then $npnum = 1 Else $pnum = 1 EndIf WEnd If $num = 2 Then Return 1 ElseIf $npnum = 1 And $num <> 2 Then Return 0 ElseIf $pnum = 1 And $npnum = 0 Then Return 1 EndIf EndIf EndFuncit checks if a number is a prime number. if it is, the function returns 1, if not it returns 0. if you insert a string or 0 (which is not a real number) it returns -1NOTE: there“s a problem with it. see thisMike<{POST_SNAPBACK}>Global got you! Add Local $i to the Func. Phillip
Insolence Posted December 26, 2004 Posted December 26, 2004 You have to use Local for variables inside funcs? "I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.
/\/\1|<3 Posted December 26, 2004 Author Posted December 26, 2004 somtimes if your script says something like "variable undeclared" but you“re sure that you declared it, then use Local. but i“d always use it in functions to avoid errors Mike.
Developers Jos Posted December 26, 2004 Developers Posted December 26, 2004 (edited) somtimesĀ Ā if your script says something like "variable undeclared" but youĀ“re sure that you declared it, then use Local. but iĀ“d always use it in functions to avoid errorsĀ Mike.<{POST_SNAPBACK}>It is a good practice to always define variables that are only used inside the Func...EndFunc as Local.I think what is meant here is that you didn't define $I as LOCAL.... Edited December 26, 2004 by JdeB SciTE4AutoIt3 Full installer Download page Ā -Ā Beta filesĀ Ā Ā Ā Read before postingĀ Ā Ā How to post scriptsourceĀ Ā Ā Forum etiquetteĀ Forum RulesĀ Ā Live for the present, Dream of the future, Learn from the past.Ā
ezzetabi Posted December 27, 2004 Posted December 27, 2004 (edited) Actually 0 is a real number... But the point of this post is an other interesting way to check is a number is prime or not. Being p the number that you want checking p is prime if (p-1)! = [-1] That it means the modulus of (p-1)! dividing by p is (p-1) E.g. lets check 7 7-1 = 6 6! = 720 720 = 102 * 7 + 6 6 is the modulus that is equal to 7 - 1. 7 is prime. Of course calculating the ! of big numbers 'may' be a problem so this is not very confortable... Yet... This is my way. MsgBox(0,'',_IsPrime(4999)) Func _IsPrime($sNum) If $sNum = 0 Then Return -1 $sNum = Int($sNum) If $sNum < 0 Then $sNum = $sNum * -1 Local $c, $bPrime = 1 If $sNum > 2 Then For $c = 2 to Int($sNum ^ (1/2)+1) If Mod($sNum,$c) = 0 Then $bPrime = 0 ExitLoop EndIf Next EndIf Return $bPrime EndFunc About the advices you got I'll add one. When you code use always Opt ('MustDeclareVars', 1) Overall if you want to share your functions! Edited December 27, 2004 by ezzetabi
abel Posted December 28, 2004 Posted December 28, 2004 hello ezzetabi, what do you think about checking only the odd factors greater than 2? MsgBox(0,'',_IsPrime(4999)) Func _IsPrime($sNum) If $sNum = 0 Then Return -1 $sNum = Int($sNum) If $sNum < 0 Then $sNum = $sNum * -1 Local $c, $bPrime = 1 If $sNum > 2 Then If Mod($sNum,2) = 0 Then Return 0 For $c = 3 to Int($sNum ^ (1/2)+1) Step 2 If Mod($sNum,$c) = 0 Then $bPrime = 0 ExitLoop EndIf Next EndIf Return $bPrime EndFunc Your way to check if a number is prim or not using the ! is very interesting. I never saw this method before. abel
LazyCoder Posted December 28, 2004 Posted December 28, 2004 Let's say P is the number. In fact, the best "common" way is to test (decending order) all dividers lower than int(sqrt(P)) as at most P=(sqrt(P))^2, avoiding all even factors. Of course, you can easily check that P can't be simply divided by 2... You'll save time this way... A good program computing A into B is mostly one that won't crash in all the other cases...
ezzetabi Posted December 28, 2004 Posted December 28, 2004 The best is testing the modulus of all prime numbers smaller or equal than the square root (rounded to the next int) than the number you have to check.More or less as I did in my udf for finding prime numbers.
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