AutoHotkey Community

It is currently May 27th, 2012, 1:21 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: August 12th, 2008, 9:48 pm 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
the function returns 0 if no prime 1 if primenumber :)

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

_________________
Image
    All scripts, unless otherwise noted, are hereby released under CC-BY


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 13th, 2008, 4:27 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bon and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group