Computer Science Canada Odd or Even Integer? |
Author: | kaotickid [ Tue Mar 18, 2008 8:43 pm ] | ||
Post subject: | Odd or Even Integer? | ||
I'm wondering if there's a code out there to distinguish between an odd and even integer. This is a problem from the Turing .PDF (Chapter 14, Question 17), it's pretty much done but I just need to put the code that distinguishes whether an integer is even or odd. Thanks ![]()
|
Author: | CodeMonkey2000 [ Tue Mar 18, 2008 8:47 pm ] |
Post subject: | RE:Odd or Even Integer? |
Use the modulus operation( the % sign). It gives the remainder of two integer. (eg 3%2 =1 since 2*2=2 and we have a remainder of 1). |
Author: | A.J [ Tue Mar 18, 2008 8:48 pm ] | ||
Post subject: | Re: Odd or Even Integer? | ||
there's a function in math called 'mod' thats also recognized in turing. x mod y gives the remainder when x is divided by y. so 17 mod 5 will give 2, since 17/5= 3 remainder 2. so for distinguishing between odd and even, just take the number mod 2. if it is 0 (anything divisible by 2 always gives 0, since there isn't any remainder), its even. Otherwise if it is 1 (if it is not divisible by 2 then it has a remainder of 1), its odd! so:
Hope this helped ![]() A.J EDIT: CodeMonkey2000 beat me to it. Oh darn ![]() |
Author: | kaotickid [ Tue Mar 18, 2008 8:52 pm ] |
Post subject: | Re: Odd or Even Integer? |
Haha A.J, but thanks to both of you, A.J and CodeMonkey ![]() Well my questions is answered ![]() |
Author: | Nyrd [ Tue Mar 18, 2008 8:54 pm ] | ||
Post subject: | Re: Odd or Even Integer? | ||
That should do it ![]() In case you're wondering, "mod" is the modulo function. It essentially finds the remainder when you divide the number by 2 (in this case). --Remember, if you divide a number by 2 and there is no remainder it must be even!-- |
Author: | Nyrd [ Tue Mar 18, 2008 8:56 pm ] |
Post subject: | Re: Odd or Even Integer? |
Wow, I was beat to it by two people . . . For shame . . . |
Author: | agnivohneb [ Tue Mar 18, 2008 9:07 pm ] | ||||
Post subject: | Re: Odd or Even Integer? | ||||
A.J @ Tue Mar 18, 2008 8:48 pm wrote:
You could also use the div function and instead of equaling 0 it equals 1.
|
Author: | HeavenAgain [ Tue Mar 18, 2008 9:15 pm ] | ||
Post subject: | Re: RE:Odd or Even Integer? | ||
CodeMonkey2000 @ Tue Mar 18, 2008 9:47 pm wrote: (eg 3%2 =1 since 2*2=2 and we have a remainder of 1).
2*2 = 2? ![]() agnivohneb @ Tue Mar 18, 2008 10:07 pm wrote: You could also use the div function and instead of equaling 0 it equals 1.
yes you could use div to check if its odd or even, but in your example .... well, you should take another look at it
|
Author: | agnivohneb [ Tue Mar 18, 2008 9:21 pm ] |
Post subject: | RE:Odd or Even Integer? |
ummmmm ... think ... think ... I give up! |
Author: | richcash [ Tue Mar 18, 2008 9:24 pm ] | ||
Post subject: | Re: Odd or Even Integer? | ||
Bitwise 'and' is a bit faster than modulus I think.
agnivohneb, try putting in any even number other than 2 in your code. See the problem? |
Author: | Saad [ Tue Mar 18, 2008 9:28 pm ] | ||
Post subject: | Re: Odd or Even Integer? | ||
If you want to be crazy and go with some mutual recursion ![]()
|
Author: | agnivohneb [ Tue Mar 18, 2008 9:29 pm ] |
Post subject: | RE:Odd or Even Integer? |
Wait! ... Ha! I got it. I reject my last comment. I know what the problem is now. It is not accurate. |
Author: | A.J [ Tue Mar 18, 2008 9:42 pm ] |
Post subject: | Re: Odd or Even Integer? |
nice richcash ![]() bits are the way to go. as for you saad.......lets just say forget about mutual recursion for now, shall we ![]() |
Author: | CodeMonkey2000 [ Wed Mar 19, 2008 2:56 pm ] |
Post subject: | RE:Odd or Even Integer? |
Wow talk about overkill....... |