| View previous topic :: View next topic |
| Author |
Message |
DerRaphael
Joined: 23 Nov 2007 Posts: 604 Location: 127.0.0.1
|
Posted: Tue Aug 12, 2008 8:48 pm Post subject: [function] isPrime - is a number a primenumber or is it not? |
|
|
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 _________________
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4078 Location: Pittsburgh
|
Posted: Wed Aug 13, 2008 3:27 am Post subject: |
|
|
| 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 |
|
 |
|