Palindromes In PHP

Richard Wiseman is a psychologist, magician, and author who runs a little blog over at http://richardwiseman.wordpress.com/. His blog talks about all sorts of things, but every Friday he posts a little puzzle that you can have a go at solving.

The last puzzle posted talked about palindromic numbers and speed, here is the puzzle in full.

The other day I went for a bike ride. My favourite route has signs every meter saying how far you have travelled. I came across the sign saying '15951 meters' and thought 'Oh, that's interesting, it is a number palindrome because it reads the same from left to right as right to left'. Then I rode on. Two hours later I came across the next palindromic number sign. How fast was I going?

I usually like to sit down for a few minutes and go over these puzzles in my head, especially if they are number based. However, this weeks puzzle got me thinking in terms of an algorithm that could work out the answer. All it would need is a counter for the start and finish, a simple palindromic detector, and a bit of maths at the end to work out the speed based on the start and end values.

$start = 15951;
$end = 0;

for ($i = $start + 1; $i < 20000; ++$i) {
    if ($i == strrev($i)) {
        print "Palindrome = " . $i . "\n";
        $end = $i;
        break;
    }
}
$result = $end - $start;
// Work out speed.
print  $result . ' meters, or ' . $result / 1000 . 'km, travelled in 2 hours makes the speed ' . (($result) / 2) / 1000 . ' kph';

This prints out the following result.

Palindrome = 16061
110 meters, or 0.11km, travelled in 2 hours makes the speed 0.055 kph

The answer to the Friday puzzle can be seen on Richard Wiseman's blog post from Monday. The answer to this puzzle was 55 meters per hour, which fits in with the answer this code created, although I worked it out in kilometres per hour.

Finally, if you are looking at string instead of a number you might need to improve the palindromic detector slightly by removing all punctuation and spaces.

$string = preg_replace( '/[^\sa-zA-Z0-9]/', '', $string);
if ($string == strrev($string)) {
    echo 'Panindrome detected.';
}

It might also be worth using the strtolower() function to make sure that the string has no characters that might cause this palindrome check to fail.

Share:

  • Add news feed
  • Bookmark this on Delicious

Comments

Using strrev() is not the

Using strrev() is not the proper way to do plaindrome. please provide a nice one to othres........

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <h4> <h5> <h6> <pre> <span> <p> <br />
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.