- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
that's what mod does, so why right it yourself?
Admin
Wrong.
it does a bit-wise comparison of each bit in the integer.
0000 0000
0000 0001
this will return 0.
Admin
No. binary means 'base 2'. Two's compliment is how negative numbers are represented. Two's compliment is pretty universal, though.
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.
Admin
Right, left, up, down it's all the same to me.
Admin
"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;
}
Admin
You could use the absolute value with signed integers too.
Admin
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.
Admin
Admin
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.
Admin
The person in question used the following example to demonstrate why recursion is not always a good solution:
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)
Admin
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-)
Admin
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))
}
Admin
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.....
Admin
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.)
Admin
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]
Admin
Admin
I'm tempted to suggest IsEven() be written as
Ah... bug-free code...
Admin
Mmm, circular defiinitions... Yay for infinite loops!
Admin
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...
Admin
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
Admin
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.
Admin
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.
Admin
...he knows about MOD, but doesn't just MOD 2... and has TWO functions? What the crap?
Admin
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.
Admin
Nearly 7 years and no one has yet pointed out that ALL examples are wrong? 0 is defined as neither odd nor even!
Admin