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

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

big thanks

edited it

Mike

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

##### Share on other sites

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.

##### Share on other sites

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

SciTE4AutoIt3 Full installer Download page Ā  -Ā Beta filesĀ  Ā  Ā  Ā  Ā  Ā
Ā
Live for the present,
Dream of the future,
Learn from the past.
Ā

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

Opt ('MustDeclareVars', 1)

Overall if you want to share your functions!

Edited by ezzetabi

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

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

##### Share on other sites

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

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...