PHP Classes
Icontem

File: primesfactory.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of zandor  >  Primes Factory  >  primesfactory.php  
File: primesfactory.php
Role: Class source
Content type: text/plain
Description: class code
Class: Primes Factory
Perform calculations with prime numbers
 

Contents

Class file image Download
<?php

# primesfactory
# coded by Alessandro Rosa
# e-mail : zandor_zz@yahoo.it
# site : http://malilla.supereva.it

# Copyright (C) 2006  Alessandro Rosa

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

# Compiled with PHP 4.4.0

class primesfactory
{
    function 
primesfactory()
    {
          
$this->reset() ;
    }

    function 
reset()
    {
        
$this->number ;
      
        
$this->factors = array();
        
$this->exponents = array();
    
        
$this->err_no = -;
        
$this->err_msg "" ;

        
$this->style_id ;
        
        
// however you can set it to the number of seconds
        // or to 0 whether you want to remove any execution time limit
        
ini_set('max_execution_time',0);
    }

    function 
set_style$id )     {     $this->style_id $id ;      }
    function 
get_style()          {     return $this->style_id ;     }

    function 
isok()
    {
        return ( 
$this->err_no == -) ? true false ;
    }

    function 
display_error()
    {
        switch( 
$this->err_no )
        {
              case 
:
                
$this->err_msg "The number $this->number is not integer;" ;
              break;
              case 
:
                
$this->err_msg "The string $this->number does not appear to be a valid number;" ;
              break;
              default:
              break;
        }
        
        echo 
"<font color=red><b>$this->err_msg</b></font>";
    }

    function 
insert_number$n )
    {
        
$this->number $n ;

        
$this->factors = array();
        
$this->exponents = array();
    }

    function 
isnumeric$sText )
    {
       
$ValidChars "0123456789";
    
       for (
$i 0$i strlen$sText ) - 1$i++)
       {
              
$Char $sText{$i};
              
              if ( 
strpos$ValidChars$Char ) === false )
              return 
;
       }

       return 
0;
    }

    function 
isdecimal$n )
    {
        
$val $n ;
        
        if ( ( 
$n $val ) == ) return ;
        else return 
;
        
// this means that ",00" was the decimal part: so it's integer
        
        
if ( strpos$n"." ) > || strpos$n"," ) > ) return ;
        
        return 
;
    }

    function 
isprime$n )
    {
        for( 
$i $i <= $n $i++ )
        {
            if ( 
$n $i == ) return false ;
        }
        
        return 
true ;
    }

    function 
isperfect()
    {
        
$sum ;
        
        for( 
$i $i $this->number $i++ )
        {
            if ( 
$this->number $i == $sum += $i ;
        }
        
        return ( 
$sum == $this->number ) ? true false ;
    }

    function 
primesbefore()
    {
        
$count ;
        
        for( 
$i $i $this->number $i++ )
        {
            if ( 
$this->isprime$i ) ) $count++ ;
        }
        
        return 
$count ;
    }


    function 
go()
    {
        
$err_catch 0  ;
        
        
$err_catch += $this->isdecimal$this->number ) ;
    
        if ( 
$err_catch )
        {
            
$this->err_no ;
            return ;
        }
        
        
$err_catch += $this->isnumeric$this->number ) ;
        
        if ( 
$err_catch )
        {
            
$this->err_no ;
            return ;
        }
        
        
$this->factorization$this->number ) ;
    }

    function 
factorization$n )
    {
        if ( 
$n == )
        {
           
$this->add_factor) ;
           return ;
        }
        
        
$remainder = -;
        
        for( 
$i $i <= $n $i++ )
        {
            
$remainder $n $i ;

            if ( 
$this->isprime$i ) && $remainder == )
            {
                
$this->add_factor$i ) ;
                                    
                
$n /= $i ;
                
                
$i ;
                
// $i is set to 1 : the next loop will increment it to 2,
                // so we are in the conditions to restart the factorization
            
}
        }
    }
    
    function 
add_factor$factor )
    {
        
$n_elements count$this->factors );        
        
        
$bADD true ;
        
        for ( 
$i $i $n_elements$i++ )
        {
            if ( 
$this->factors[$i] == $factor )
            {
                
$bADD false ;
                break ;
            }
        }

        if ( 
$bADD )
        {
            
// create a new entry in the array
            // and fills it with the new factor entry

            
$this->factors[$i] = $factor ;
            
$this->exponents[$i] = ;
        }
        else
        {
            
// the factor was found, so only the related exponent
            // shall be updated
            // $this->factors[ $i ] = $factor ;

            
$this->exponents[$i]++ ;

        }
        
        return 
true ;
    }

    function 
display()
    {
          
$ret_str "" ;
          
$n_elements count$this->factors );
          
          if ( 
$this->style_id == 2$ret_str .= "$" // TeX/LaTeX opener for mathematical formulas
          
          
for ( $i $i $n_elements $i++ )
          {
              if ( 
$this->exponents[$i] > )
              {
                    
$ret_str .= $this->factors[$i];
                    
                    switch( 
$this->style_id )
                    {
                        case 
0:
                            
$ret_str .= "^" ;
                        break;
                        case 
1:
                            
$ret_str .= "<sup>" ;
                        break;
                        case 
2:
                            
$ret_str .= "^{" ;
                        break;
                    }
                    
                    
$ret_str .= $this->exponents[$i];

                    switch( 
$this->style_id )
                    {
                        case 
0:
                            
$ret_str .= "" ;
                        break;
                        case 
1:
                            
$ret_str .= "</sup>" ;
                        break;
                        case 
2:
                            
$ret_str .= "}" ;
                        break;
                    }
              }
              else 
$ret_str .= $this->factors[$i] ;
          
              if ( 
$i $n_elements )
              {
                    switch( 
$this->style_id )
                    {
                        case 
0:
                            
$ret_str .= " x " ;
                        break;
                        case 
1:
                            
$ret_str .= "&bull;";
                        break;
                        case 
2:
                            
$ret_str .= "\\times" ;
                        break;
                    }
              }
          }

        if ( 
$this->style_id == $ret_str .= "$" // TeX/LaTeX closer for mathematical formulas
    
        
return $ret_str ;
    }


    var 
$factors ;
    var 
$exponents ;
    
    var 
$number ;
    var 
$err_no ;
    var 
$err_msg ;
    var 
$style_id ;  // 0 for plain, 1 for html, 2 for TeX/LaTeX2e

}

?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products