Jump to content

[SOLVED] FactorFinder Program Problem.


se1zure

Recommended Posts

I wrote a program where the user enters a number, and then it finds all the factors of that number, and displays it in a table.

 

It works great for the most part. Up to numbers like

99,999,999, but then problems arise. Adding another 9 to that (which then runs the loop 900 million more times) gives me this error:

 

Fatal error: Maximum execution time of 60 seconds exceeded in /home2/data/hosted/scetchbook/php/factor.php on line 10

 

Is there a way to turn off the execution time limit?

 

I though that, since theoretically, it doesn't need to test every number past half of the original (because 2 is the second lowest factor you won't always have), so it only needs to run the loop half of the times. I will do this, but is there a way to turn off the execution time limit.

 

Here is the code for the program:

 

<?php
// FactorFinder v1 

$num = $_POST["num"]; ;
$goto = 1;
echo "<p align='center'>Factors of $num:</br >";
echo "<table width='600' cellpadding='2' cellspacing='2' bgcolor='white'>";
echo "<tr><td width='200' align='center' bgcolor='#B0E2FF'><b> Factor </td><td bgcolor='#8DB6CD' width='200' align='center'><b><font color='white'> Multiplyer </td><td width='200' align='center' bgcolor='#B0E2FF'><b> Resultant </td></tr>";
$prime = 0;
while ($goto <= $num){
$fac = $num%$goto;
if ($fac==0){
        $mult = $num / $goto;
	echo "<tr><td width='200' align='center' bgcolor='#8DB6CD'> <font color='white'>$goto </td><td bgcolor='#B0E2FF' width='200' align='center'> $mult </td><td width='200' align='center' bgcolor='#8DB6CD'> <font color='white'>$num </td></tr>";
	$prime = $prime + 1; }
else {
	echo "";
	}
$goto = $goto + 1;
}
echo "</table>";
if ($prime==2){
echo "This is a prime Number. It is divisible by only 1 and itself.";}
else {
echo "This is not a prime number, there are a total of $prime factors.";}
?>

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/40634-solved-factorfinder-program-problem/
Share on other sites

If i am hosting this online, how woudl I go about doing this?

 

Thanks.

 

 

Oh, and the reason I'm interested is, I want to create a loops that I'll leave running for a few months on my other computer to find as high of a prime number as a can (and potentially break the current record). Of course, I will run this from that computer, and not the net... but not for now.

To be honest, I have no idea how to set the max executiong time in the php.ini file. I have just been coming and reading these forums so much that I have seen numerous topics such as this and it was solved that way, haha.

 

EDIT: Oops, I didn't realize this topic was solved, sorry for bumping it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.