UDF

Recommended Posts

ok i know this is a dumb question but there are some files that people have posted that when i download it and run it nothing happens.

A couple of these files said there a UDF and some dont say.

heres an example

http://www.autoitscript.com/forum/index.ph...=24643&st=0

i copy the script and save it as .au3 then run it but nothing happens

"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share on other sites

A UDF is a 'script' made to be used with #include. Once you have 'included' the UDF, you can Call the functions inside that UDF. For example:

```#include <IsPrime.au3>

If _IsPrime(3) Then
MsgBox(0, "", It's a prime")
Else
MsgBox(0, "", "It's not a prime")
EndIf```

Share on other sites

That is a UDF only. You need to call it with a integer to evalute if it is prime. The values returned are 0 or 1. 1 is for success so we will test for it.

```; Test the condition and if it is successful, then you will see the msgbox
If _IsPrime(7) And Not @error Then
MsgBox(0, '', 'Is a prime number')
EndIf

;===============================================================================
;
; Function Name:   _IsPrime
; Description:: Check if a number is prime or not
; Parameter(s): \$i_num - Number to check
; Requirement(s):  None
; Return Value(s): 1 - Number is prime
;              0 - Number is not prime
;               -1 - String isn't a number
; Author(s):       RazerM
;
;===============================================================================
;
Func _IsPrime(\$i_num)
If StringIsDigit(\$i_num) = 0 Then Return SetError(1, 0, 0)
If \$i_num > 3 Then
If Mod(\$i_num, 2) = 0 Then Return 0
If Mod(\$i_num, 3) = 0 Then Return 0
EndIf
If \$i_num = 1 Then Return 0
Local \$divisor, \$increment, \$maxdivisor
\$divisor = 5
\$increment = 2
\$maxdivisor = Sqrt(\$i_num) + 1

Do
If Mod(\$i_num, \$divisor) = 0 And \$i_num <> \$divisor Then Return 0
\$divisor = \$divisor + \$increment
\$increment = 6 - \$increment
Until \$divisor > \$maxdivisor
Return 1
EndFunc ;==>_IsPrime```

The 7 is passed to the function in the parameter variable \$i_num. I changed Dim to Local and added SetError rather then -1 return .

Edited by MHz

Share on other sites

ok guys

i have this gui, and if the put a number into the imputbox and press the button i want a msg box to tell me if the number is prime.

```#include <GuiConstants.au3>

GuiCreate("Prime Finder", 316, 111,-1, -1 , BitOR(\$WS_OVERLAPPEDWINDOW, \$WS_CLIPSIBLINGS))

\$Button_1 = GuiCtrlCreateButton("Is It Prime?", 60, 60, 190, 30)
\$Input_2 = GuiCtrlCreateInput("", 10, 20, 300, 30)

GuiSetState()
While 1
\$msg = GuiGetMsg()
Select
Case \$msg = \$GUI_EVENT_CLOSE
ExitLoop
Case \$msg = \$Button_1
;read the input box here then find out if it is prime.
;if it is prime the msgbox" the number is prime
;if it isn't prime then msgbox the number is not prime
EndSelect
WEnd```

help plz

"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share on other sites

That is a UDF only. You need to call it with a integer to evalute if it is prime. The values returned are 0 or 1. 1 is for success so we will test for it.

```; Test the condition and if it is successful, then you will see the msgbox
If _IsPrime(7) And Not @error Then
MsgBox(0, '', 'Is a prime number')
EndIf
;===============================================================================
;
; Function Name:   _IsPrime
; Description:: Check if a number is prime or not
; Parameter(s): \$i_num - Number to check
; Requirement(s):  None
; Return Value(s): 1 - Number is prime
;              0 - Number is not prime
;               -1 - String isn't a number
; Author(s):       RazerM
;
;===============================================================================
;
Func _IsPrime(\$i_num)
If StringIsDigit(\$i_num) = 0 Then Return SetError(1, 0, 0)
If \$i_num > 3 Then
If Mod(\$i_num, 2) = 0 Then Return 0
If Mod(\$i_num, 3) = 0 Then Return 0
EndIf
If \$i_num = 1 Then Return 0
Local \$divisor, \$increment, \$maxdivisor
\$divisor = 5
\$increment = 2
\$maxdivisor = Sqrt(\$i_num) + 1

Do
If Mod(\$i_num, \$divisor) = 0 And \$i_num <> \$divisor Then Return 0
\$divisor = \$divisor + \$increment
\$increment = 6 - \$increment
Until \$divisor > \$maxdivisor
Return 1
EndFunc ;==>_IsPrime

One is not a prime number?

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share on other sites

ok guys

i have this gui, and if the put a number into the imputbox and press the button i want a msg box to tell me if the number is prime.

```#include <GuiConstants.au3>

