• Your Name (unregistered)
    LastDigit = mod(N) mod 10
    isn't that supposed to be int(N)?
  • Your Name (unregistered) in reply to Your Name
    wtf, the forum screws up quotes now?
  • (cs)

    Presumably, the coder didn't trust the obvious N mod 2 solution.

    Perhaps he thought that odd and even don't work when the number is binary.

    No, I've worked it out. This is the result of a genetically determined algorithm - random code was generated until the output from the function was always correct.

  • StarLite (unregistered) in reply to Bellinghman

    I also like the fact he has 2 functions: idOdd() and isEven().
    I wonder if he will call isOdd() when isEven() returns false :P

  • (cs) in reply to StarLite

    Another likely solution:

    n And 1 ' Remember, Folks, Bit 0 makes an odd number [;)]

     

  • Brent (unregistered) in reply to Mike R

    He could have converted the number to a string to get the last digit.

  • (cs) in reply to Mike R

    I'm a little confused...

    Since N isn't specified as an int, as far as I can see, what will these functions return for, say, IsEven(4.2)? [:|]

    Scary stuff.

  • (cs) in reply to rogueRPI

    Simply amazing... he understands how mod works, but can't figure out how to use it to determine if a number is even?

    Having two functions is SOMEWHAT understandable for readability... it doesn't really hurt anything, except maybe maintainability if the definition of odd and even changes (or if they figure out what they're doing with floats :))

  • Tom Mathews (unregistered)

    You're thinking of Captchas.

    Try here: http://www.15seconds.com/issue/040202.htm

  • (cs) in reply to mugs
    mugs:

    Simply amazing... he understands how mod works, but can't figure out how to use it to determine if a number is even?

    Having two functions is SOMEWHAT understandable for readability... it doesn't really hurt anything, except maybe maintainability if the definition of odd and even changes (or if they figure out what they're doing with floats :))

    In case it wasn't obvious, the bolded part was meant to be sarcastic. [:)]

  • (cs) in reply to Bellinghman

    Bellinghman:
    Presumably, the coder didn't trust the obvious N mod 2 solution. Perhaps he thought that odd and even don't work when the number is binary. No, I've worked it out. This is the result of a genetically determined algorithm - random code was generated until the output from the function was always correct.

    lol - same thing I thought...WhyTF is he/she using mod 10?!?[:|]

  • (cs) in reply to rogueRPI

    rogueRPI:
    Since N isn't specified as an int, as far as I can see, what will these functions return for, say, IsEven(4.2)? [:|]

    It will return True, since he/she's casting N to an int 4.2 will become 4.

  • znaps (unregistered)
    <!--StartFragment --> I see the WTF! For isOdd(N) he should have returned !isEven(N), right ?!? right?!?!
  • misses the point (unregistered) in reply to skicow
    skicow:

    lol - same thing I thought...WhyTF is he/she using mod 10?!?[:|]


    Why not use mod 6?  The even numbers would then be 0,2 and 4.  Odd numbers: 1,3 and 5.

    It will work with mod by any even number...

  • (cs) in reply to skicow
    skicow:

    rogueRPI:
    Since N isn't specified as an int, as far as I can see, what will these functions return for, say, IsEven(4.2)? [:|]

    It will return True, since he/she's casting N to an int 4.2 will become 4.

    Ah, duh, me brain not go work-work yet today huh huh.

    Of course that just raises new questions, since he's casting his input then returning information about the (possibly changed) input number [:^)]

  • Jimmy James (unregistered) in reply to Bellinghman

    "Presumably, the coder didn't trust the obvious N mod 2 solution."

    Maybe the developer didn't realize understand the concept of even vs. odd and only knew this rule by rote.  I know lots of developers who are unable to

  • Sweets (unregistered)

    IF NOT IsEven(N) AND NOT IsOdd(N) THEN
           Call IM_AN_IDIOT()
    End if

  • (cs) in reply to misses the point
    Anonymous:
    skicow:

    lol - same thing I thought...WhyTF is he/she using mod 10?!?[:|]


    Why not use mod 6?  The even numbers would then be 0,2 and 4.  Odd numbers: 1,3 and 5.

    It will work with mod by any even number...

    I know that mod will work with any even number, but didn't you learn in school to get the lowest common denominator? [;)]

    LastDigit = int(N) mod 666 [6]

  • Anonymous (unregistered) in reply to Tom Mathews

    Open that link and do a search for "Figure: dictionarydb".  A bonus WTF for today!

  • Anonymous (unregistered) in reply to Anonymous

    Sorry, I didn't do that post right - I meant to reply to Tom Mathews's post with link to http://www.15seconds.com/issue/040202.htm.

  • Steve (unregistered)

    Oh boy, thanks for the laugh, was feeling a bit tired today but that perked me up.  Made me realize that my 4 years of university was worth something after all.

  • misses the point (unregistered) in reply to Anonymous
    Anonymous:

    Open that link and do a search for "Figure: dictionarydb".  A bonus WTF for today!


    I do not see what you are referring to.  Was there an error on the page that you saw?
    Maybe the error has been fixed already.

  • (cs) in reply to Steve

    >Made me realize that my 4 years of university was worth something after all.

    So at the end of 4 years of university study you can reliably and efficiently tell if a number if odd or even?

    Congratulations!  You are doing better than most [:)]

  • (cs) in reply to misses the point
    Anonymous:
    Anonymous:

    Open that link and do a search for "Figure: dictionarydb".  A bonus WTF for today!


    I do not see what you are referring to.  Was there an error on the page that you saw?
    Maybe the error has been fixed already.

    I couldn't find the text "Figure: dictionarydb", but just under the text: "Pictures below demonstrate CAPTCHAs generated by gimpy" check out the top right image and they word they say it is [*-)]

    Cheers,

    evnafets

  • Anonymous (unregistered)

    eh, it basically what everybody was taught in gradeschool.  I wouldn't be surprised if this was a typical example of somebody coming up with a solution and asking somebody more knowledgeable for the piece they needed to get the solution to work (how do I find the last digit in a number).  If they had mentioned what they were really trying to do they would have gottena better answer.

    It could easily be worse.  He could have converted N to a string and used string ops to find the lsat digit.

  • SwitchBL8 (unregistered) in reply to evnafets

    As you can see, their word "space" is in red, because they should have typed "spade". The rest of the captions are in green, meaning their a bit human after all.

  • TopCod3r (unregistered)

    What? doesnt this code work? At least he didnt convert it to a string first. What else can you ask for?

  • (cs)

    <font size="4">

    <font size="3">Is there a function IsEvenOdder() ?
    Though I can't imagine that's possible.</font>
    <font size="3"></font>
    </font>

  • Anonymous Coward (unregistered)
    <!--StartFragment -->I've seen worse:

    <FONT class=fixed_width face="Courier, Monospaced">DEFSNG A-Z 
    FUNCTION IsItOdd% (VAR&)
    'Turn VAR& into a string
    VAR$ = STR$(VAR&)
    'Get the last character in the string
    VAR$ = RIGHT$(VAR$, 1)
    'Convert the last character to a single number
    VAR = VAL(VAR$)
    SELECT CASE VAR
            'IsItOdd% = 0 if even, 1 if odd
            CASE IS = 0
                    IsItOdd% = 0
            CASE IS = 1
                    IsItOdd% = 1
            CASE IS = 2
                    IsItOdd% = 0
            CASE IS = 3
                    IsItOdd% = 1
            CASE IS = 4
                    IsItOdd% = 0
            CASE IS = 5
                    IsItOdd% = 1
            CASE IS = 6
                    IsItOdd% = 0
            CASE IS = 7
                    IsItOdd% = 1
            CASE IS = 8
                    IsItOdd% = 0
            CASE IS = 9
                    IsItOdd% = 1
    END SELECT
    END FUNCTION
    </FONT>

    Disclaimer: This was posted on Usenet, so it doesn't meet the requirements for a WTF submission.

  • Andre (unregistered) in reply to Anonymous Coward

    Nope, it can still get worse...

    <?php

    <font color="#000000"><font color="#007700">function </font><font color="#0000bb">IsEven</font><font color="#007700">(</font><font color="#0000bb">$num</font><font color="#007700">)
    {
    </font><font color="#0000bb">  </font><font color="#007700">if (</font><font color="#0000bb">$num </font><font color="#007700">< </font><font color="#0000bb">0</font><font color="#007700">)
    </font><font color="#0000bb">     return IsEven</font><font color="#007700">(-</font><font color="#0000bb">$num</font><font color="#007700">);
    </font><font color="#0000bb">  </font><font color="#007700">if (</font><font color="#0000bb">$num </font><font color="#007700">> </font><font color="#0000bb">1</font><font color="#007700">)
    </font><font color="#0000bb">     return IsEven</font><font color="#007700">(</font><font color="#0000bb">$num</font><font color="#007700">-</font><font color="#0000bb">2</font><font color="#007700">);
    </font><font color="#0000bb">  </font><font color="#007700">if (</font><font color="#0000bb">$num </font><font color="#007700">== </font><font color="#0000bb">1</font><font color="#007700">)
    </font><font color="#0000bb">     return 0</font><font color="#007700">;
    </font><font color="#0000bb">  </font><font color="#007700">else return </font><font color="#0000bb">1</font><font color="#007700">;
    }
    </font><font color="#0000bb">?>

    (from thephpwtf.com ...)
    </font> </font>

  • (cs) in reply to Andre

    Recursive odd/even is the best ever. =D Impress professors and colleagues alike!

    I like the new forum look a lot, except the main page is too slim (I can't find whatever css is doing it) and the lack of a reply button on the bottom. (Quickpost would be even nicer, hint to CommunityServer folks.) Otherwise, [Y] here's to (hopefully) the end of the forum wtfs!

  • Anonymous (unregistered) in reply to Tom Mathews
    Anonymous:

    You're thinking of Captchas.

    Try here: http://www.15seconds.com/issue/040202.htm

    Sorry guys...  If you scroll to the bottom of the above page you will see "Writing Your Own CAPTCHA Application."  Following that link takes you to, http://www.15seconds.com/issue/040203.htm.  On that page you will see the "Figure: dictionarydb" I was talking about.

    This table is in what I like to call "26th Normal Form."

  • (cs)

    Good that he did it with 10, iimagine what should have happened if it was 100.

  • Welcome To The Machine (unregistered)

    <FONT face=Tahoma size=2>Hmmm, and what about negatives?</FONT>

  • Not anonymous any more (unregistered)

    Another solution:

    Find all prime factors of N and check if 2 is one of them.

  • (cs) in reply to Anonymous
    Anonymous:
    Anonymous:

    You're thinking of Captchas.

    Try here: http://www.15seconds.com/issue/040202.htm

    Sorry guys...  If you scroll to the bottom of the above page you will see "Writing Your Own CAPTCHA Application."  Following that link takes you to, http://www.15seconds.com/issue/040203.htm.  On that page you will see the "Figure: dictionarydb" I was talking about.

    This table is in what I like to call "26th Normal Form."

    Haha... wtf would they do that?

  • (cs)

    I realize it isn't required in VB, but for god's christ please try to add some declarations in your code.

    Two functions there, are they private or public? We'll just let it use whatever the default is.

    Is that parameter being passed by reference or by value? Who cares, it's not like the function is modifying the value! Not yet, at least.

    And speaking of which, what kind of variable is that coming in? VB's catch-all Variant type, no doubt.

    Finally, why bother even declaring variables if you're not going to give them a type? Just get rid of that "Option Explicit" at the top of your form (if you even bothered) and let the compiler do all the work for you. After all, you have to get back to writing Excel macros to calculate your cell phone bill. I know I'm anal retentive about it.

    Oh yeah, and the functions suck.

  • (cs)

    Perhaps the real WTF is that a swiss-army-knife language like VB6 doesn't already have these functions. While I've always favored minimalist core languages in general, it seems to me that if you're going to go the 'bigger is better' route, you should at least cover all the reasonable cases, and this is one that comes up quite often.

    In VB if you don't declare types, it uses Variants for everything, which is pretty inefficient; also, you end up with typing holes like you won't believe. I guess this person hasn't heard of Option Explicit, either. There are some languages where dynamic typing is the standard idiom, but VB isn't one of them.

    Anyway, as has already been beaten to death here, the most reasonable approaches for this function are either

    <font size="4">Function IsOdd(n as Integer) as Boolean
        IsOdd = CBool(n mod 2)
    End Function</font>

    or

    <font size="4">Function IsOdd(N as Integer) as Boolean 
        IsOdd = CBool(n And 1)
    End Function</font>

    either of which would be easier to code and understand than this. And of course, the obvious way to write IsEven() would be

    <font size="4">Function IsEven(n as Integer) as Boolean
        IsEven = Not IsOdd(n)
    End Function</font>

    Was this too hard to figure out?

    Pedantic aside: in priniciple, the bitwise version should be slightly faster than the modulo version on most systems; in practice, the difference is probably not measurable. Also, the bitwise approach assumes 2's-complement arithmetic; while this is always true with VB, as well as on virtually all current hardware, it would technically be a portability WTF in languages such as C.

    (BTW, what's the preferred method of posting code examples in a message? The [ code ] tags seem to have problems with indentation, at least in preview mode. I'm using the Formatted text type here, but it doesn't look quite right.)

  • Walter (unregistered) in reply to misses the point

    Why not use mod 6?  The even numbers would then be 0,2 and 4.  Odd numbers: 1,3 and 5.


    Or indeed, mod 2...

  • (cs) in reply to Walter

    Bah, you heretics and your mod 2. Why back in my day we had to make use of nothing but OR gates because AND gates were more expensive.

    The sad part is that I am semi-serious. I had to hard-code (meaning pluggable module sets) using nothing but OR gates and the last 3 AND gates we had in stock. Since the stock would not be replenished until after quarterly cutoff and this machine (meaning high-profit contact) had to be on the train before quarterly ended.

  • (cs) in reply to GalacticCmdr

    OR gates? And three ANDs? Bloody luxury, that. We had nothing but NAND gates -- nothing at all but, mind you, on 4-bys  -- then our fathers would beat us to death with a herring.

  • wsargent (unregistered) in reply to znaps

    No,  ! isEven() is the same as isUnEven(), for lumpy numbers.  Likewise, ! isOdd() is actually the same as isUnOdd(), otherwise known as isNormal().

    Sheesh.  What are those math professors teaching these days?

  • (cs) in reply to Andre
    Anonymous:
    Nope, it can still get worse...

    <font color="#000000"><font color="#007700">function </font><font color="#0000bb">IsEven</font><font color="#007700">(</font><font color="#0000bb">$num</font><font color="#007700">)
    {
    </font><font color="#0000bb">  </font><font color="#007700">if (</font><font color="#0000bb">$num </font><font color="#007700">< </font><font color="#0000bb">0</font><font color="#007700">)
    </font><font color="#0000bb">     return IsEven</font><font color="#007700">(-</font><font color="#0000bb">$num</font><font color="#007700">);
    </font><font color="#0000bb">  </font><font color="#007700">if (</font><font color="#0000bb">$num </font><font color="#007700">> </font><font color="#0000bb">1</font><font color="#007700">)
    </font><font color="#0000bb">     return IsEven</font><font color="#007700">(</font><font color="#0000bb">$num</font><font color="#007700">-</font><font color="#0000bb">2</font><font color="#007700">);
    </font><font color="#0000bb">  </font><font color="#007700">if (</font><font color="#0000bb">$num </font><font color="#007700">== </font><font color="#0000bb">1</font><font color="#007700">)
    </font><font color="#0000bb">     return 0</font><font color="#007700">;
    </font><font color="#0000bb">  </font><font color="#007700">else return </font><font color="#0000bb">1</font><font color="#007700">;
    }
    </font><font color="#0000bb">?>

    (from thephpwtf.com ...)
    </font> </font>

    Now that takes the cake!  It's a little hard to believe someone who can think recursively would write this as serious code, though. 
  • (cs)

    That just makes me shudder [:|]

  • Pax (unregistered) in reply to Mike R

    That only works for numbers stored as 2's-complement which I admit is the vast majority of circumstances, but is in no means mandated by any standard.

  • Pax (unregistered) in reply to mugs

    "if the definition of odd and even changes" ?????

    Reminds me of the following snippet of code:

    #define PI 3.14159 /* In case value of PI ever changes */

    Regards,

    Pax.

    PS: Hey, I can't even read that captcha box - I'll have to repost.

     

    Aaah, that's better.

     

  • LinuxUser (unregistered)

    Oh joy, the joy of that code! Oh that is so beautiful, I think I will start using that same brilliant method in my code. You know, most programming can be done without much understanding of math, but understanding just a little bit of math now and then wouldn't hurt.

  • (cs) in reply to Schol-R-LEA
    Schol-R-LEA:

    Pedantic aside: in priniciple, the bitwise version should be slightly faster than the modulo version on most systems; in practice, the difference is probably not measurable. Also, the bitwise approach assumes 2's-complement arithmetic; while this is always true with VB, as well as on virtually all current hardware, it would technically be a portability WTF in languages such as C.


    I don't see why it would lead to portability problems? Both the C and C++ standards definition od operator & should ensure that the following will return 1 if the number is odd:

    int isOdd(int i){
        return i & 1;
    }


    Maybe I'm missing something though. What system would that not port to?
  • (cs) in reply to Andy

    Wow that code tag really jacked up my formatting. That should be(second try):
    <font face="Lucida Console" size="2"> int isOdd(int i){
        return i & 1;
    }</font>

  • (cs) in reply to Andy

    Perhapse I should clarify. In both the C and C++ standards & is a binary operator. The word binary by definition is two's compliment is it not? I could be wrong but I thought the two were identical. If I am correct then any integer &'d by 1 should return 1. Is this not correct?

Leave a comment on “IsOdd()? Yes, Is Very Odd.”

Log In or post as a guest

Replying to comment #:

« Return to Article