Tuesday, November 1, 2011

Solution to Project Euler Problem 4 - The largest palindrome made from the product of two 2-digit numbers and two 3-digit numbers

http://projecteuler.net/problem=4

What will you know in this perl script ?
1) How to split a number/string (like 123456) into an array?
2) How to convert array into string?
3) Perl's numerical sort function and the <=> numerical comparison operator
4) Finding uniq elements in an array using grep and hash
5) You will know how to generate palindromes



Question: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.

Answer:  906609 (993 * 913)


Solution: 

#!/usr/bin/perl -w
use strict;


#Palindrome from 2 digit products
print "Palindromes from 2 digit products are ... \n";
generate_palindrome( 10, 99);
print "Palindromes from 3 digit products are ... \n";
#palindrome from 3 digit numbers
generate_palindrome( 100, 999);


sub generate_palindrome {
        my ($start, $end)=@_;
        my ($i, $j, $res,@number,@revers,$rev, @palindromes,@sorted, @unique, %s
een);
        for ($i=$start;$i<=$end;$i++) {
                for ($j=$start;$j<=$end;$j++) {
                        $res=$i*$j;
                        #Split a number into array
                        @number = ( $res =~ m/./g );
                        #reverse the array which stores the number
                        @revers=reverse(@number);
                        #convert array to string
                        $rev = "@revers";
                        #Replace the Extra separating spaces with blank
                        $rev =~ s/(.)\s/$1/seg;
                        #print "$i * $j = $res \n";
                        #print "Reverse: $rev \n";
                        if ( $res eq $rev ) {
                                #print "$res \n";
                                push (@palindromes, $res);
                        }
                }
        }


        #Perl's sort function and the <=> numerical comparison operator
        #The sort function takes an optional code block, which lets you replace
the default alphabetic comparison subroutine with your own.
        #This comparison function is called each time sort has to compare two va
lues.
        #The values to compare are loaded into the special package variables $a
and $b , which are automatically local ized.


        @sorted = sort { $a <=> $b } @palindromes;
        # Finding uniq elements in an array using grep and hash
        @unique = grep { ! $seen{$_}++ } @sorted;
        print "@unique\n";
        print "The largest palindrome is $unique[$#unique] \n";
}


