Bouncy Numbers PHP

Bouncy Numbers on projecteuler.net

Under 7 seconds with ASUS G53J (xampp) PHP.

How does it work?
We start at 0, and check each number for “bounciness” until we get an even 99% (brute force method).

Although the script could be shortened, I found this one to be the quickest PHP script yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
$num = 0;
$count = 0;
$percentage = 0;

//$num = 21780;
//$count = 19602;

$ts = microtime(1);

while($percentage != 0.99)
{
    $incline = $decline = false;
    $str = (string)$num;
    $len = strlen($num);
   
    for($i = 0; $i < $len; $i++)
    {
        if(isset($str[$i + 1])){

            if($str[$i] > $str[$i+1])
            {
                $incline = true;
                if($decline)
                    break;
            }
            else if($str[$i] < $str[$i+1])
            {
                $decline = true;
                if($incline)
                    break;
            }
           
        }
    }
   
    if($incline && $decline)
      $count++;
   
   $num++;  
   $percentage = $count / $num;
}

echo $num.' - '.(microtime(1) - $ts);

PHP garbage collecting

Recently I was working on a PHP object that kept going over the allowed memory limit. The easy way around that would be to bump PHP’s memory limit. Unfortunately I planned on running the script every 5 seconds. I find bumping PHP’s memory limit okay in certain circumstances but I decided that I was just going to do it properly.

PHP’s garbage collecting can be a little slow. Using unset() does unset the variable but it still exists in memory until garbage collecting has occurred (which may not be fast enough). This is no good for what I wanted. What is written below seems to work well. By assigning a null value to the variable, we clear any data off of it.

1
2
$var = null;
unset($var);

Basic, I know. But it works awesome!

PHP Thumb Generator 2011

First things first, I want to try and ensure every post on this blog is something interesting or creative.

This post is about a small script I threw together to create thumbnails of images.  Although this is not necessarily new or “creative”, I feel it deserves to be posted as it’s small and condensed.  Other scripts on Google are heavily outdated and a lot do not work.  The ones that do work contain workarounds that are not needed due to CSS3, or things you would never use (because we’re all Web 2.0 and all).

The script created does the following things

  • Can create thumbnail by width or height (it finds the correct dimension), or choose a custom width and height
  • Quality (1-100)
  • Output as a different format
  • Save the file as a new file (or overwrite)

It’s pretty basic, but it is essentially all you need.  I’ve uploaded it and it can be downloaded here.  Since WordPress is against files ending with certain extensions (namely zip files), remove the .txt extension from the file and it should work properly.

EDIT :

I’ve updated the script thanks to user pradip pointing out a problem. The script can be viewed here and downloaded here