- 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
This is not the worst way I ever heard of to determine if a number is odd or even.
That prize is awarded to a jolly chap whose meanderings during an interview were described to me by the interviewer.
Great, O(n) to find if n is odd... (Depending on the divide support in the CPU, the conventional method might be as heavy as O(log2(n)).)
Admin
let us follow isOdd(2)
unsigned counter = 0; while( 1 ) if( counter == number ) return false; # 0==2
while( 1 ) # second round through the while loop if( counter == number ) return false; # 1==2
2 is odd
Admin
I can't tell if you are being sarcastic or not. Its amazing the amount of wtf's in wtf comments.
I quite like that answer for isOdd, shows creativity, to hell with efficiency.
Admin
Hell no, for ints all you do is check the least significant bit and you're done! It's a constant time check - it really couldn't be easier. Having said that I have no idea if (x % 2 == 0) would be compiled to that if that is the mechanism you refer to.
Admin
Do I get a 🍪 now?
Admin
Edit: And cyborg beat me by 2 minutes! But his answer wasn't there when i pressed the quote-button!
Admin
Do I get a 🍪 now? I hate the fact that I always want to write in C-like Syntax even though I currently work with VB.NET at work. ARRRGH SEMICOLONS
Admin
I was actually following the code of #436854 from stevethecynic. Manually following some code is a great way to get inspired in getting more ways to make it fail.
His code will actually work if it has a "counter++;" before the end of the while(1) loop,
Admin
Seriously, the modulo operator is overrated, isn't it? ;-)
Admin
Yeah, and here he could have just done a binary search in the range [0,n] for an integer that, when multiplied by 2 gives the number in question...and if the search fails..well...then it's odd right? That would be O(log2(n)) for sure...much faster ;-)
Admin
It looks like someone forgot a "counter ++" at the bottom of the while loop (between the comparisions)
Admin
Admin
Admin
Also doesn't work for negative numbers.
Admin
For websites there is only one correct way to implement zebra-striping:
With a CSS selector: nth-child(2n)!
That is the Discourse way, and of course Discourse is doing it right (tm)!
Admin
Admin
Admin
This reminds me of one old joke:
Three C coders write a function that takes an integer argument between 0 and 10 and returns 1 if argument is odd, 0 if it is even.
Newbie coder writes this:
Second, more experienced coder writes this:
Third one, the most experienced of them, writes this:
Admin
Good thing it only takes unsigned numbers.
Admin
Admin
Admin
Admin
Of course, I have seen a truly awful compiler, one that even on its most aggressive optimising settings would compile (on 8088/86) this:
to this: At the time, the version we had was 8 years old. It was still the live release version, and was still being sold. We changed to a different compiler (IIRC the new one was Aztec C) and boosted (software) floating point performance by a factor of three and reduced code size by about a fifth.Curiously on Aztec C I compiled some code that contained this line:
(where inportb() returns unsigned char) and found that it generated a mov instruction instead of a shift or multiply for the times-256...Admin
Admin
So you might be inclined to say that I'm TRWTF, and today, based on my performance here, I'd be inclined to agree.
Admin
WTF is happening to The Daily WTF? Only stories from more than a decade past to be found here nowadays?!
Captcha: saepius. Latin saepe often, hence "more often" - fits well to this comment.
Admin
Admin
Obviously you're all wrong. It should be
because there's not a problem that can't be solved with recursion! It's the functional way!Admin
To be fair change the language locale is a bad id, locale is for input and output.
It might seems like a good idea to optimise and not have those I/O conversion layers but try making a large system without a uniform internal representation especially when it has to talk with other systems (no outer conversion layer necessitates an inner conversion layer).
Admin
I know, right? Everyone knows the correct answer is to take the number and post it through an API endpoint to a digital camera positioned above a wooden table. The point of the camera is to abuse the ALU for the odd/even logic.
The output is then sent to a scanner which, for security reasons, has a wooden table on it. The table is situated to fall on anyone who attempts to disturb the scanner and make a great noise alerting people to the fact in the process.
The scanner then forwards the now-secure output to the interwebs, where it will be implanted in a photo of a wooden table for steganographic purposes.
Finally it is returned to the rendering algorithm to dictate the colour of the row (which will either be beech or oak) and judged fit-for-purpose by Heath Robinson, once it has been decoded.
Admin
Hey my son is a compiler that isn't able to optimise i % 2 to i & 1, and let me tell you, it's no laughing matter!
Admin
Formats are hard.
Reminds me of something I built in Access back when my simple time format was hh:mm. I wrote VB that made time boxes, on entry, highlight the first digit, and then proceed to overwrite digits as you entered them. It worked great.
Fast forward to a computer upgrade which used a simple time format of h:mm. Needless to say, the VB totally failed and now, it clears the whole entry on your first keystroke (same as it would if you tabbed into the box normally, which was what I wanted to avoid).
No biggie, I figure... just change the format code for that box to hh:mm.
No can do.
It changes hh:mm to "Simple time". Simple time is h:mm.
I tried without luck to change it programmatically and finally gave up because typing the minutes portion is easier than trying to force Access to do what I want. And I'm not about to change the simple time format just because Access doesn't understand.
Admin
I would like to see Louis's implementation of FizzBuzz. inb4 people start posting fizzbuzz code
Admin
Admin
Admin
No triple number is prime except 3.
Really it's only the idea there's something special about "even" and "odd" beyond the fact every even number is a multiple of 2 that makes this an "odd" fact which it isn't given something very odd indeed would be going on if somehow a multiple of a prime - 2 - was also a prime given that kind of breaks the entire definition.
Admin
Nice try, but no. "zebra striping" existed well before 2001. It actually started on gasp stationary which is commonly referred to as "greenbar continuous stationary", "greenbar continuous feed", or just "greenbar".
As for why Fred never cared for it, to each their own. Many people find it a simple, clear way of delineating between rows so that you can follow them across the table somewhat more easily than if you just rely on borders.
Admin
Silly! It's obvious that the string will be longer (by one or two characters) than the original number if the original is odd, and shorter or the same length if the original is even.
Regardless of the decimal separator character!
Admin
use Lingua::EN::Numbers qw(num2en); sub isodd {return (num2en shift) =~ /e.?$/}
Admin
Admin
and the experienced one gets a kick in the nuts for providing the heaviest and the slowest solution.
The 2 obvious solution get compiled in 1 and assembly instruction, the other one needs the load of an address (4 byte immediate or 8 byte immediate on a 64 bit machine) and a load base+index instruction, which has the side effect of loading the lookup_table into the cache which might evict more precious data. It annoys me always when algorithm are compared in a imaginary machine that knows nothing about registers, caches (lvl 1, 2 or 3), local memory, remote memory (on multi socket machines) etc.
Admin
Admin
works 99% of the time - so good enough for all practical purposes
captcha: damnum
Admin
Admin
Cann't there be a 1000-seperator when you convert it to string? ( "1,000" as in 1000 )?
Admin
Yes, I know, but I am an humourless git.
Admin
You don't need a double cast...
bool isOdd(int n) { return n >= 0 ? (n/2 == (n-1)/2) : isOdd(-n);
}
Which, unlike the (n & 0x1) or the (n % 2) solution, actually guarantees[0] the correct answer for n < 0. C is not a two-complement language, even though it kinda is.
[0] Well, nearly. isOdd(INT_MIN) will recurse infinitely. Yay, C.
Admin
To be accurate, every frickin' computer installation back in the days of batch accounting programs used green-bar or grey-bar to aid in making sense of tabulated output. That was the default form loaded on the printer, thus every print job used it. I would say that qualifies as "all the rage".
With emphasis on "rage" whenever the printer jammed.
My name is 'Tristique' and I am a young college graduate who utterly rejects the idea that anything existed before I was born. Including grey-bar continuous forms.
Admin
You don't need a double cast...
bool isOdd(int n) { return n >= 0 ? (n/2 == (n-1)/2) : isOdd(-n); }
Which, unlike the (n & 0x1) or the (n % 2) solution, actually guarantees[0] the correct answer for n < 0. C is not a two-complement language, even though it kinda is.
[0] Well, nearly. isOdd(INT_MIN) will recurse infinitely. Yay, C.
Admin