• ironclay (unregistered) in reply to Mike R

    that's what mod does, so why right it yourself?

  • ironclay (unregistered) in reply to Andy
    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?

    Wrong.

    it does a bit-wise comparison of each bit in the integer.

    0000 0000
    0000 0001

    this will return 0.
  • (cs) in reply to Andy

    Andy:
    The word binary by definition is two's compliment is it not? I could be wrong but I thought the two were identical.

    No.  binary means 'base 2'.  Two's compliment is how negative numbers are represented.  Two's compliment is pretty universal, though.

    Andy:
    If I am correct then any integer &'d by 1 should return 1. Is this not correct?

    I suppose there could be ways of representing negatives where odd numbers did not have a 1 in the first (lowest) bit.  I don't know why that would be used over two's compliment as it is so easy to work with.

  • (cs) in reply to ironclay

    Anonymous:
    so why right it yourself?

    Right, left, up, down it's all the same to me.

  • (cs) in reply to ironclay

    "If I am correct then any integer &'d by 1 should return 1. Is this not correct?"-Andy

    Sorry that should have been "any odd integer"

    To the other poster thanks for clearing up the "two's compliment" thing for me. I wasn't aware of the negative numbers thing. So to be truly portable it would have to be:
    unsigned int isOdd(unsigned int i){
        return i & 1;
    }

  • (cs) in reply to Andy

    You could use the absolute value with signed integers too.

  • Anonymous Coward (unregistered)
    function isOdd(i)
    {
        if(i = 1)
           return true
        else
           return !isEven(i-1)
    }

    function isEven(i)
    {
        if(i = 1)
           return false
        else
           return !isOdd(i-1)
    }


    I actually saw someone make serious use of this method to determine if a number was odd or not.  Of course, they were using it to teach the concept of co-recursion.
  • (cs) in reply to LinuxUser
    Anonymous:
    You know, most programming can be done without much understanding of math...
    And a good thing too, or I would be flipping burgers.
  • (cs) in reply to Anonymous Coward
    Anonymous:
    I actually saw someone make serious use of this method to determine if a number was odd or not.  Of course, they were using it to teach the concept of co-recursion.


    Ok, that's an acceptable use of that approach, so long as it is clear that you wouldn't actually do it that way in practice. I have to admit that it has an almost Lisp-ish feel to it, which appeals to me. Though of course both CL and Scheme actually have even[p|?] and odd[p|?] predicates, unlike most languages for some reason...

    BTW, depending on the language this is supposed to (this looks like Javascript to me), you may have a typo in your version; you used '=' for the comparison instead of the '==' most C-derived languages use. HTH.
  • Anonymous Coward (unregistered) in reply to Schol-R-LEA
    Schol-R-LEA:
    Anonymous:
    I actually saw someone make serious use of this method to determine if a number was odd or not.  Of course, they were using it to teach the concept of co-recursion.


    Ok, that's an acceptable use of that approach, so long as it is clear that you wouldn't actually do it that way in practice. I have to admit that it has an almost Lisp-ish feel to it, which appeals to me. Though of course both CL and Scheme actually have even[p|?] and odd[p|?] predicates, unlike most languages for some reason...

    BTW, depending on the language this is supposed to (this looks like Javascript to me), you may have a typo in your version; you used '=' for the comparison instead of the '==' most C-derived languages use. HTH.


    The person in question used the following example to demonstrate why recursion is not always a good solution:
    function fibonacci(i)
    {
        if(i = 1 or i = 2)
            return 1
        else
            return fibonacci(i-1) + fibonacci(i-2)
    }


    It's written in the C-like pseudocode I use for explaining algorithms.  I use '=' for both assignment and equality, since it's easily understood by most people.

    (BTW: someone needs to fix the CAPTCHA: it's giving me mustard-yellow text on a mustard-brown background)
  • (cs) in reply to Anonymous Coward

    Ah, yes, that lovely O(n^2) tree recursion effect, compared to that sad, tawdry little O(n) iterative version. 8o I imagine memoization is out of the question? I thought so. 8-)

  • Anonymous Coward (unregistered) in reply to Schol-R-LEA

    It's worse than O(n ^ 2).  Recursive calculation of the Fibonacci numbers is O(2^n).

    Personally, my favorite method of calculating Fibonacci numbers is

    function fibonacci(i)
    {
        return (((1 + sqrt(5)) ^ i) - ((1 - sqrt(5)) ^ i)) / (sqrt(5) * (2 ^ i))
    }

  • Mike (unregistered) in reply to Anonymous Coward

    I hope you round it as well :-)

    Would be interesting to see where the performance cut-off is between your version and a quick iterative version. Obviously depends very much on whether or not your compiler spots sqrt(5) as a constant.....

  • (cs) in reply to mxksweeb
    mxksweeb:
    Anonymous:
    You know, most programming can be done without much understanding of math...
    And a good thing too, or I would be flipping burgers.


    You guys do realize that poor math skills is one reason for  many of the WTF's on this board, right?  Taking a class in logic and a class in algebra should be minimum requirements for a Computer Science degree....  oh wait, they are at most universities!

    (It's also unforunate that they don't stress higher math and computer architecture to Computer Scientists like they do with Computer Engineers, but I shall step off my high horse now.)
  • (cs) in reply to Charles Nadolski
    Charles Nadolski:
    You guys do realize that poor math skills is one reason for  many of the WTF's on this board, right?  Taking a class in logic and a class in algebra should be minimum requirements for a Computer Science degree....  oh wait, they are at most universities!


    Actually, if you ask me the problem is because computers don't come with a minimum skill requirement from the user. If you want to drive a car, you need a drivers license. But to operate a computer you don't need a computer license.
    Still, that wouldn't be a problem if it wasn't for all those morons who happen to own a computer for two months and think they can start writing all kinds of software for it now. They just start to educate themselves, without even botherig about getting any degree in any computer-related study because that costs money and after half a year or so they start looking for a job as programmer...

    And companies are sometimes just desperate enough in their search for new programmers that they're willing to even hire those morons. Why? Because even a bad products is sometimes better than no product. No product means no sales and thus no future. Bad products predict pad sales and a bad future, but hey... At least you have a future... [:D]
  • Anonymous (unregistered) in reply to mugs
    mugs:
    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?

    And apparently their dictionary lacks words like "eating", or has a really poor sort going on.
  • (cs) in reply to Schol-R-LEA
    Schol-R-LEA:
    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>


    I'm tempted to suggest IsEven() be written as
    <font size="4">Function IsOdd(n as Integer) as Boolean
        IsOdd = Not IsEven(n)
    End Function</font>
    Ah... bug-free code...
  • (cs) in reply to Maurits

    Mmm, circular defiinitions... Yay for infinite loops!

  • (cs) in reply to Charles Nadolski
    Charles Nadolski:
    Mmm, circular defiinitions... Yay for infinite loops!


    Especially useful in team programming.  One person writes IsEven, the other IsOdd - each can comfortably reflect on how clever they were to reuse existing code...
  • (cs) in reply to Andy

    To check to see if number N is odd;

    1) Populate an array with N items (doesn't matter what the items are)
    2) Start to randomly remove pairs of items from the array
    3) If you end up with a lonely item, then N is odd
    4) You're odd if you take this seriously

  • (cs)

    Perhaps there is a pre-disposed suspicion of the even-ness of the number N. Oddly, in the world of numbers "even" has the upper-hand if you believe zero is an even number. That is to say: odds are greater by one (an odd) that the number is even if the range of numbers is odd and includes zero. Likewise, the odds are even ( that the number is odd or even ) if the range begins with an odd and the range is even.

    It should be noted as significant that these two functions may only be testing the last digit of any (base ten) number. In which case all bets are even... unless you apply a bell-shaped curve of probability for random-ness of numbers 0to9 vs 1to10. in which case the mid-point vacillates between even and odd based on the oddity of the minimum number.

    Given this complexity of task, it's no wonder the author created two functions.

  • Skeptic (unregistered) in reply to olddog

    I have seen another solution in my time. I can't remember the *exact* syntax because it was very dependant on casting in VB.NET;

    If (n / 2.0) = (n / 2) Then

    ' Is Even

    Else

    ' Is False

    EndIf

    Essentially, if the floating point result of division by 2.0 is equal to the integer divide by 2 the number must be even. It worked a treat, but was promptly replaced by something less... insane.

  • GrouchyAdmin (unregistered)

    ...he knows about MOD, but doesn't just MOD 2... and has TWO functions? What the crap?

  • A Truly Selective Programmer (unregistered) in reply to dubwai
    dubwai:
    No.  binary means 'base 2'.  Two's compliment is how negative numbers are represented.  Two's compliment is pretty universal, though.

    First, it's complement. Compliments are for your significant other half or whatever the politically correct term of the hour is.

    Second, I cut my teeth on a machine with one's complement. So they are out there.

    Third, use binary operators when doing binary operations. Use mathematical operators when doing mathematical operations. Getting the remainder of a division is a mathematical operation, so you should write n % 2 (at least in C). If your compiler is worth its money, it'll know whether it can optimize this to testing the least-significant bit on the target platform. You don't have to do the compiler's work, and your code says what you mean (and does what you mean, too!). It's called maintainability.

  • Paul Neumann (unregistered)

    Nearly 7 years and no one has yet pointed out that ALL examples are wrong? 0 is defined as neither odd nor even!

  • suscipit (unregistered) in reply to Paul Neumann
    Paul Neumann:
    Nearly 7 years and no one has yet pointed out that ALL examples are wrong? 0 is defined as neither odd nor even!
    Nearly two years later, but I can't let this go unchallenged.

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

Log In or post as a guest

Replying to comment #:

« Return to Article