Jump to content

why do certain numbers crash this program immediatly?


Jskid

Recommended Posts

Along time ago I wrote this program that prime factors a number. Under EasyPHP 1.8 certain numbers crash the program. The program crashes even if the first thing it does is print out a line it won't do this. An example number is 15999. This was one of the first programs I wrote and my style was very poor, this is a prime example of spaghetti code.

<?php
print("started");
$num = 15999;
$startnum = 1;
if($num == 1){
print("1 has no factors");
exit;
}
if($num == 0){
print("0 has no factors");
exit;
}else{
print("The factors of ".$num." are:<br />");
}
primefind($num, $startnum);
if($num == 0){
print("0");
}
else if($num == 1){
print("1");
}else{
}
function primefind($num, $startnum){
$primestat = 'f';
for($counter1 = $startnum; $counter1<=$num; $counter1++){
	for($counter2 = 2; $counter2<$counter1; $counter2++){
		$primecheck = $counter1%$counter2;
		if($primecheck != 0){
			$primestat = 't';
		}else{
			$primestat = 'f';
			break;
		}
	}
	if($primestat == 't'||$counter1 == 2){
		factorcheck($counter1, $num);
		break;
	}
}
}
function factorcheck($prime, $num){
$remainder = $num%$prime;
if($remainder == 0)
{
	print($prime.'<br />');
	$startnum = 1;
	primefind(($num/$prime), $startnum);
	//exit;
	return $prime;
}
else{
	$prime++;
	primefind($num, $prime);
}
}
?>

 

Does anyone know why certain numbers crash this program? How does PHP work, how is it possible for a script to crash immediately without printing the first line?

Link to comment
Share on other sites

I know nothing about this EasyPHP thing. I;ve just run it on PHP5.3 in command line. And I got some initial output, before it crashes

 

Actual output was

startedThe factors of 15999 are:<br />3<br />PHP Fatal error:  Maximum function
nesting level of '100' reached, aborting! in C:\wamp\www\t1.php on line 223
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\t1.php:0
PHP   2. primefind() C:\wamp\www\t1.php:31
PHP   3. factorcheck() C:\wamp\www\t1.php:141
PHP   4. primefind() C:\wamp\www\t1.php:223
PHP   5. factorcheck() C:\wamp\www\t1.php:141
PHP   6. primefind() C:\wamp\www\t1.php:191
.... (continues until 99)

Link to comment
Share on other sites

The reason that you're not getting any error output might have something to do with the values you have for display_errors and error_reporting in your php.ini.

 

BTW, to get more / better / nicer debug output, you might want to install Xdebug. As an added benefit, it protects against infinite recursion.

Link to comment
Share on other sites

Just to make sure there's no misunderstanding: the first line prints "started" and then it goes into the convoluted process of prime factoring. With the number 15999 the program really quickly crashes - doesn't even print out "started". Does this mean that the preprocessed was trying to do an optimization and got screwed up? I mean is no one else surprised that the first line of code which defiantly isn't problematic is not being executed? :shrug:

Link to comment
Share on other sites

It has to do with the fact that primefind calls factorcheck and factorcheck calls primefind.  You can implement this with like 4 lines of code utilizing two loops in java, and I bet you could port it to php without too much trouble:

 

public class PrimeFactors {

public static List<Integer> primeFactors(int number) {

int n = number;

List<Integer> factors = new ArrayList<Integer>();

for (int i = 2; i <= n; i++) {

while (n % i == 0) {

factors.add(i);

n /= i;

}

}

return factors;

}

 

Surprised no one has this in php posted on the web right now.

Link to comment
Share on other sites

I mean is no one else surprised that the first line of code which defiantly isn't problematic is not being executed? :shrug:

You might have output buffering enabled, so when the script crashes, nothing is send to browser.

Link to comment
Share on other sites

Surprised no one has this in php posted on the web right now.

 

I'm not sure which web you're referring to, but there are good and bad examples of this, in PHP, posted all over the internet; it's a very common basic task to have a stab at.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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