Created
June 18, 2010 20:32
-
-
Save j1n3l0/444186 to your computer and use it in GitHub Desktop.
Find the largest palindrome made from the product of two 3-digit numbers [euler,perl]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
=pod | |
Find the largest palindrome made from the product of two 3-digit numbers. | |
=cut | |
use strict; | |
use warnings FATAL => 'all'; | |
my @numbers = reverse 100 .. 999; | |
my $answer = eval { | |
my ( $max, $val ) = ( 0, '' ); | |
while ( my $a = shift @numbers ) { | |
for my $b (@numbers) { | |
my $c = $a * $b; | |
if ( ( $max < $c ) && ( $c == reverse $c ) ) { | |
( $max, $val ) = ( $c, "{ $a * $b = $c }" ); | |
} | |
} | |
} | |
return $val; | |
}; | |
print "$answer\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment