Blockland Forums > Modification Help

[Resource] Factoring + Is Integer + Is Prime

Pages: (1/2) > >>

Axolotl:

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: ---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;
}
}
}
}

--- End code ---


Nexus:

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

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

Axolotl:


--- Quote from: Nexus on March 18, 2012, 05:48:05 PM ---==>echo("word" == mfloor("word"));
1
==>echo("not an empty string" == NULL);
1

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

--- End quote ---
oh heh

Greek2me:

Here's a few more:

isNumber

--- Code: (isNumber) ---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;
}
--- End code ---
Any valid number (152, -5.7, 64.90, -93) will return true.

stripTrailingZeros

--- Code: (stripTrailingZeros) ---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;
}
--- End code ---
Removes excess 0's from the end of a decimal. (5.679000 => 5.679)

Nexus:


--- Quote from: Axolotl on March 18, 2012, 05:57:52 PM ---oh heh

--- End quote ---

mInteger should also use $=

Pages: (1/2) > >>

Go to full version