Jskid Posted April 22, 2010 Share Posted April 22, 2010 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 https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/ Share on other sites More sharing options...
teamatomic Posted April 22, 2010 Share Posted April 22, 2010 I use RapidPHP 2010 and it give this: startedThe factors of 15999 are: 3 5333 Must be some flaw in easyPHP HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/#findComment-1046616 Share on other sites More sharing options...
Mchl Posted April 22, 2010 Share Posted April 22, 2010 Too much recursion Maximum function nesting level of '100' reached, aborting! Link to comment https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/#findComment-1046618 Share on other sites More sharing options...
Jskid Posted April 22, 2010 Author Share Posted April 22, 2010 I only get this problem using EasyPHP 1.8(http://software.emule.com/easyphp-1-8/), older and newer versions work fine. What I really like to understand is how come the program crashes immediately and doesn't even do the very first line that is a print statement? Link to comment https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/#findComment-1046628 Share on other sites More sharing options...
Mchl Posted April 22, 2010 Share Posted April 22, 2010 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 https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/#findComment-1046629 Share on other sites More sharing options...
Jskid Posted April 22, 2010 Author Share Posted April 22, 2010 I thought this is how PHP works: it is red through once so that the functions have a definition, and then it starts at the <?php and executes one line of code at a time? Link to comment https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/#findComment-1046716 Share on other sites More sharing options...
Mchl Posted April 22, 2010 Share Posted April 22, 2010 A bit simplified, but yes. And then you call a function in function in function in funcion.... more than 100 times which seems to be max number allowed (don't know if it's configurable) Link to comment https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/#findComment-1046719 Share on other sites More sharing options...
dirkers Posted April 22, 2010 Share Posted April 22, 2010 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 https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/#findComment-1046746 Share on other sites More sharing options...
Jskid Posted April 23, 2010 Author Share Posted April 23, 2010 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? Link to comment https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/#findComment-1046758 Share on other sites More sharing options...
andrewgauger Posted April 23, 2010 Share Posted April 23, 2010 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 https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/#findComment-1046764 Share on other sites More sharing options...
Mchl Posted April 23, 2010 Share Posted April 23, 2010 I mean is no one else surprised that the first line of code which defiantly isn't problematic is not being executed? You might have output buffering enabled, so when the script crashes, nothing is send to browser. Link to comment https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/#findComment-1046894 Share on other sites More sharing options...
salathe Posted April 23, 2010 Share Posted April 23, 2010 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 https://forums.phpfreaks.com/topic/199419-why-do-certain-numbers-crash-this-program-immediatly/#findComment-1046965 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.