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

I HAZ A FIBO, RLY

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

Problem

First LOLCODE program, run here. This program inputs a Fibonacci number from the user, then determines and prints whether the number is in the Fibonacci series. What do you think?

HAI 1.2

    HOW IZ I GetNum

        VISIBLE "Entr yr numbr: "

        I HAS A WatzNum
        GIMMEH WatzNum

        FOUND YR WatzNum

    IF U SAY SO

    HOW IZ I ProovFibo YR Num

        I HAS A Fibo1 ITZ 1
        I HAS A Fibo2 ITZ 2

        IM IN YR Nums

            BOTH SAEM Num AN Fibo1, O RLY?
                YA RLY
                    VISIBLE "YEAH, FIBONACCI NUM!"
                    GTFO
                NO WAI
            OIC

            BOTH SAEM Num AN Fibo2, O RLY?
                YA RLY
                    VISIBLE "YEAH, FIBONACCI NUM!"
                    GTFO
                NO WAI
            OIC

            BOTH SAEM Fibo2 AN BIGGR OF Num AN Fibo2
            O RLY?
                YA RLY
                    DIFFRINT Num AN Fibo1, O RLY?
                        YA RLY
                            DIFFRINT Num AN Fibo2, O RLY?
                                YA RLY
                                    VISIBLE "BOO, IZ NO FIBONACCI NUM!"
                                    GTFO
                                NO WAI
                            OIC
                            GTFO
                        NO WAI
                    OIC
                NO WAI
            OIC

            Fibo1 R SUM OF Fibo1 AN Fibo2
            Fibo2 R SUM OF Fibo1 AN Fibo2

        IM OUTTA YR Nums

    IF U SAY SO

    I HAS A Num
    I HAS A MaxNum ITZ 50
    Num R I IZ GetNum MKAY
    Num IS NOW A NUMBR

    I IZ ProovFibo YR Num MKAY

KTHXBYE

Solution

BOTH SAEM Fibo2 AN BIGGR OF Num AN Fibo2
O RLY?
    YA RLY
        DIFFRINT Num AN Fibo1, O RLY?
            YA RLY
                DIFFRINT Num AN Fibo2, O RLY?
                    YA RLY
                        VISIBLE "BOO, IZ NO FIBONACCI NUM!"
                        GTFO
                    NO WAI
                OIC
                GTFO
            NO WAI
        OIC
    NO WAI
OIC


equals this pseudo code

if (Fibo2 >= max(Num, Fibo2))
{
    then
        if (Num != Fibo1) {
            then
                if (Num != Fibo2) {
                    then
                        print "BOO, IZ NO FIBONACCI NUM!"
                        break
                    else
                }
                break
            else
        }
    else
}


Basically, what you've made has some strange bits.

Let's clean it up.

if (Fibo2 >= max(Num, Fibo2))//weird check, replace with Fibo2 > Num instead
{
    then
        if (Num != Fibo1) {
            then
                if (Num != Fibo2) {
                    then
                        print "BOO, IZ NO FIBONACCI NUM!"
                        break
                    else//remove useless else
                }
                break
            else//remove useless else
        }
    else//remove useless else
}


if (Fibo2 > Num) {
then
    if (Num != Fibo1) { 
    then
        if (Num != Fibo2) { 
        then
            print "BOO, IZ NO FIBONACCI NUM!"
            break
        }
        break
    }
}


Huh. Well, we don't need a double break.

if (Fibo2 > Num) {
then
    if (Num != Fibo1) { 
    then
        if (Num != Fibo2) { 
        then
            print "BOO, IZ NO FIBONACCI NUM!"
        }
        break
    }
}


Well, neither of these two can be true, due to the way your algorithm works (you do a check for Fibo1 == Num and Fibo2 == Num to determine if it is a fibonacci number). Remove the unneeded checks.

if (Fibo2 > Num) {
then
    print "BOO, IZ NO FIBONACCI NUM!"
    break
}


And translate back to LOLCODE.

DIFFRINT Fibo2 AN SMALLR OF Fibo2 AN Num
O RLY?
YA RLY
    VISIBLE "BOO, IZ NO FIBONACCI NUM!"
    GTFO
OIC


Well, that simplified things!

Indentation

I still don't like the indentation, though. Three lines for a single if statement?
Let's combine them:

DIFFRINT Fibo2 AN SMALLR OF Fibo2 AN Num
O RLY?, YA RLY
    VISIBLE "BOO, IZ NO FIBONACCI NUM!"
    GTFO
OIC


This is the minimum I'd do. Even shorter would be:

DIFFRINT Fibo2 AN SMALLR OF Fibo2 AN Num, O RLY?, YA RLY
    VISIBLE "BOO, IZ NO FIBONACCI NUM!"
    GTFO
OIC


But then we're basically having the brace-style argument again; Put them on a new line or on the next?

LOLCODE internal workings

I confused myself a couple times during the writing of this answer.

For instance, you had a BOTH SAEM Fibo2 AN BIGGR OF Num AN Fibo2 check, but this is what the spec says:

BOTH SAEM  AN BIGGR OF  AN    BTW x >= y
BOTH SAEM  AN SMALLR OF  AN   BTW x  AN SMALLR OF  AN    BTW x > y
DIFFRINT  AN BIGGR OF  AN     BTW x < y


And you have BOTH SAEM AN BIGGR OF AN . So what is your version going to do?

Well, if we split it into two statements...

BIGGR OF  AN    BTW Math.max(x, y)
SMALLR OF  AN   BTW Math.min(x, y)
I HAZ A RESULT ITZ BIGGR OF  AN   BTW RESULT = Math.max(x, y)
BOTH SAEM  AN RESULT  BTW x = Math.max(x, y)


Apparently DIFFRINT Fibo2 AN SMALLR OF Fibo2 AN Num is what you need. It basically checks if "the smaller of Fibo2 and Num" is "Fibo2".

Code Snippets

BOTH SAEM Fibo2 AN BIGGR OF Num AN Fibo2
O RLY?
    YA RLY
        DIFFRINT Num AN Fibo1, O RLY?
            YA RLY
                DIFFRINT Num AN Fibo2, O RLY?
                    YA RLY
                        VISIBLE "BOO, IZ NO FIBONACCI NUM!"
                        GTFO
                    NO WAI
                OIC
                GTFO
            NO WAI
        OIC
    NO WAI
OIC
if (Fibo2 >= max(Num, Fibo2))
{
    then
        if (Num != Fibo1) {
            then
                if (Num != Fibo2) {
                    then
                        print "BOO, IZ NO FIBONACCI NUM!"
                        break
                    else
                }
                break
            else
        }
    else
}
if (Fibo2 >= max(Num, Fibo2))//weird check, replace with Fibo2 > Num instead
{
    then
        if (Num != Fibo1) {
            then
                if (Num != Fibo2) {
                    then
                        print "BOO, IZ NO FIBONACCI NUM!"
                        break
                    else//remove useless else
                }
                break
            else//remove useless else
        }
    else//remove useless else
}
if (Fibo2 > Num) {
then
    if (Num != Fibo1) { 
    then
        if (Num != Fibo2) { 
        then
            print "BOO, IZ NO FIBONACCI NUM!"
            break
        }
        break
    }
}
if (Fibo2 > Num) {
then
    if (Num != Fibo1) { 
    then
        if (Num != Fibo2) { 
        then
            print "BOO, IZ NO FIBONACCI NUM!"
        }
        break
    }
}

Context

StackExchange Code Review Q#79304, answer score: 46

Revisions (0)

No revisions yet.