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.

Comments

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

 

<?php

$a  = "414";

$check = strlen($a);

 

for($j=0; $j<$check; $j++)

{

$fstr = $a[$j];

$sec =  $check - $j-1;

$lstr = $a[$check - $j-1];

if($fstr == $lstr)

{

$flag = "ok";

continue;

}

else

{

$flag = "No";

break; /* No need of further operation */

}

}

echo "<br>";

if($flag == 'ok')

{

echo $a." == this is palindrome number";

}

else

{

echo $a."this is not a palindrome number";

}

?>

 

Permalink

 

<?php

$str="1574751";

$ln=strlen($str);

for($i=$ln-1;$i>=0; $i--)

{

$rstr[]=$str[$i];

}

$rstr=implode($rstr);

if($str==$rstr){

echo $str." is palindrome.<br>";

}

else{

echo $str." is not a palindrome.";

}

?>

 

Permalink
this example is good to understand
Permalink
will you know how to find plaindrom in a string part, or a complete string? example: God saw I was a dog. Too bad - I hid a boot.
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.