Jump to content

Handling Big numbers


Abhyro

Recommended Posts

Hi ..am new to PHP and am using it to solve some mathematical problems which require extensive computation.

 

Following is a code to solve a problem:

<?php

$num =1;
$n =0;
$temp = 0;
$count =0;
$Final =0;

While( $num < [color=orange]1000000)[/color]
{
   $n = $num;
       while($n >1)
         {

            if(( $n % 2) == 0)
              {
                $n = $n/2;
                $temp++;
              }
             
             else
              {
                $n =3*$n +1;
                $temp++;
              }
           }

     If($temp > $count)
          {
            $count =$temp;
            $Final =$num;
          }


      $num++;
      $temp =0;
}

  echo $Final;
  echo "<BR>";
  echo $count;

?> 

   

I am running into "Maximum execution time of 30 seconds exceeded" error...its happening coz of the number 1 million...the code wrks fine for 100,1000,10000 and 100000...but breaks down @ 1000000.....is there any way to solve this situation...or PHP is not capable of handling huge numbers?

 

please help...am stuck with this even though am so close to solving the prob.

 

Thanks

Abhijeet

 

Link to comment
https://forums.phpfreaks.com/topic/237107-handling-big-numbers/
Share on other sites

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.