mmubarak Posted January 9, 2013 Share Posted January 9, 2013 Hi I am trying to create a large for loop to do a math calculation but I get the following error: HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request. I don't get the same error when I am working with smaller numbers. Is there a way that I can do calculations with large for loops? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/ Share on other sites More sharing options...
haku Posted January 9, 2013 Share Posted January 9, 2013 Maybe. Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1404364 Share on other sites More sharing options...
Barand Posted January 9, 2013 Share Posted January 9, 2013 There is a BC Math library for handling large numbers Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1404399 Share on other sites More sharing options...
mmubarak Posted January 10, 2013 Author Share Posted January 10, 2013 Thank you for replying. I am actually trying to find the least common multiple of all the numbers between 1 and 20. I currently have a for loop up to a very large number (with the hope that the solution lies within that range). But I am unable to do it. But when I try to find the least common multiple for all the numbers between 1 and 10, I have no problem. Any suggestions would be greatly appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1404529 Share on other sites More sharing options...
Jessica Posted January 10, 2013 Share Posted January 10, 2013 Post your code, turn on error reporting, blah blah blah Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1404530 Share on other sites More sharing options...
mmubarak Posted January 10, 2013 Author Share Posted January 10, 2013 <?php for ($i = 10; $i < 5000; $i++) { $count = 0; for ($j = 1; $j <= 10; $j++) { if ($i % $j == 0) { $count++; } } if ($count == 10) { echo $i; } } ?> That's my code and it gives me a right answer for all the numbers between 1 and 10. But when I change it to all the numbers between 1 and 20, I have a problem. Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1404531 Share on other sites More sharing options...
Jessica Posted January 10, 2013 Share Posted January 10, 2013 Can you explain again what you're trying to do? This code doesn't look like anything to do with LCM. http://en.wikipedia.org/wiki/Least_common_multiple Or if it does you're going about it in a weird way. Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1404538 Share on other sites More sharing options...
Jessica Posted January 10, 2013 Share Posted January 10, 2013 (edited) Just for fun I wrote this function. I'm not going to claim it is the best way, it just made sense to me. It worked for 1-10 no problem and 1-20 took a while, but eventually finished. 1-18 was fast enough, after that it takes a while, I had to put my max exec time up to 1 min. Don't try to do 1-10 and 1-20 on the same page though. If it doesn't do what you want, please explain what you're trying to do. And why. Original Idea: <?php set_time_limit(60); function findLCM($nums, $debug=true){ //First, order them largest to smallest. rsort($nums); $count_nums = count($nums); if($count_nums > 1){ //The greatest possible solution is the product of all numbers. $max = 1; foreach($nums AS $n){ $max *= $n; } if($debug) echo "Max is: $max"; //Start out assuming we haven't found it. $lcm_found = FALSE; if($debug) $tries = 0; //The first possible is the max number in the list. $possible_lcm = $nums[0]; while(!$lcm_found && $possible_lcm < $max){ if($debug) $tries++; //Check if it's the LCM. Since we care if any one isn't it, first assume we found it $lcm = true; if($debug) echo "<br><br><b>Try #{$tries}<br> Possible LCM: {$possible_lcm}</b><br>"; //Check each number - checking the max number is redundant but oh well. foreach($nums AS $n){ if($debug) echo "Checking $possible_lcm % $n ... ".($possible_lcm % $n).' ... '; if($possible_lcm % $n != 0){ //And we didn't find it, so break out. $lcm = false; if($debug) echo 'Not LCM</br>'; break; }else{ if($debug) echo '<br>'; } } if($lcm == true){ $lcm_found = true; }else{ $possible_lcm += $nums[0]; } } return $possible_lcm; } } $a=1; $b=20; $nums = range($a,$B); $lcm = findLCM($nums, true); echo "LCM of ".implode($nums, ', ').": $lcm<br>"; I had a better idea, wrote out why it was better, and then erased it by accident. working on it now. Edited January 10, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1404542 Share on other sites More sharing options...
Jessica Posted January 10, 2013 Share Posted January 10, 2013 I added this code to it: function getDivisors($nums){ rsort($nums); $use_these_nums = array(); foreach($nums AS $num){ $goes_into_another = FALSE; foreach($nums AS $bigger){ if($bigger%$num==0 && $num != $bigger){ $goes_into_another = TRUE; } } if(!$goes_into_another){ $use_these_nums[] = $num; } } return $use_these_nums; } and changed the other function to: function findLCM($nums, $debug=true){ //First, order them largest to smallest. rsort($nums); $count_nums = count($nums); if($count_nums > 1){ //Then remove any numbers that go into other numbers. $nums = getDivisors($nums); .... You don't need to check if the possible LCM is divisible by any number that goes into another number you're checking. Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1404544 Share on other sites More sharing options...
Jessica Posted January 10, 2013 Share Posted January 10, 2013 LCM of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10: 2520 LCM of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15: 360360 LCM of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20: 232792560 Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1404546 Share on other sites More sharing options...
mmubarak Posted January 10, 2013 Author Share Posted January 10, 2013 Thank you very much. I am a newbie so it will take me a while to understand what you wrote up there. Will get back to you. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1404560 Share on other sites More sharing options...
mmubarak Posted January 12, 2013 Author Share Posted January 12, 2013 I see that you used the variable $debug in many instances throughout the code. Can you please explain to me why you used it and whether this is a standard among programmers. Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1405095 Share on other sites More sharing options...
Christian F. Posted January 12, 2013 Share Posted January 12, 2013 Yes, and no. For example code, at least over a certain complexity level, some form of extra debugging functionality is usually built in (or rather, should be, in my opinion). Exactly how this is done isn't really standardised, but usually this involves a variable or (more commonly) a constant named $debug or DEBUG respectively. The DEBUG constant is very often used in production code too, to enable extended debugging when in development. In those cases, however, the actual debug reporting code is usually handled by a separate function and/or class. So as not to clutter the business logic itself. Is it a standard? Not sure, but it should most definitely be. Quote Link to comment https://forums.phpfreaks.com/topic/272881-large-for-loop-not-working-for-math-calculation/#findComment-1405144 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.