Output: 
Palindromes from 2 digit products are ...
121 242 252 272 323 363 414 434 444 464 484 494 525 555 575 585 595 616 636 646 656 666 676 686 696 737 767 777 828 848 858 868 888 949 969 979 989 999 1001 1221 1551 1771 1881 2002 2112 2332 2442 2552 2772 2992 3003 3663 3773 4004 4224 4554 4664 4774 4884 5005 5115 5225 5335 5445 5775 6006 6336 6776 7007 7227 8008 8118 8448 9009
The largest palindrome is 9009
Palindromes from 3 digit products are ...
10201 11211 12221 12321 13231 13431 14241 14541 14641 15151 15251 15351 15651 15851 16261 16761 17271 17871 18081 18281 18981 19291 19591 20002 20202 20402 20502 21012 21112 21312 21412 21712 21812 21912 22022 22422 22922 23232 23432 23532 23632 23932 24442 24642 24742 25152 25252 25452 25652 25752 26062 26162 26462 26562 26862 26962 27072 27472 27572 27772 27872 27972 28182 28282 28482 28782 29192 29392 29492 29592 29792 29892 29992 30003 30303 30603 31313 31413 31613 3252332623 33033 33233 33333 33633 34243 34443 34643 34743 35453 35653 35853 35953 36663 36863 36963 37073 37373 37673 37873 37973 38383 38683 39093 39593 39693 39893 40004 40304 40404 40504 40704 40804 41114 41314 41514 41814 42024 42224 42624
42824 42924 43134 43434 43734 43834 44044 44144 44344 44444 44544 44744 44844 44944 45154 45254 45854 45954 46364 46464 46664 46764 46864 46964 47174 47674 47874 47974 48184 48384 48484 48884 48984 49494 49594 49794 49894 50005 50505 50605 51015 51315 51415 51615 51815 52125 52325 52425 52525 52625 52725 52925 53235 53535 53835 53935 54145 54945 55055 55255 55555 55755 55955 56165 56265 56465 56565 56865 57275 57375 57475 57575 57875 58185 58485 58685 58985 59095 59295 5949559595 59895 59995 60006 60306 60606 60706 61016 61116 61516 61716 62426 62526 62626 62726 62826 62926 63036 63336 63536 63736 63936 64246 64446 64546 64746 65056 65156 65556 65656 65856 66066 66466 66566 66666 66766 66866 67076 67176 67276
67776 67876 67976 68086 68186 68286 68486 68586 68686 68786 68886 69296 69496 69596 69696 69996 70007 70307 70707 70807 71117 71217 71817 72027 72627 72927 73337 73437 73537 73937 74347 74447 74547 74847 74947 75057 76167 76467 76867 77077 77677 77777 77877 78287 78387 78987 79097 79297 79497 79597 79797 79897 80008 80208 80408 80608 80808 80908 81018 81618 81718 81918 82128 82228 82328 82628 82728 82928 83538 83638 83738 83838 84048 84148 84348 84448 85058 85158 85358 86268 86668 86768 86868 87078 87178 87278 87478 87978 88088 88288 88688 88788 88888 89198 89298 89498 89598 89698 89798 90009 90109 90209 90909 91719 92129 92229 92329 92529 92629 92829 93639 93839 93939 94149 94249 95259 95559 95659 96369 96669
96869 97079 98189 98289 98489 98589 98789 98889 99099 99199 99299 99599 99699 99799 99899 99999 101101 102201 105501 106601 108801 110011 111111 117711 119911 121121 122221 123321 127721 128821 129921 131131 133331 135531 137731 138831 140041 141141 142241 143341 147741 149941 154451 155551 156651 159951 161161 162261 165561 168861 171171 174471 178871 180081 182281 184481 187781 188881 189981 198891 201102 202202 204402 209902 210012 212212 213312 214412 215512 216612 219912 220022 221122 222222 225522 227722 231132 232232 234432 235532 238832 239932 242242 244442 246642 249942 252252 255552 256652 257752 258852 259952 262262 266662 270072 272272 273372 276672 277772 279972 280082 282282 284482 286682 289982 290092 292292 294492 296692 297792 299992 301103 302203 303303 306603 308803 320023 321123 324423 329923 330033 333333 335533 343343 345543 348843 354453 357753 359953 363363 366663 367763 369963 371173 372273 374473 375573 377773 378873 384483 391193 393393 397793 399993 401104 402204 404404 405504 407704 408804 409904  412214 414414 416614 420024 421124 424424 425524 426624 428824 432234 434434 436634 438834 440044 441144 442244 443344 444444 445544 447744 452254 456654 459954 461164 462264 464464 468864 469964 470074 471174 474474 476674 477774 484484 485584 487784 488884 489984 491194 493394 505505 506605 507705 508805 509905 510015 512215 513315 514415 515515 519915 520025 522225 523325 525525 528825 531135 534435 535535 536635 543345 545545 548845 549945 550055 551155 554455 555555 560065 561165 564465 565565 570075 571175 573375 575575 576675 577775 579975 580085 585585 588885 589985 592295 595595 601106 602206 603306 604406 606606 611116 612216 616616 618816 619916 623326 630036 631136 636636 639936 642246 648846 649946 650056 652256 653356 654456 656656 657756 660066 661166 663366 666666 672276 675576 678876 689986 693396 696696 698896 723327 729927 737737 747747 749947 770077 780087 793397 801108 802208 804408 807708 809908 819918 821128 824428 828828 840048 853358 855558 861168 886688 888888 906609
The largest palindrome is 906609 (993 * 913 )

No comments: