Jump to content

Large for loop not working for math calculation


mmubarak

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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 by Jessica
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.