# Prime

## Recommended Posts

I wrote this script to judge if it's prime or not...

Is any method to do this work more pretty ?

```\$x = InputBox("IsPrime","Input a number to judge if it is prime","","",160 ,150 ,610 ,203)
If \$x == 1 Then
MsgBox(0,"Prime","Is Not Prime or Prime")
ElseIf \$x == 2 Then
MsgBox(0,"Prime","Is Prime")
Else
EndIf

For \$y = 2 To \$x-1 Step +1
If(Mod(\$x , \$y)) = 0 Then
MsgBox(0,"Prime","Is Not Prime")
ExitLoop
Else
MsgBox(0,"Prime","Is Prime")
ExitLoop
EndIf
Next```

##### Share on other sites

I have this in my snippet folder (not created by me, I think UEZ if my memory serves me correct.)

```ConsoleWrite(_IsPrimeNumber(1) & @CRLF)

Local \$iDivisor, \$iIncrement, \$iMaxDivisor
If StringIsDigit(\$iNumber) = 0 Then
Return SetError(1, 0, -1)
EndIf
If \$iNumber > 3 Then
If Mod(\$iNumber, 2) = 0 Then
Return 0
EndIf
If Mod(\$iNumber, 3) = 0 Then
Return 0
EndIf
EndIf

\$iDivisor = 5
\$iIncrement = 2
\$iMaxDivisor = Sqrt(\$iNumber) + 1

Do
If Mod(\$iNumber, \$iDivisor) = 0 And \$iNumber <> \$iDivisor Then
Return 0
EndIf
\$iDivisor = \$iDivisor + \$iIncrement
\$iIncrement = 6 - \$iIncrement
Until \$iDivisor > \$iMaxDivisor
Return 1

##### Share on other sites

@guinness According to that function 1 is a prime...

Here is an SRE version, just for giggles.

```ConsoleWrite(_IsPrime(1) & @CRLF)
ConsoleWrite(_IsPrime(3) & @CRLF)
ConsoleWrite(_IsPrime(9) & @CRLF)
ConsoleWrite(_IsPrime(29) & @CRLF)

Func _IsPrime(\$iNum)
Local \$sNum = ''
For \$i = 1 To \$iNum Step 1
\$sNum &= '1'
Next
Return Not StringRegExp(\$sNum, '^(1?|(11+?)\2+)\$')
EndFunc```

##### Share on other sites

Another interesting flavor:

```; prime test using simple regexp

For \$i = 2 to 20 ; we all know that neither 0 or 1 are primes
ConsoleWrite(\$i & ' is ')
If IsPrime(\$i) Then
ConsoleWrite('prime.' & @LF)
Else
ConsoleWrite('composite.' & @LF)
EndIf
Next

Func IsPrime(\$iTest)
Local \$s
For \$j = 1 To \$iTest
\$s &= 'a'
Next
StringRegExp(\$s, '^(aa+)\1+\$', 1)
Return(@error <> 0)
EndFunc```

Edit: I've been taken over at high speed...

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

I know, it's a habit I picked up not long ago, to write the Step as well.

Edit: And is it me or did the code indentation issue get worse?

Edited by Robjong

##### Share on other sites

Seems like the basic editor destroys indents. A dent against me?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

Seems like the basic editor destroys indents. A dent against me?

Not at all, even with the full editor I had to put in indentation manually, when pasting it all disappears.

##### Share on other sites

@Robjong - use the basic editor, it works a hell of a lot better.

@jchd - can you explain your SRE, please?

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

##### Share on other sites

Here you will find a decent explenation of these patterns:

http://tinyurl.com/regexp-primes

Edit: the original URL was not converted correctly so I had to make a tiny one

Edited by Robjong

##### Share on other sites

Thanks Rob,

As usual, I understand it in theory but have NO chance of applying it in practice. Going to re-read the PCRE doc (again) for what/why/when backtracking is.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

##### Share on other sites

Here you will find a decent explenation of these patterns:

http://tinyurl.com/regexp-primes

Edit: the original URL was not converted correctly so I had to make a tiny one

An informative article about that SRE, which when I looked at it I thought "huh?!"

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

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

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