GuiCreate("Prime Finder", 316, 111,-1, -1 , BitOR(\$WS_OVERLAPPEDWINDOW, \$WS_CLIPSIBLINGS))

\$Button_1 = GuiCtrlCreateButton("Is It Prime?", 60, 60, 190, 30)
\$Input_2 = GuiCtrlCreateInput("", 10, 20, 300, 30)

GuiSetState()
While 1
\$msg = GuiGetMsg()
Select
Case \$msg = \$GUI_EVENT_CLOSE
ExitLoop
Case \$msg = \$Button_1
;read the input box here then find out if it is prime.
;if it is prime the msgbox" the number is prime
;if it isn't prime then msgbox the number is not prime
EndSelect
WEnd
oÝ÷ ÙKjrüIl{ôáz|"¶¨ºµÚ¢Ø^­ë-jëh×6If _IsPrime(\$Data)
; Code to do for prime
Else
; Code for not prime
ÌÍÐ]ÛÌJJBÈÛÙHÈÈÜ[YB[ÙBÈÛÙHÜÝ[YB[Y```

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share on other sites

Dethedric, putting your code together with Manadar's, here is the resulting code:

```#include <GuiConstants.au3>

GuiCreate("Prime Finder", 316, 111,-1, -1 , BitOR(\$WS_OVERLAPPEDWINDOW, \$WS_CLIPSIBLINGS))

\$Button_1 = GuiCtrlCreateButton("Is It Prime?", 60, 60, 190, 30)
\$Input_2 = GuiCtrlCreateInput("", 10, 20, 300, 30)

GuiSetState()
While 1
\$msg = GuiGetMsg()
Select
Case \$msg = \$GUI_EVENT_CLOSE
ExitLoop
Case \$msg = \$Button_1
Local \$x01 = GUICtrlRead (\$Input_2 )
If _IsPrime (\$x01 ) And Not @error Then
MsgBox(0, \$x01 & " - Prime?", "Yes" )
Else
MsgBox(0, \$x01 & " - Prime?", "No" )
EndIf
EndSelect
WEnd

;===============================================================================
;
; Function Name:   _IsPrime
; Description:: Check if a number is prime or not
; Parameter(s): \$i_num - Number to check
; Requirement(s):  None
; Return Value(s): 1 - Number is prime
;             0 - Number is not prime
;              -1 - String isn't a number
; Author(s):       RazerM
;
;===============================================================================
;
Func _IsPrime(\$i_num)
If StringIsDigit(\$i_num) = 0 Then Return SetError(1, 0, 0)
If \$i_num > 3 Then
If Mod(\$i_num, 2) = 0 Then Return 0
If Mod(\$i_num, 3) = 0 Then Return 0
EndIf
If \$i_num = 1 Then Return 0
Local \$divisor, \$increment, \$maxdivisor
\$divisor = 5
\$increment = 2
\$maxdivisor = Sqrt(\$i_num) + 1

Do
If Mod(\$i_num, \$divisor) = 0 And \$i_num <> \$divisor Then Return 0
\$divisor = \$divisor + \$increment
\$increment = 6 - \$increment
Until \$divisor > \$maxdivisor
Return 1
EndFunc;==>_IsPrime```
Edited by Squirrely1

Share on other sites

@PsaltyDS

The latest udf that is in my signature is different from the one you criticise

```;===============================================================================
;
; Function Name:   _IsPrime
; Description:: Check if a number is prime or not
; Parameter(s): \$i_num - Number to check
; Requirement(s):  None
; Return Value(s): 1 - Number is prime
;              0 - Number is not prime
;               -1 - String isn&apos;t a number
; Author(s):       RazerM
;
;===============================================================================
;
Func _IsPrime(\$i_num)
If StringIsDigit(\$i_num) = 0 Then Return -1
If \$i_num > 3 Then
If Mod(\$i_num, 2) = 0 Then Return 0
If Mod(\$i_num, 3) = 0 Then Return 0
EndIf
If \$i_num = 1 Then Return 0
Dim \$divisor, \$increment, \$maxdivisor
\$divisor = 5
\$increment = 2
\$maxdivisor = Sqrt(\$i_num) + 1

Do
If Mod(\$i_num, \$divisor) = 0 And \$i_num <> \$divisor Then Return 0
\$divisor = \$divisor + \$increment
\$increment = 6 - \$increment
Until \$divisor > \$maxdivisor
Return 1
EndFunc ;==>_IsPrime```

As you can see it returns -1 and that's it

Also, 1 is not a prime number.

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.

Share on other sites

thanks Squirrely1 its exactly what i wanted,

also thanks RazerM for the function

i need this for a school projct

"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share on other sites

@PsaltyDS

... 1 is not a prime number.

"[A]prime number (or a prime) is a natural number that has exactly two (distinct) natural number divisors, which are 1 and the prime number itself."

Momma always said: "Larn someth'n new every day."

I get to go home now...

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Create an account

Register a new account