Author Topic: [Resource] Factoring + Is Integer + Is Prime  (Read 2836 times)

mInteger(%a); - is %a an integer?
mPrime(%a); - is %a a prime number?
mFactors(%a); - returns all the factors of %a

mFactors will spaz out when you put in something near or over 1,000,000
Code: [Select]
function mInteger(%a){return (%a $= mfloor(%a));} // Is the number an integer?

function mPrime(%n) // Is the number prime?
{
for(%i=2;%i<%n;%i++)
{
if(%n % %i == 0)
{
return 0;
}
}
return 1;
}

function mFactors(%a) // Get all the factors of %a
{
for(%i=1;%i<=%a;%i++)
{
if(mInteger(%a / %i))
{
if(%factors $= "")
{
%factors = %i;
}
else
{
%factors = %factors SPC %i;
}
}
}
}
« Last Edit: March 18, 2012, 06:45:58 PM by Axolotl »

==>echo("word" == mfloor("word"));
1
==>echo("not an empty string" == NULL);
1

use $= instead of == and "" instead of NULL
« Last Edit: March 18, 2012, 04:50:29 PM by Nexus »

==>echo("word" == mfloor("word"));
1
==>echo("not an empty string" == NULL);
1

use $= instead of == and "" instead of NULL
oh heh

Here's a few more:

isNumber
Code: (isNumber) [Select]
function isNumber(%flag)
{
if(%flag $= "")
return 0;

%check = "-.0123456789";

%lastPos = -1;
for(%i=0; %i < strLen(%flag); %i++)
{
%char = getSubStr(%flag,%i,1);

%pos = strPos(%check,%char);
if(%pos < 0)
return 0;
if(%pos == 0)
{
if(%neg)
return 0;
if(%lastPos < 2 && %lastPos >= 0)
return 0;
%neg = 1;
}
if(%pos == 1)
{
if(%dec)
return 0;
if(%lastPos < 2 && %lastPos >= 0)
return 0;
%dec = 1;
}

%lastPos = %pos;
}

return 1;
}
Any valid number (152, -5.7, 64.90, -93) will return true.

stripTrailingZeros
Code: (stripTrailingZeros) [Select]
function stripTrailingZeros(%flag)
{
if(!isNumber(%flag))
return %flag;

%pos = -1;
for(%i=0; %i < strLen(%flag); %i++)
{
%char = getSubStr(%flag,%i,1);

if(%char $= ".")
{
%dec = 1;
%pos = %i;
%zero = 1;
continue;
}
if(%char $= "0" && %dec)
{
if(!%zero)
%pos = %i;
%zero = 1;
}
else
{
%zero = 0;
%pos = -1;
}
}

if(%pos > 0)
%flag = getSubStr(%flag,0,%pos);

return %flag;
}
Removes excess 0's from the end of a decimal. (5.679000 => 5.679)


Code: [Select]
function mSumSeries(%n,%f,%d)
{
//n = number of terms
//f = first term
//d = difference
return %n/2*((2*%f)+(%n-1)*%d);
}
ex:
mSumSeries(5,1,1); = 1+2+3+4+5 = 15
mSumSeries(4,2,1); = 2+3+4+5 = 14
mSumSeries(3,1,2); = 1+3+5 = 9


nvm, will ask in seperate topic

Code: [Select]
function mSumSeries(%n,%f,%d)
{
//n = number of terms
//f = first term
//d = difference
return %n/2*((2*%f)+(%n-1)*%d);
}
ex:
mSumSeries(5,1,1); = 1+2+3+4+5 = 15
mSumSeries(4,2,1); = 2+3+4+5 = 14
mSumSeries(3,1,2); = 1+3+5 = 9



Arithmetic sequence!