Jump to content

PHP Factoring (Composite numbers to Prime)


Recommended Posts

Alright....so I was bored, and was looking through some student books I had, and decided to implement anything I could that had to do with math...this is actually for a math php class i'm making, but converted it to function for you guys to test out. I know it's probably not the best way to do it, but it took me about an hour 30 minutes to create, and it is by far one of the most complicated projects I have done, and i'm proud to have finished it(after pulling out a few chunks of hair of course). I wasn't able to upload it to my website at the moment(tired as hell) but feel free to test the code on your own!

 

<?php
//using preg_match to see if the number is prime
function is_prime($number)
{
return !preg_match('/^1?$|^(11+?)\1+$/x', str_repeat('1', $number));
}

//lets factor some numbers!
function factor_number($number, $static_num) //we need a static variable "$static_num" to tell you the original number
{
static $factor = array(); //declare this static so the value won't change everytime the function is called
if(is_prime($number) === FALSE) //if the number is not prime
{
	for($i = 2; $i <= $number; $i++) //create our loop to find the first number it is divisible by
	{
		if($number % $i == 0) //if after division there are no remanders we found our first divisor
		{
			$factor[] = $i; //add the current $i number to our factor array so we can display the factored numbers later
			$number = $number / $i; //divide the number by the divisor

			if(is_prime($number)) //if the number is now prime we tack on the final number to make the multiplication correct
				$factor[] = $number;

			factor_number($number, $static_num); //if it's not prime, loop through until the number is prime
			break;
		}
	}
}
elseif(is_prime($number) === TRUE) //if the number is prime
{
	echo "Number has been factored.<br/>"; //tell them the number was sucesfully factored
	echo "Factor for {$static_num} is: ";
	$array_count = count($factor); //count the array values for display
	foreach($factor as $key=>$value)
	{
		if($key != $array_count - 1) //if the current key of the array is not the last one, display the value with a multiplication sign
		{
			echo $value . " X ";
		}
		else //else its the last digit and we don't need the sign
		{
			echo $value;
		}
	}
}
}

factor_number(9, 9);
/* the function above displays...
Number has been factored.
Factor for 9 is: 3 X 3
*/
?>

 

And I know the echo statements aren't the best choice for showing the results, but that's simple enough to change, just let me know what you guys think!

Link to comment
Share on other sites

Eh, just did some testing to see the highest number I could run..and I got up to 133,955,554 (that had to go through the process of factoring)...here are the results...

 

Number has been factored.

Factor for 133,955,554 is: 2 X 66,977,777

This page was created in 3.600429058075 seconds

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.