Howdy All,
There yet another Cartalk Puzzler that begs for a programmatic solution for y'all to dig your teeth into.
It shouldn't take very much code to do this, so if you would post your code in [ignore]
[/ignore] tags, we'll at least get to see the differences in our coding styles.
As an additional problem, you could try golfing it, but that goes against my own sensibilities
Enjoy,
- Miller
There yet another Cartalk Puzzler that begs for a programmatic solution for y'all to dig your teeth into.
It shouldn't take very much code to do this, so if you would post your code in [ignore]
Code:
[COLOR=WHITE WHITE][/COLOR]
As an additional problem, you could try golfing it, but that goes against my own sensibilities
Enjoy,
- Miller
Code:
# Highlight text to reveal my solution:
[COLOR=white white]
use strict;
# Palindrome Rules
# X: substr(S, -4)
# X+1: substr(S, -5)
# X+2: substr(S, -5, 4)
# X+3: S
for my $i (1..999999) {
if (length($i+3) == 6
&& is_palidrome(substr($i, -4))
&& is_palidrome(substr($i+1, -5))
&& is_palidrome(substr($i+2, -5, 1))
&& is_palidrome($i+3)
) {
print $i, "\n";
}
}
sub is_palidrome {
my ($num) = @_;
my $len = 1 + int(length($num) / 2);
my $x = substr($num, 0, $len);
my $y = reverse substr($num, -$len);
return $x eq $y;
}[/COLOR]