I’m a PHP dev and I was wondering if someone could help me converting this code into a Perl script.
I wrote this PHP code today:
<?php
for ($i = 1; $i > $i++; $i++) {
$cut = strlen($i)/2;
$a = substr($i,0,$cut);
$b = strrev($a);
$som = $a + $b;
if((strrev($a) == $b) && (strrev($som) == $som) && ($a > 10000)) {
echo $a . " + " . $b . " = " . $som . "\n";
}
}
?>
What it does:
If you keep this formula in mind: $a + $b = $c it basically
- Cuts $i in half, reverses the 2st part and stores into $b, and stores the first part unreversed into $a.
- Adds $a and $b into $c, and checks if $c equals $c in reverse.
-
Outputs the ‘palindromes’ , looking like this:
1248 + 8421 = 9669
I’d be very helpful if anyone could help me out 🙂
Thanks in avance.
Considering Zaids advise and little thinking, some small changes:
When using
use warningsI get a warning for the addition, have to look at this.