Comment On IsOdd()? Yes, Is Very Odd.

I'm really at a loss of words over this code that Scott Wickham sent. I honestly can't think of anything to possibly say to augment this very, very odd way of checking for odds and evens, so here goes ... [expand full text]
« PrevPage 1 | Page 2Next »

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 12:35 • by Your Name
LastDigit = mod(N) mod 10

isn't that supposed to be int(N)?

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 12:40 • by Your Name
31458 in reply to 31456
wtf, the forum screws up quotes now?

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 12:45 • by 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.

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 12:49 • by StarLite
31460 in reply to 31459
I also like the fact he has 2 functions: idOdd() and isEven().

I wonder if he will call isOdd() when isEven() returns false :P

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 12:54 • by Mike R
31463 in reply to 31460

Another likely solution:


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


 

Could be worse

2005-03-21 13:07 • by Brent
31465 in reply to 31463
He could have converted the number to a string to get the last digit.

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 13:09 • by rogueRPI
31466 in reply to 31463

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.

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 13:17 • by mugs
31467 in reply to 31466

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 :))

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 13:21 • by Tom Mathews

You're thinking of Captchas.


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

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 13:27 • by mugs
31469 in reply to 31467
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. [:)]

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 13:55 • by skicow
31473 in reply to 31459

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?!?[:|]

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 13:58 • by skicow
31474 in reply to 31466

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.

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 14:18 • by znaps
 I see the WTF! For isOdd(N) he should have returned !isEven(N), right ?!? right?!?!

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 14:20 • by misses the point
31477 in reply to 31473
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...



Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 14:21 • by rogueRPI
31478 in reply to 31474
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 [:^)]

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 14:23 • by Jimmy James
31479 in reply to 31459

"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

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 14:30 • by Sweets
IF NOT IsEven(N) AND NOT IsOdd(N) THEN

       Call IM_AN_IDIOT()

End if



Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 14:33 • by skicow
31481 in reply to 31477
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]

Aside: Captchas

2005-03-21 14:40 • by Anonymous
31482 in reply to 31468

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

Re: Aside: Captchas

2005-03-21 14:42 • by Anonymous
31483 in reply to 31482

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.

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 16:00 • by Steve
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.

Re: Aside: Captchas

2005-03-21 16:18 • by misses the point
31493 in reply to 31482
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.



Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 16:19 • by evnafets
31494 in reply to 31492

>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 [:)]

Re: Aside: Captchas

2005-03-21 16:24 • by evnafets
31495 in reply to 31493
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

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 16:27 • by Anonymous

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.

Re: Aside: Captchas

2005-03-21 17:01 • by SwitchBL8
31501 in reply to 31495
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.

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 17:03 • by TopCod3r
What? doesnt this code work? At least he didnt convert it to a string first. What else can you ask for?

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 17:38 • by SnipeZ
Is there a function IsEvenOdder() ?
Though I can't imagine that's possible.


Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 18:12 • by Anonymous Coward
I've seen worse:

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

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

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 19:14 • by Andre
31511 in reply to 31509
Nope, it can still get worse...



<?php
function IsEven($num)
{
  if ($num < 0)
     return IsEven(-$num);
  if ($num > 1)
     return IsEven($num-2);
  if ($num == 1)
     return 0;
  else return 1;
}
?>



(from thephpwtf.com ...)




Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 19:49 • by foxyshadis
31515 in reply to 31511
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!

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 20:27 • by Anonymous
31518 in reply to 31468
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."

Re: IsOdd()? Yes, Is Very Odd.

2005-03-21 20:43 • by prakash
Good that he did it with 10, iimagine what should have happened if it was 100.



Re: IsOdd()? Yes, Is Very Odd.

2005-03-22 03:31 • by Welcome To The Machine
Hmmm, and what about negatives?

Re: IsOdd()? Yes, Is Very Odd.

2005-03-22 08:10 • by Not anonymous any more

Another solution:


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

Re: IsOdd()? Yes, Is Very Odd.

2005-03-22 08:30 • by mugs
31538 in reply to 31518
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?

Re: IsOdd()? Yes, Is Very Odd.

2005-03-22 10:34 • by Manni

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.

Re: IsOdd()? Yes, Is Very Odd.

2005-03-22 12:04 • by Schol-R-LEA
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



Function IsOdd(n as Integer) as Boolean
    IsOdd = CBool(n mod 2)
End Function


or



Function IsOdd(N as Integer) as Boolean 
    IsOdd = CBool(n And 1)
End Function



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




Function IsEven(n as Integer) as Boolean
    IsEven = Not IsOdd(n)
End Function



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.)

Re: IsOdd()? Yes, Is Very Odd.

2005-03-22 12:05 • by Walter
31544 in reply to 31477


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...



Re: IsOdd()? Yes, Is Very Odd.

2005-03-22 13:01 • by GalacticCmdr
31550 in reply to 31544
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.

Re: IsOdd()? Yes, Is Very Odd.

2005-03-22 17:15 • by Stan Rogers
31570 in reply to 31550
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.

Re: IsOdd()? Yes, Is Very Odd.

2005-03-22 19:11 • by wsargent
31573 in reply to 31476
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?

Re: IsOdd()? Yes, Is Very Odd.

2005-03-22 20:21 • by sas
31576 in reply to 31511
Anonymous:
Nope, it can still get worse...



function IsEven($num)
{
  if ($num < 0)
     return IsEven(-$num);
  if ($num > 1)
     return IsEven($num-2);
  if ($num == 1)
     return 0;
  else return 1;
}
?>



(from thephpwtf.com ...)





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

Re: IsOdd()? Yes, Is Very Odd.

2005-03-22 21:03 • by Rescendent
That just makes me shudder [:|]

Re: IsOdd()? Yes, Is Very Odd.

2005-03-23 01:36 • by Pax
31584 in reply to 31463
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.

Re: IsOdd()? Yes, Is Very Odd.

2005-03-23 01:41 • by Pax
31585 in reply to 31467

"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.


 

Re: IsOdd()? Yes, Is Very Odd.

2005-03-23 03:09 • by LinuxUser
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.

Re: IsOdd()? Yes, Is Very Odd.

2005-03-23 13:19 • by Andy
31619 in reply to 31543
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?

Re: IsOdd()? Yes, Is Very Odd.

2005-03-23 13:21 • by Andy
31620 in reply to 31619
Wow that code tag really jacked up my formatting. That should be(second try):


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

}


Re: IsOdd()? Yes, Is Very Odd.

2005-03-23 13:28 • by Andy
31622 in reply to 31620
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?

« PrevPage 1 | Page 2Next »

Add Comment