Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Created June 18, 2010 20:32
Show Gist options
  • Save j1n3l0/444186 to your computer and use it in GitHub Desktop.
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]
#!/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