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);

Drupal – Add all users to simplenews newsletter

The script below will add all users to your simplenews newsletter and subscribe them to the top most newsletter (if you have more that one newsletter, you can just loop through it)

This has only been tested in Drupal 5.x.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
   
    $user = "DB_USERNAME";
    $pass = "DB_PASSWORD";
    $db = "DB_NAME";

    $dbh = new PDO('mysql:host=localhost;dbname='.$db, $user, $pass);
   
   
        $sth = $dbh->prepare("
            SELECT users.name AS subscribed_mail, users.mail AS registered_users, users.uid, simplenews_subscriptions.snid, users.created
            FROM users
            LEFT JOIN simplenews_subscriptions ON users.mail = simplenews_subscriptions.mail
            UNION ALL
            SELECT users.name, simplenews_subscriptions.mail, users.uid, simplenews_subscriptions.snid, users.created
            FROM users
            RIGHT JOIN simplenews_subscriptions ON users.mail = simplenews_subscriptions.mail
        "
);
        $sth->execute();
        $row =$sth->fetchAll(PDO::FETCH_ASSOC);
       
        ?><table border="1">
            <tr>
                <th>Ref #</td>
                <th>UID</th>
                <th>Username</th>
                <th>Mail</th>
                <th>SNID</th>
                <th>Created (GMT)</th>
            </tr>
        <?php
            $notAdded = 0;
            $totalUsers = 0;
            foreach($row as $key=>$var){
                   
                    if($totalUsers % 2 == 0){
                        ?><tr style="background-color:#DDD"><?php
                    }else{
                        ?><tr><?php
                    }
                    ?>
                   
                        <td><?php echo $totalUsers+1; ?></td>
                        <td><?=$var['uid'];?></td>
                        <td><?=$var['subscribed_mail'];?></td>
                        <td><?=$var['registered_users'];?></td>
                        <td><?=$var['snid'];?></td>
                        <td><?php
                            if(!empty($var['created'])){
                                echo date("Y-m-d H:i:s", $var['created']);
                            };
                        ?></td>
                    </tr>
                    <?php
                   
                    $totalUsers++;
               
                //IF SNID IS EMPTY, IT ISN'T ADDED
                if(empty($var['snid']) && !empty($var['uid']) && !empty($var['registered_users'])){
                    $notAdded++;
                   
                    //ADD IT INTO SUBSCRIPTIONS
                    $sth = $dbh->prepare("INSERT INTO simplenews_subscriptions (mail) VALUES (?)");
                    $sth->execute(array($var['registered_users']));
                   
                   
                };
               
               
            }
        ?>
        </table><?php
       
        echo 'Number that needs to be added : '.$notAdded.'<br/>';
        echo 'Total users : '.$totalUsers.'<br/>';
       
        if($notAdded > 0){
            echo 'Added missing rows<br/>';
            $sth = $dbh->prepare("SELECT snid FROM simplenews_subscriptions");
            $sth->execute();
            $row = $sth->fetchAll(PDO::FETCH_ASSOC);
            //print_r($row);
            $news = $dbh->prepare("SELECT tid FROM simplenews_snid_tid GROUP BY tid");
            $news->execute();
            $news = $news->fetchAll(PDO::FETCH_ASSOC);
           
            if(!empty($news)){
                foreach($row as $key=>$var){
                    $sth = $dbh->prepare("INSERT INTO simplenews_snid_tid (snid,tid) VALUES (?,?)");
                    $sth->execute(array($var['snid'],$news[0]['tid']));
                }
            }
        }
   
?>

HTML5 Canvas Bubble Demo V2

The HTML Canvas Bubble Demo got a lot of attention and I received a lot of hits from that. Since last year, I used it on a WP theme and updated it to make it more functional.

So here is a link to the NEW demo.


Here is a link to download all the files, including the demo

When using it on your own site, create a DOM element with the ID “canvasBubbles”, include the “bubbles.js” script :

Basic Usage

1
2
3
<script>
   bubblesMain();
</script>

Custom Options (Example)

1
2
3
4
5
6
7
8
9
10
11
12
<script>
    bubblesMain(new Object({
        type : 'radial',
        revolve : 'center',
        minSpeed : 100,
        maxSpeed : 500,
        minSize : 50,
        maxSize : 150,
        num : 100,
        colors : new Array('#FF0000','#FFFFFF','#BBBBBB')
    }));
</script>
  • type (linear,radial)
  • revolve (topLeft, topRight, bottomLeft, bottomRight) This is used when “type” is radial
  • opacity (any floating number between 0 and 1)
  • minSpeed (any number)
  • maxSpeed (any number)
  • minSize (any number)
  • maxSize (any number)
  • num (number of bubbles)
  • colors (an array of 6 digit HEX colors)

Comparing strings with DOM elements

This problem confused me for a bit and may be of some help to someone in the future.  What I was trying to do was compare a javascript string with an existing DOM element (Don’t ask why).

Anyways, normal text is no problem, the problem is that when HTML tags are entered into the DOM it is modified to work properly. Inserting a table or input element gets modified in its own way.

  • Tables get tbody tags if none exist
  • input buttons get organized by value,onclick,type (only ones tested)

Therefore matching the HTML string with the DOM after it’s changed will tell you they aren’t equal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function compare2Strings(str,str2){
    alert('string 1 : '+str+', string 2 : '+str2);

    if(str.length != str2.length){
        alert('different lengths :'+str.length+'/'+str2.length);
    }
   
    for(i=0;i<str.length;i++){
            if(str.length < i || str2.length < i){
                return false;
            }

        if(str.charAt(i) != str2.charAt(i)){
            alert('character at position '+i+' is different :'+str.charAt(i)+'/'+str2.charAt(i));
        }
    }
}

This function is pretty crude, but showed me what was wrong. Hopefully if this is what you are looking for, it helps you with your project.

Happy Coding!

HTML5 Canvas Transform

I was messing around with HTML5 Canvas transform and was having trouble getting the right values that I wanted.

I decided to build a page that allowed me to test how to manipulate images on the fly. It allows linking to multiple images and transforming it how you want.
Ensure that there is an image source and that there is a value in each box for the image to appear.

Check it out, and if you have any questions or suggestions please feel free to comment.

HTML5 Canvas transform