Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Ponder This: the worm-eaten page

by gaal (Parson)
on Dec 16, 2004 at 22:51 UTC ( #415505=CUFP: print w/ replies, xml ) Need Help??

IBM have a monthly riddle set up called Ponder This. The riddles are mathematical -- too difficult for me :)

Here's part of Novermber's (cute) riddle, followed by a Perl solution that surprised me for being so short and, to my perlverted mind, straightforward. I've only begun looking at that site, but I bet other riddles there can be solved with Perl, too.

The numbers 1 to 9999 (decimal base) were written on a paper. Then the paper was partially eaten by worms. It happened that just those parts of paper with digit "0" were eaten. Consequently the numbers 1200 and 3450 appear as 12 and 345 respectively, whilst the number 6078 appears as two separate numbers 6 and 78. What is the sum of the numbers appearing on the worm-eaten paper?
perl -le '$sum += $_ for map { split /0/ } 1 .. 9999; print $sum' 37359000

Comment on Ponder This: the worm-eaten page
Download Code
Re: Ponder This: the worm-eaten page
by BrowserUk (Pope) on Dec 16, 2004 at 23:16 UTC

    Cute. Though it would be a tad more efficient, (and -w complient)if it were /0+/


    Examine what is said, not who speaks.        The end of an era!
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Which can conceptually thought of as, "if a worm is already in the neighborhood, it'll eat a 0 it finds close by before setting out to forage for more elsewhere".

      Okay, okay, metaphors can get silly sometimes :)

Re: Ponder This: the worm-eaten page
by Anonymous Monk on Dec 17, 2004 at 10:59 UTC
    perl -le '$_ = "@{[1 .. 9999]}"; s/[ 0]+/+/g; print eval'
      Nice++ :)
      bit shorter
      perl -le 'print eval join'+',map{split/0+/}1..9999'
        But I suppose this is cheating:
        perl -le 'map$\+=$_,map/[^0]+/g,1..9999;print'
      perl -le '$_=join 0,1..9999;y/0/+/s;print eval'

        Gasp, what a beautiful orange! Could this really be brilliant Dutch golfer (-ugene in disguise? If so, he might be irritated if someone shaved a single stroke from his lovely solution. ;-)

        # One stroke improvement on (-ugene. ^.^ perl -le '$_=join 0,1..9x4;y/0/+/s;print eval' # And a couple of longer ones for fun. perl -le 'print eval join"+",map/[^0]+/g,1..9x4' perl -le 's}}join"+",map/[^0]+/g,1..9x4}ee;print' perl -le 's;;$%x9998;e;s;;++$%;eg;y;0;+;s;print eval'
Re: Ponder This: the worm-eaten page
by zentara (Archbishop) on Dec 17, 2004 at 12:17 UTC
    perlverted :-) I learned a new word today.

    I'm not really a human, but I play one on earth. flash japh
Re: Ponder This: the worm-eaten page
by hv (Parson) on Dec 17, 2004 at 13:58 UTC

    This can also be solved by arithmetic quite readily.

    The sum of the digits 1 .. 9 is 45.

    Each digit appears 1000 times in each of the units, 10s, 100s and 1000s places.

    The units always count as themselves: total 1000 * 45.

    The tens are followed by a zero 1/10 of the time: total (100 + 900 * 10) * 45.

    The hundreds are directly followed by a zero 1/10 of the time, and followed by a non-zero digit then a zero 1/10 of the remaining: total (100 + 90 * 10 + 810 * 100) * 45.

    Similarly for the thousands: total (100 + 90 * 10 + 81 * 100 + 729 * 1000) * 45.

    So the sum is (1300 + 1080 * 10 + 891 * 100 + 729 * 1000) * 45 = 830200 * 45 = 37,359,000

    This approach can readily be adapted to base b, and beyond to the eat digits 0 .. x-1 required for part 2 of the puzzle.

    Hugo

Re: Ponder This: the worm-eaten page
by Anonymous Monk on Jan 26, 2005 at 12:48 UTC
    Of course you take a lateral approach and say that the "worm-eaten paper" is the missing part and therefore the sumof the zeros would of course be zero?
      But if you do that, you have to consider that the zeros on the page may form a pattern that looks like a number. Now you have to go over the many possible dimentions of the page and check them all! Thankfully, you have a head start, since you can use this OCR code; all you have to account for now is rotation.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://415505]
Approved by BrowserUk
Front-paged by ysth
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-07-22 07:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (106 votes), past polls