Prime Factorizing For Big Evil Number '$bignumber'"; for ($i = 2; $i < $bignumber-1; $i++) { //print "
$i"; if (isprime($i)) { // if $i is a prime //print " Prime "; if (isprimefactor($bignumber, $i)) { // $i is a prime, now test if it's a primefactor //print " Factor!"; $foundfactors[] = $i; if (testfactor($bignumber, $foundfactors)) { print "
Breaking because testfactor() is happy."; break; } } } } print "
All done, prime factors collected are:
";
print_r($foundfactors);
print "

Largest Factor: " . end($foundfactors); print "
Total Memory Used: " . memory_get_peak_usage(TRUE)/1024 . " Kbytes"; // ======================================================= // everything in math can be undone, and factors can be divided backwards // so lets test, so we can exit the factorization loop midstream function testfactor($bigevilnumber, $factors) { // set point to last item in array, and do bignum / last factor end($factors); $y = $bigevilnumber / current($factors); while(prev($factors)) { $z = $y / current($factors); $y = $z; } reset($factors); if ($y==1) return TRUE; else return FALSE; } function isprime($num) { if ($num==1) return false; // 1 is not a prime if ($num<4) return true; // 2 and 3 are primes if (($num%2)==0) return false; // exclude evens, all primes are odd if ($num<9) return true; // 4,6,8 already excluded if (($num%3)==0) return false; // exclude remaining multiples of 3 $primefound = FALSE; // test num from 2 up to sqrt(num) for ($m=2; $m<=sqrt($num); $m++) { if (($num % $m)==0) { // composite, quit now return false; } else { $primefound = TRUE; } } return ($primefound); } function isprimefactor($bignumber, $primenumber) { if (($bignumber % $primenumber)==0) { return TRUE; } return FALSE; } ?>