HiveBrain v1.2.0
Get Started
← Back to all entries
patternMajor

LOL'ing-Up Project Euler One

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
ingloloneprojecteuler

Problem

It's been a while since I last wrote some lolcode, so I felt like tackling Project Euler #1.


If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.


Find the sum of all the multiples of 3 or 5 below 1000.

This is the code/script I wrote to solve this:

HAI 1.2
    VISIBLE "HAI, PROJEK LOLLER ONE!!"

    I HAS A LIMIT ITZ 1000
    I HAS A TOTAL ITZ 0
    I HAS A CHEEZ ITZ 3
    I HAS A BURGER ITZ 5
    I HAS A CHEEZBURGER ITZ PRODUKT OF CHEEZ AN BURGER

    HOW IZ I ADDTOTAL YR VALUE
        I HAS A RESULT ITZ SUM OF VALUE AN TOTAL
        FOUND YR RESULT
    IF U SAY SO

    IM IN YR MIND UPPIN YR NUMBER TIL BOTH SAEM NUMBER AN LIMIT

        I HAS A PICKLE ITZ FAIL

        BOTH SAEM 0 AN MOD OF NUMBER AN CHEEZBURGER
        O RLY?, YA RLY
            PICKLE R WIN
        NO WAI
            BOTH SAEM 0 AN MOD OF NUMBER AN CHEEZ
            O RLY?, YA RLY
                PICKLE R WIN
            NO WAI
                BOTH SAEM 0 AN MOD OF NUMBER AN BURGER
                O RLY?, YA RLY
                    PICKLE R WIN
                OIC
            OIC
        OIC

        BOTH SAEM PICKLE AN WIN
        O RLY?, YA RLY
            TOTAL R I IZ ADDTOTAL YR NUMBER MKAY
        OIC

    IM OUTTA YR MIND

    VISIBLE SMOOSH "TEH ANSWER IZ " AN TOTAL MKAY
    VISIBLE "DOWN WITH PROJEK LOLLER ONE!"

KTHXBYE


This code can be executed on CodingGround, and produces this output:


HAI, PROJEK LOLLER ONE!!

(spoiler)


TEH ANSWER IZ 233168


DOWN WITH PROJEK LOLLER!

Is the fizzbuzz-like logic appropriate, or I fell in the "easy" trap? Would there be a way to collapse all the conditions into one and make a shorter script? Or is this algorithm a bad/inefficient one?

Solution

Same Guy, Same Language, same criticism?

Let me start by (again) critzising your variable casing choice of SHOUTCASE

It's so darn hard to tell what's a variable and what's not :(

But your names got better since the last time. Cheez and Burgr aren't the best, but given their content is arbitrarily set and accordingly somewhat meaningless they really stand their ground.

One thing though: your Cheezburgers are not needed.

It's a waste of computation power to check divisors by the biggest one first if you return early. (I recall rolfl having some nice stats on that in one fizz-buzz review I can't find right now). We can completely elimimate that O RLY? YA RLY from your code.

Oh and while we're at cheeseburgers... There's one thing I really dislike on cheeseburgers... Pickles.

The temporary TROOF variable to remove that three YA RLY doing the same thing is completely overkill. Instead of PICKLES R WIN it would be sufficient to add the current Number to the total.

Code until now:

IM IN YR Mind UPPIN YR Number TIL BOTH SAEM Number AN Limit   
    BOTH SAEM 0 AN MOD OF Number AN Cheez
    O RLY?, YA RLY
          Total R I IZ AddToTotal YR Number MKAY
    NO WAI
          BOTH SAEM 0 AN MOD OF Number AN Burger
          O RLY?, YA RLY
                Total R I IZ AddToTotal YR Number MKAY
          OIC
    OIC
IM OUTTA YR Mind


Now what have we got here? this looks like it should be either an ElseIf or just a simple combination of conditions into an Or.

The latter is the easier one, but for reference: There is an else if. It's MEBBE

Final Code:

HAI 1.2
    VISIBLE "HAI, PROJEK LOLLER ONE!!"

    I HAS A Limit ITZ 1000
    I HAS A Total ITZ 0
    I HAS A Cheez ITZ 3
    I HAS A Burger ITZ 5

    HOW IZ I AddToTotal YR Value
        I HAS A Result ITZ SUM OF Value AN Total
        FOUND YR Result
    IF U SAY SO

    BTW Iterates from 0 to 999
    IM IN YR Mind UPPIN YR Number TIL BOTH SAEM Number AN Limit
        EITHER OF BOTH SAEM 0 MOD OF Number AN Cheez AN BOTH SAEM 0 MOD OF Number AN Burger
        O RLY?, YA RLY
             Total R I IZ AddToTotal YR Number MKAY
        OIC
    IM OUTTA YR Mind

    VISIBLE SMOOSH "TEH ANSWER IZ " AN Total MKAY
    VISIBLE "DOWN WITH PROJEK LOLLER ONE!"

KTHXBYE

Code Snippets

IM IN YR Mind UPPIN YR Number TIL BOTH SAEM Number AN Limit   
    BOTH SAEM 0 AN MOD OF Number AN Cheez
    O RLY?, YA RLY
          Total R I IZ AddToTotal YR Number MKAY
    NO WAI
          BOTH SAEM 0 AN MOD OF Number AN Burger
          O RLY?, YA RLY
                Total R I IZ AddToTotal YR Number MKAY
          OIC
    OIC
IM OUTTA YR Mind
HAI 1.2
    VISIBLE "HAI, PROJEK LOLLER ONE!!"

    I HAS A Limit ITZ 1000
    I HAS A Total ITZ 0
    I HAS A Cheez ITZ 3
    I HAS A Burger ITZ 5

    HOW IZ I AddToTotal YR Value
        I HAS A Result ITZ SUM OF Value AN Total
        FOUND YR Result
    IF U SAY SO


    BTW Iterates from 0 to 999
    IM IN YR Mind UPPIN YR Number TIL BOTH SAEM Number AN Limit
        EITHER OF BOTH SAEM 0 MOD OF Number AN Cheez AN BOTH SAEM 0 MOD OF Number AN Burger
        O RLY?, YA RLY
             Total R I IZ AddToTotal YR Number MKAY
        OIC
    IM OUTTA YR Mind

    VISIBLE SMOOSH "TEH ANSWER IZ " AN Total MKAY
    VISIBLE "DOWN WITH PROJEK LOLLER ONE!"

KTHXBYE

Context

StackExchange Code Review Q#79298, answer score: 24

Revisions (0)

No revisions yet.