pr33 has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks, Looking for any improvements within my code to find the largest palindrome number using product of any 3 dight numbers .There is another algorithm using recursion which I would be trying , But wanted to check for any improvisations using of the method of finding the product and then reversing the digits to check for the palindrome.
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; ######## sub three_digit_prodcuts { my @products = (); my $i = 999; my $counter = 999; while ($i >= 100 ) { my $product = $i * $counter; push @products, $product; $counter--; if ($counter >= 100) { redo; }else { $counter = $i - 1; $i--; next; } } return \@products; } my $pr = three_digit_prodcuts; my @arr = (); foreach my $n (@$pr) { my $rev = join '', reverse(split //, $n); if ($n == $rev) { push @arr, $n; next; } } my $largest = ( sort { $b <=> $a } @arr)[0]; print "$largest\n"; $ time ./largest_3_digit_product_palindrome.pl 906609 real 0m0.633s user 0m0.614s sys 0m0.018s
|
|---|