- 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
Admin
Admin
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.
Admin
I also like the fact he has 2 functions: idOdd() and isEven().
I wonder if he will call isOdd() when isEven() returns false :P
Admin
Another likely solution:
n And 1 ' Remember, Folks, Bit 0 makes an odd number [;)]
Admin
He could have converted the number to a string to get the last digit.
Admin
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.
Admin
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 :))
Admin
You're thinking of Captchas.
Try here: http://www.15seconds.com/issue/040202.htm
Admin
In case it wasn't obvious, the bolded part was meant to be sarcastic. [:)]
Admin
lol - same thing I thought...WhyTF is he/she using mod 10?!?[:|]
Admin
It will return True, since he/she's casting N to an int 4.2 will become 4.
Admin
Admin
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...
Admin
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 [:^)]
Admin
"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
Admin
IF NOT IsEven(N) AND NOT IsOdd(N) THEN
Call IM_AN_IDIOT()
End if
Admin
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]
Admin
Open that link and do a search for "Figure: dictionarydb". A bonus WTF for today!
Admin
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.
Admin
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.
Admin
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.
Admin
>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 [:)]
Admin
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
Admin
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.
Admin
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.
Admin
What? doesnt this code work? At least he didnt convert it to a string first. What else can you ask for?
Admin
<font size="4">
</font>Admin
Disclaimer: This was posted on Usenet, so it doesn't meet the requirements for a WTF submission.
Admin
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>
Admin
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!
Admin
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."
Admin
Good that he did it with 10, iimagine what should have happened if it was 100.
Admin
<FONT face=Tahoma size=2>Hmmm, and what about negatives?</FONT>
Admin
Another solution:
Find all prime factors of N and check if 2 is one of them.
Admin
Haha... wtf would they do that?
Admin
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.
Admin
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
or
either of which would be easier to code and understand than this. And of course, the obvious way to write IsEven() would be
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.)
Admin
Or indeed, mod 2...
Admin
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.
Admin
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.
Admin
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?
Admin
Now that takes the cake! It's a little hard to believe someone who can think recursively would write this as serious code, though.
Admin
That just makes me shudder [:|]
Admin
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.
Admin
"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.
Admin
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.
Admin
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:
Maybe I'm missing something though. What system would that not port to?
Admin
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>
Admin
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?