forked from as3/as3-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathisPrime.as
More file actions
29 lines (25 loc) · 667 Bytes
/
isPrime.as
File metadata and controls
29 lines (25 loc) · 667 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package utils.number
{
/**
Determines if the number is prime.
@param value: A number to determine if it is only divisible by <code>1</code> and itself.
@return Returns <code>true</code> if the number is prime; otherwise <code>false</code>.
@example
<code>
trace(NumberUtil.isPrime(13)); // Traces true
trace(NumberUtil.isPrime(4)); // Traces false
</code>
*/
public function isPrime(value:Number):Boolean
{
if (value == 1 || value == 2)
return true;
if (isEven(value))
return false;
var s:Number = Math.sqrt(value);
for (var i:Number = 3; i <= s; i++)
if (value % i == 0)
return false;
return true;
}
}