Jump to content
Sign in to follow this  
/\/\1|<3

IsPrimeNumber function

Recommended Posts

/\/\1|<3

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

Share this post


Link to post
Share on other sites
phillip123adams

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

Share this post


Link to post
Share on other sites
/\/\1|<3

big thanks :idiot:

edited it

Mike

Share this post


Link to post
Share on other sites
Insolence

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.

Share this post


Link to post
Share on other sites
/\/\1|<3

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.

Share this post


Link to post
Share on other sites
Jos

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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
ezzetabi

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

Share this post


Link to post
Share on other sites
abel

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

Share this post


Link to post
Share on other sites
LazyCoder

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

Share this post


Link to post
Share on other sites
ezzetabi

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.

Share this post


Link to post
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
Sign in to follow this  

×