actionscript
Math2 ActionScript functions

Some math function are simple to write but often very useful in many situations. Here is part of flanture math2 actionscript library.
sum - sum of all numbers between l and h
prod - product of all numbers between l and h
sumAr - sum of all array elements
static function sumAr(a:Array):Number {
var s:Number = 0;
for(var i=0;i < a.length;i+=1){
s=s+a[i];
}
return s;
}
prodAr - product of all array elements
static function prodAr(a:Array):Number {
var p:Number = 1;
for(var i=0;i < a.length;i+=1){
p=p*a[i];
}
return p;
}
If any of array elements is zero, last function is zero, so:
prodArIZ - product of all array elements, zeros ignored
static function prodArIZ(a:Array):Number {
var p:Number = 1;
for(var i=0;i < a.length;i+=1){
if(a[i]!=0){
p=p*a[i];
}
}
return p;
}
Homework for newbies: write function that returns all prime numbers between 1 and n?

Post a Comment
1 Comments
Just for fun :)
ReplyDeletefunction isPrime (n:uint):Boolean
{
for (var i:uint=1; i<= Math.sqrt(n); i++)
{
var calc:uint = n%i;
if (calc == 0 && i != n && i != 1)
{
return false;
break;
}
}
return true;
}
trace(isPrime(7));
Thanks for sharing your thoughts !