Php Prime (prime number) implementation code. For more information, see.
The code is as follows:
Class timer
{
Var $ time_start;
Var $ time_end;
Function _ construct ()
{
$ This-> time_start = 0;
$ This-> time_end = 0;
}
Function timer ()
{
$ This->__ construct ();
}
Function start ()
{
List ($ usec, $ sec) = explode ("", microtime ());
$ This-> time_start = (float) $ usec + (float) $ sec;
}
Function stop ()
{
List ($ usec, $ sec) = explode ("", microtime ());
$ This-> time_end = (float) $ usec + (float) $ sec;
}
Function show ($ output = false)
{
$ Total = $ this-> time_end-$ this-> time_start;
If ($ output ){
Echo $ total, "sec ";
Return true;
}
Return $ total. "sec ";
}
}
?>
Echo 'check prime
';
Function IsPrime ($ I)
{
If ($ I <2)
{
Return false;
}
// Var $ iterator;
For ($ iterator = 2; $ iterator <= sqrt ($ I); $ iterator ++)
{
If ($ I % $ iterator = 0)
{
Return false;
}
}
Return true;
}
$ Sw = new timer ();
$ Sw-> start ();
For ($ j = 1; $ j <100; $ j ++)
{
If (IsPrime ($ j ))
{
Echo 'True
';
}
Else
{
Echo 'false
';
}
}
$ Sw-> stop ();
$ Sw-> show (true );
?>