Jump to content

IsPrimeNumber function


/\/\1|<3
 Share

Recommended Posts

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 by /\/\1|<3
Link to comment
Share on other sites

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
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

NOTE: thereĀ“s a problem with it. see this

Mike

<{POST_SNAPBACK}>

Global got you! Add Local $i to the Func.

Phillip

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

  • Developers

somtimesĀ Ā  :idiot:

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Ā  :D

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 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.
Ā  :)

Link to comment
Share on other sites

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 by ezzetabi
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...
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...