AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[function] isPrime - is a number a primenumber or is it not?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
DerRaphael



Joined: 23 Nov 2007
Posts: 604
Location: 127.0.0.1

PostPosted: Tue Aug 12, 2008 8:48 pm    Post subject: [function] isPrime - is a number a primenumber or is it not? Reply with quote

the function returns 0 if no prime 1 if primenumber Smile

Code:

; demo.primenumber.ahk
MsgBox % isPrime(31337)

isPrime(iNr) { ; (w) by DerRaphael / zLib style release
   ; Thx Eratosthenes :)
   Loop, % Floor(Sqrt(iNr))
   {
      n := A_Index, cPrime := 1, ret := true
      if (A_index>1)
         Loop % n-1
            if ((Mod(n,a_index)=0) && (A_index>1)) {
               cPrime := 0
               break
            }
      if ((cPrime) && (A_index>1) && (Mod(iNr,n)=0)) {
         ret := false
         break
      }
   }
   Return ret
}



greets
dR
_________________
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Wed Aug 13, 2008 3:27 am    Post subject: Reply with quote

Nice. You could also use the divisor finding functions from here, or the machine code (fast) prime finding functions, also used in the popup calculator.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group