I was doing one Euler project problem, but stuck...
Okay, as the title says, I'm stuck. I got a program and from what I know, it is supposed to work, but fact is... it doesn't. I don't know what might be wrong it it, for is seems good to me.
The question is to find the largest palindrome number (one which can be read in either directions, e.g.. 323, 4224, 510015, etc) which is formed by the product of two 3-digit numbers. Here's my coding:
Code:
$L = 1000;
for ($A = 100 ; $A < $L ; $A++)
{
for ($B = 100 ; $B < $L ; $B++)
{
$P = $A*$B;
if
(
substr($P, 0, 1) == substr($P, (length($P)-1), (length($P))) &&
substr($P, 1, 2) == substr($P, (length($P)-2), (length($P)-1)) &&
substr($P, 2, 3) == substr($P, (length($P)-3), (length($P)-2)) &&
length($P) == 6
)
{
print $A, " x " , $B, " = ", $P, "\n";
push @array, $P;
}
}
}
$max = $array[0];
foreach $i (@array[1..$#array])
{
if ($i > $max)
{
$max = $i;
}
}
print "The largest palindrome is " , $max , "\n";
Or through pastebin: http://pastebin.com/iyGAJVKx
I inserted the line:
print $A, " x " , $B, " = ", $P, "\n";
To check all the incoming numbers as an attempt to find the problem. When I put only the first condition, that is:
substr($P, 0, 1) == substr($P, (length($P)-1), (length($P)))
I get palindromes and other non-palindromic numbers, of course, and the answer is in the lot of numbers, that is 906609.
However, with the full conditions, it doesn't give me the required palindrome. For a certain reason, all it gives me are numbers with all the same digits and the largest turns out to be 888888.
Any help in understanding what's wrong? :)
Thanks!