Thursday, September 5, 2013

LOLCODE - Problem 10 Redo

Most programming languages have some sort of an inspiration. FORTRAN was inspired by a want to TRANslate FORmulas, LISP was designed for LISt Processing, Whitespace was inspired by having too much to drink one night, BrainFuck was inspired by a wish to make a minimal Turing-complete language...and also to make a very obfuscated language. But yesterday, I programmed in a language inspired by these:


LOLCODE is a wonderful language, where programs are written in a language that would be very familiar to one of these cats, which of course should always be the target audience for a program. Anyhow, I decided to resolve Problem 10 in LOLCODE - my solution runs in about 15 minutes, which is unfortunately long, but I am pretty sure that this is mostly due to aspects of the language not being implemented very efficiently, because similar prime sieves generally run more quickly in other languages. The most unfortunate aspect of that 15 minutes wait is that when I ran it for the first time, I discovered that the answer was greater than 231, which is the greatest value that can be stored in a NUMBR in lolcode. To remedy this, I store the answer as two base 10,000,000 digits, which then when concatenated form the correct answer (notably this would require some fanciness to accommodate for leading zeroes in the one's place...but luckily there were no leading 0's in this case.) Now, without further ado, my LOLCODE solution (which has now opened up ML for later use)
CAN HAS STDIO?
HAI 1.3

I HAS A TOTAL ITZ 2
I HAS A TOTALM ITZ 0
I HAS A P ITZ 3
I HAS A N
IM IN YR NUMS
    N R 2
    BTW NEWTON'S METHOD FOR SQRT
    I HAS A XN
    XN R QUOSHUNT OF P AN 2
    I HAS A XN1
    IM IN YR LOOP
        XN1 R QUOSHUNT OF SUM OF XN AN QUOSHUNT OF P AN XN AN 2
        EITHER OF BOTH SAEM XN AN XN1 AN BOTH SAEM XN AN SUM OF XN1 AN 1
        O RLY?
            YA RLY
                GTFO
            NO WAI
                XN R XN1
        OIC
    IM OUTTA YR LOOP

    I HAS A CAP ITZ SMALLR OF XN AN XN1
    IM IN YR TEST
        BOTH SAEM 0 AN MOD OF P AN N
        O RLY?
            YA RLY
                GTFO
        OIC
        DIFFRINT N AN SMALLR OF N AN CAP
        O RLY?
            YA RLY
                BTW ANSWER IS REALLY BIG SO WE REPRESENT IT
                BTW IN BASE 1000000
                TOTAL R SUM OF TOTAL AN P
                BOTH SAEM TOTAL AN BIGGR OF TOTAL AN 10000000
                O RLY?
                    YA RLY
                        TOTAL R DIFF OF TOTAL AN 10000000
                        TOTALM R SUM OF TOTALM AN 1
                OIC
                GTFO
        OIC
        N R SUM OF N AN 1

    IM OUTTA YR TEST
    BOTH SAEM P AN 1999999
    O RLY?
        YA RLY
            GTFO
        NO WAI
            P R SUM OF P AN 2
    OIC
IM OUTTA YR NUMS
VISIBLE ":{TOTALM}:{TOTAL}"
KTHXBYE

No comments:

Post a Comment