# Object Oriented Factorization Program in PHP

I love actuarial stuff.  But, when I am learning any kind of new programming or mathematics, I always think number theory.

On my deathbed, I will probably not be thinking about loss ratios.  But I will be thinking about divisibility.  Number theory is the thing that originally drew me, and many others, to mathematics.

So, in the interest of learning PHP, I wanted to put a factorization application up on my website. But I wanted to be able to factorize more than one number at a time, so that one can observe the relationship between properties of different integers. So, I thought, what I want is several instances of the factorizer. That means writing an object oriented application.
You will observe below that the constructor does all of the work. That is because number theory stuff is very calculation intensive. Many of the variables, like phi, can be calculated as by-products of other calculations.

class DestructNumber
{ var \$N;
private \$sumofdivisors = 0;
private \$divisorlist=array();
private \$primedivisorlist=array();
private \$primedivisorstring;
private \$phi = 1;
// this constructor does all of the work
// that allows the utility to use a more efficient
// algorithm than a strictly obect oriented approach
// would allow

public function __construct(\$N) { \$this->number = \$N;
// find half of divisors
\$counter = 1;
\$end = floor(sqrt(\$this->number));
for (\$m=1; \$m <= \$end;\$m++ ) { if (\$this->number%\$m == 0)
{\$this->divisorlist[\$counter]=\$m;
\$this->sumofdivisors=\$this->sumofdivisors + \$m;
\$counter++;
}
}
//find other half of divisors
for( \$m = \$counter-1;\$m > 0;\$m--)
{
\$quotient = \$this->number/\$this->divisorlist[\$m];
if (\$this->divisorlist[\$m] != \$quotient)
{\$this->divisorlist[\$counter] = \$quotient;
\$this->sumofdivisors=\$this->sumofdivisors + \$quotient;
\$counter++;
}}

// extract primes
\$bar=1;
for(\$m = 2; \$m <= \$counter-1; \$m++) { if (DestructNumber::primep(\$this->divisorlist[\$m]))
{\$this->primedivisorlist[\$bar][1]=\$this->divisorlist[\$m];
\$bar++;}
}
//find powers of primes
for (\$m=1; \$m <= \$bar-1; \$m++) { \$power=1; \$x = round(pow(\$this->primedivisorlist[\$m][1], \$power));

while (\$this->number % \$x == 0
&& \$x <= \$this->number)
{
\$power++;
\$x = round(pow(\$this->primedivisorlist[\$m][1], \$power));
}
\$this->primedivisorlist[\$m][2] = \$power-1;
}
// create prime divisor string
\$this->primedivisorstring =””;
for (\$m = 1; \$m <= \$bar-1;\$m++) { \$this->primedivisorstring .= \$this->primedivisorlist[\$m][1];
if (\$this->primedivisorlist[\$m][2]>1 )
{ \$this->primedivisorstring .= ““.\$this->primedivisorlist[\$m][2].”“;
}
if (\$m < \$bar-1) {\$this->primedivisorstring .= “*”;}
\$this->phi = \$this->phi * round(pow(\$this->primedivisorlist[\$m][1], (\$this->primedivisorlist[\$m][2]-1)))*(\$this->primedivisorlist[\$m][1]-1);
}
}

All of the work has been done by my constructor. Now, I just need some methods to return the values.

// define methods
private function primep (\$N){
\$stop = floor(sqrt (\$N));
for ( \$m=2; \$m <= \$stop; \$m++) { if (\$N%\$m ==0) return 0; } return 1; } public function get_N(){ return \$this->number;
}
public function get_sigma(){
return \$this->sumofdivisors-\$this->number;
}
public function get_numberdivisors(){
return count(\$this->divisorlist);
}
public function get_divisorstring(){
return \$this->primedivisorstring;
}
public function get_phi(){
return \$this->phi;
}
public function get_prime(){
return (count(\$this->divisorlist) == 2 ) ? "Yes" : "No";
}
public function get_perfect(){
return (\$this->sumofdivisors == \$this->number) ? "Yes" : "No";
}
public function get_square(){
return ( count(\$this->divisorlist)%2 == 1) ? "Yes" : "No";
}

}

There are some fun things here. A number is prime if it has two divisors. A number is square if it has an odd number of divisors.
Here is the link for my web factorizer.

Next, I will be posting a database mining application to explore number properties.