Jump to content

IsPrimeNumber


/\/\1|<3
 Share

Recommended Posts

i created this function:

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

example:

$z = IsPrimeNumber(2)
If $z = 1 Then
   $y = "TRUE"
ElseIf $z = 0 Then
   $y = "FALSE"
ElseIf $z = -1 Then
   $y = "NO NUMBER"
EndIf
MsgBox(0, "is prime?", $y)

this returns 1 and displays TRUE

but if i use it like this:

$log = @ScriptDir & "\Primzahlen.txt"

$op = FileOpen($log, 1)
For $i = 1 To 100
   If IsPrimeNumber($i) = 1 Then
      FileWriteLine($log, $i)
   EndIf
Next
FileClose($op)

it doesnt write 2 in the log but as you know 2 returns 1 and is a prime number, too.

now my question: where´s the mistake? :idiot:

Mike

Edited by /\/\1|<3
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...