-2

I have a code which works well up to 10 million i.e. 10,000,000 but stops working for higher value. Is there anyway to work with extremely large numbers in PHP

<?php
    $num_value_string;
    $counter=0;
    for($i=1;$i<=100000000;$i++)
    {
        $num_value_string=(string)$i;
        if(strstr($num_value_string,"17"))
            $counter++;
    }
    echo $counter . " numbers between 1 to " . --$i . " contains 17";
?>
  • There are much easier ways to solve this problem without spending hours to days in a for-loop. – p.s.w.g Aug 12 '14 at 14:05
  • I want to work on all values between 1 and 100,000,000....my code searches for 14 existing anywhere in the number – Ashish Bhatnagar Aug 12 '14 at 14:05
  • can you pls tell me any method to achieve this p.s.w.g – Ashish Bhatnagar Aug 12 '14 at 14:05
  • i want to count all the number between 1-100,000,000 containing 14 anywhere in them – Ashish Bhatnagar Aug 12 '14 at 14:07
  • 2
    Try to think about it mathematically rather than algorithmically. How many ways are there of arranging *n* digits such that a certain sequence of digits appears somewhere in the larger sequence? Once you think you've solved it mathematically you can use this simple method with smaller numbers to check your solution. – p.s.w.g Aug 12 '14 at 14:07
  • Check this question http://stackoverflow.com/questions/3740573/extremely-large-integers-in-php – webNeat Aug 12 '14 at 14:07
  • but simple php alone can handle numbers like 100,000,000 when I tried to execute $str=(string)100000000; echo $str it works perfectly.....or when i try to echo 100000000+1 ......it also works perfectly......then where is the problem – Ashish Bhatnagar Aug 12 '14 at 14:12
  • Are you executing this script in the browser and having timeout ? – webNeat Aug 12 '14 at 14:17
  • Yes I am executing this in a browser....but there is no information about timeout....how to check for a timeout happening? – Ashish Bhatnagar Aug 12 '14 at 14:18
  • I executed the loop for($i=1;$i<=100000000;$i++){} echo $i and it printed the value of $i perfectly. I also tried if(strstr("1200000000","12")) echo "found" else echo "not found" and this also worked. I also tried converting a large number to string like $str=(string)12000000000 echo $str and it printed the string perfectly......so when all the parts are working perfectly individually....why the code is not working when these small parts are combines???? – Ashish Bhatnagar Aug 12 '14 at 14:20
  • I guess it's timeout or memory limit issue. Check my answer below – webNeat Aug 12 '14 at 14:32
  • well thanks webNeat....no heavy BC or GMP needed....simple solution worked for me....thanks – Ashish Bhatnagar Aug 12 '14 at 14:41

2 Answers2

0

You need to use appropriate library for big number (e.g. BC Math), but usually this is used to compute calculation on numbers, not as variable in a (long) loop.

ocirocir
  • 3,543
  • 2
  • 24
  • 34
-2

Looking at your code, PHP can handle these numbers without extra things. So your issue is timeout or memory limit. Try Setting this at the start of your script

ini_set("memory_limit","256M"); // very large !
set_time_limit ( 3600 ); // one hour is more then enough !

By the way, your problem could be solved more simply then that !

webNeat
  • 2,768
  • 1
  • 20
  • 22
  • working perfectly.....u gave the ultimate solution....sometimes we don't need very deep programming tricks....simple things works perfectly. Thanks. – Ashish Bhatnagar Aug 12 '14 at 14:38
  • 1
    Except that this just hides the underlying problem with your code. It is not "the ultimate solution". – vascowhite Aug 12 '14 at 14:41
  • This was just to show you what is the problem. But your actual solution to the problem is not simple; Sure it's simple when writing it but it's very slow and use huge memory which is not good at all. If your script needs `5 minutes` to answer for `max = 10^8` it will need `500 minutes` for `max = 10^10` or even more. You should think about that. – webNeat Aug 12 '14 at 14:46
  • may be u r right..... but it worked for me and this is a code just to experiment...not a part of any big real world problem. – Ashish Bhatnagar Aug 12 '14 at 15:03