www.webdeveloper.com
Results 1 to 11 of 11

Thread: interger problem

  1. #1
    Join Date
    Dec 2009
    Location
    City of Smiles
    Posts
    172

    interger problem

    Hi all.

    I am trying to solve a problem from project euler, for the purpose of learning I'm not asking here for the answer or logic, I got the latter but it seems that I have a problem in outputting the integer since it exceeds the limit of variable limit in PHP (as I know).

    PHP Code:
    <?php

    $a 
    0;
    $b 1;
    $c 0;
    $even_count 0;

    for( 
    $i $i 4000000 $i++ ) {
        
    $c $a $b;
        
    $a $b;
        
    $b $c;
        if( 
    $c == ) {
            
    $even_count += $c;
        }
    }

    echo 
    "Sum of all even-valued terms: " $even_count;

    ?>
    The code is all about the sum of even numbers in the fibbonaci sequence which do not exceed four million.

    Whenver I run the program the output as expected is
    Code:
    Sum of all even-valued terms: INF
    I can't figure out how to output such big number. Any help? Thanks.
    Can't stand long hours coding in front of computer without being intoxicated.

  2. #2
    Join Date
    Sep 2010
    Posts
    160
    What is the result if you call var_dump() on your $even_count variable?

    My simple guess is that you have indeed exceeded the limit on your platform for handling numerical values and that if you have a 32BIT operating system you are out of luck. Perhaps with a 64BIT operating system it would be fine.

    I checked the PHP manual but I cannot find any object/function that would allow you to store the numerical value as something else and work with it to get around your problem.

    Archie

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,245
    You could use the BCMath functions, or the PEAR Math_BigInteger package.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Sep 2010
    Posts
    160
    Quote Originally Posted by NogDog View Post
    You could use the BCMath functions, or the PEAR Math_BigInteger package.
    Yeah, what NogDog said.

    Great that you found them, I knew they should exist but did not know what they might be called. Good to know for future reference.

    Archie

  5. #5
    Join Date
    Dec 2009
    Location
    City of Smiles
    Posts
    172
    Quote Originally Posted by zimonyi View Post
    What is the result if you call var_dump() on your $even_count variable?

    My simple guess is that you have indeed exceeded the limit on your platform for handling numerical values and that if you have a 32BIT operating system you are out of luck. Perhaps with a 64BIT operating system it would be fine.

    Thanks for your reply, I checked the PHP manual but I cannot find any object/function that would allow you to store the numerical value as something else and work with it to get around your problem.

    Archie
    Whenever I call var_dump() on $even_count variable it outputs float(INF). Yeah you're right, I'm using 32-bit operating system.

    Quote Originally Posted by NogDog View Post
    You could use the BCMath functions, or the PEAR Math_BigInteger package.
    Thanks once more for your reply NogDog, I checked the link you gave me but I can't seem to have an idea how to start using it. Can you give me some samples or simple approach regarding this? Thanks so much.
    Can't stand long hours coding in front of computer without being intoxicated.

  6. #6
    Join Date
    Sep 2010
    Posts
    160
    Quote Originally Posted by kingdm View Post
    Whenever I call var_dump() on $even_count variable it outputs float(INF). Yeah you're right, I'm using 32-bit operating system.



    Thanks once more for your reply NogDog, I checked the link you gave me but I can't seem to have an idea how to start using it. Can you give me some samples or simple approach regarding this? Thanks so much.
    As long as you have the package installed (see the Installation section on the link NogDog gave) you just need to exchange your

    Code:
    $even_count += $c;
    with

    Code:
    $even_count =  bcadd($even_count, $c);
    Archie

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,245
    PHP Code:
    $a 0;
    $b 1;
    $c 0;
    $even_count 0;
    for (
    $i 0$i 4000000$i++) {
       
    $c bcadd($a$b);
       
    $a $b;
       
    $b $c;
       if (
    bcmod($c2) == 0) {
          
    $even_count bcadd($even_count$c);
       }
    }
    echo 
    "Sum of all even-valued terms: " $even_count
    And be prepared to wait awhile, and probably have to use set_time_limit() to allow it to run long enough. (120 seconds was not enough on my PC.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Dec 2009
    Location
    City of Smiles
    Posts
    172
    Thanks Archie and NogDog for your reply.

    Upon reading the BCMath and your replies I finally get the idea how does this things work, however I can't make it run due to some execution time limit.

    @NogDog

    I wonder where will I set the execution time to greater value, I had looked into the php.ini but the only BCMath related thing I could get is in here

    Code:
    [bcmath]
    ; Number of decimal digits for all bcmath functions.
    ; http://php.net/bcmath.scale
    bcmath.scale = 0
    Sorry for being quite redundant in asking, its just that its not my usual hobby to manipulate the php.ini configurations
    Can't stand long hours coding in front of computer without being intoxicated.

  9. #9
    Join Date
    Dec 2009
    Location
    City of Smiles
    Posts
    172
    Sorry for my earlier post, after doing some research and trials I found out how to use the set_time_limit(). I added set_time_limit(1000) on top of my php code. But still I got the fatal error for maximum execution time. What is the maximum seconds I could set? Is the problem lies in my 1gb ram laptop?
    Can't stand long hours coding in front of computer without being intoxicated.

  10. #10
    Join Date
    Sep 2010
    Posts
    160
    Quote Originally Posted by kingdm View Post
    Sorry for my earlier post, after doing some research and trials I found out how to use the set_time_limit(). I added set_time_limit(1000) on top of my php code. But still I got the fatal error for maximum execution time. What is the maximum seconds I could set? Is the problem lies in my 1gb ram laptop?
    See set_time_limit() for more details.

    Archie

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,245
    You can set it to zero (0), which means no time limit. However, before doing that, try setting the maximum for $i in the loop condition to a much smaller number to test your code before letting it loose for 4 million iterations so you can find out if the code is reasonable. If it takes 1/1000th of a second per iteration (purely hypothetically, maybe more, maybe less), you'll need at least 4000 seconds -- a bit over an hour -- for it to run.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles