# SyntaxBomb - Indie Coders

## Languages & Coding => Blitz Code Archives => Miscellaneous => Topic started by: RemiD on October 21, 2019, 22:03:41

Title: determine if an integer value is odd or is even
Post by: RemiD on October 21, 2019, 22:03:41
needed this today, and i knew that i had the functions on my harddrive, somewhere :
Code: [Select]
`;determine if an integer value is odd or is even 20160101Graphics3D(640,480,32,2)SeedRnd(MilliSecs())For i% = 0 To 101 R1% = IsOdd(i) R2% = IsEven(i) DebugLog("i = "+i) DebugLog("IsOdd ? "+R1) DebugLog("IsEven ? "+R2) WaitKey()NextEnd()Function IsOdd(TInt%) R% = Abs(TInt) Mod 2 If(R = 0)  Return False ElseIf(R = 1)  Return True EndIfEnd FunctionFunction IsEven(TInt%) R% = Abs(TInt) Mod 2 If(R = 0)  Return True ElseIf(R = 1)  Return False EndIfEnd Function`
Title: Re: determine if an integer value is odd or is even
Post by: Steve Elliott on October 21, 2019, 22:09:45
I always think it's tidier/less code to not use else in cases when there are only 2 possibilities.

Code: [Select]
`Function IsOdd(TInt%) R% = Abs(TInt) Mod 2 If(R = 0)  Return False Endif Return TrueEnd Function`
Title: Re: determine if an integer is odd or even
Post by: STEVIE G on October 21, 2019, 22:10:12
Why not use one simple function ... if it isn't even it's odd.  ;D

Code: [Select]
`Function IsEven( I% ) Return ( Abs(I) Mod 2 ) = 0End Function`
Title: Re: determine if an integer value is odd or is even
Post by: Steve Elliott on October 21, 2019, 22:30:45
Yes you could re-write it completely, simplify even more and use a not if required.

Code: [Select]
`If( Not IsEven( num ) )  ; odd number`
Title: Re: determine if an integer value is odd or is even
Post by: TomToad on October 21, 2019, 23:26:34
If number & 1
Number is odd
Else
Number is even
Title: Re: determine if an integer value is odd or is even
Post by: STEVIE G on October 22, 2019, 00:50:29
If number & 1
Number is odd
Else
Number is even

;D I knew there must be a bit shift variant.
Title: Re: determine if an integer value is odd or is even
Post by: Steve Elliott on October 22, 2019, 08:00:10
Haha, nice one.  I love these little efficiencies.   ;D
Title: Re: determine if an integer value is odd or is even
Post by: RemiD on October 22, 2019, 12:06:31
i think that you should spend less time trying to optimize a procedure which is already fast enough (it takes 0.00012 millisecond so 0.12 microsecond to complete the procedure on my 8 years old, low end, laptop)
Title: Re: determine if an integer value is odd or is even
Post by: Steve Elliott on October 22, 2019, 12:13:04
a) I think you should be grateful for some neat advice and not instead sulk.  And b) that's a lazy attitude to coding, these little efficiencies all add-up.  No excuse for ugly bloaty routines when you were given some good advice, and like you mentioned you can store and re-use them again.

Thanks guys, I for one enjoyed your input.
Title: Re: determine if an integer value is odd or is even
Post by: RemiD on October 22, 2019, 17:00:58
thanks for teaching me how to behave and programming "Steve Elliott", i feel better now. i am really grateful to you. :P
Title: Re: determine if an integer value is odd or is even
Post by: Steve Elliott on October 22, 2019, 20:51:52
lol personally I love to learn.  If somebody comes up with a better solution than myself I love that!  Because I've learnt something extra...Rather than tell people they are wasting their time and now come up with a sarcastic remark.
Title: Re: determine if an integer value is odd or is even
Post by: RemiD on October 23, 2019, 18:11:19
@steve elliott>>you are too smart for me, i give up :-*
Title: Re: determine if an integer value is odd or is even
Post by: TomToad on October 23, 2019, 21:24:04

Code: [Select]
`Function isEven(num%) If num Shr 1 Shl 1 = num Then Return True Return FalseEnd FunctionFunction isOdd(num%) If num Shr 1 Shl 1 <> num Then Return True Return FalseEnd FunctionFor i = 0 To 100 If isEven(i) Print i+" is even" ElseIf isOdd(i) Print i+" is odd" Else Print i+" is nothing" EndIfNext`
Code: [Select]
`Function isEven(num%) If ((num - 1) Xor num) <> 1 Then Return True Return FalseEnd FunctionFunction isOdd(num%) If ((num - 1) Xor num) = 1 Then Return True Return FalseEnd Function`
Code: [Select]
`Function isEven(num%) For i = -2147483648 To +2147483647 Step 2 If num = i Then Return True Next Return FalseEnd FunctionFunction isOdd(num%) For i = -2147483647 To +2147483647 Step 2 If num = i Then Return True Next Return FalseEnd Function`
Title: Re: determine if an integer value is odd or is even
Post by: Derron on October 23, 2019, 21:28:38
Code: [Select]
`Function isEven(num%) For i = -2147483648 To +2147483647 Step 2 If num = i Then Return True Next Return FalseEnd FunctionFunction isOdd(num%) For i = -2147483647 To +2147483647 Step 2 If num = i Then Return True Next Return FalseEnd Function`

Ohh no ... seems to contain a mistake. Else I would prefer these functions as I see they check all potential candidates. Thanks for the laugh :)

bye
Ron
Title: Re: determine if an integer value is odd or is even
Post by: Qube on October 23, 2019, 22:56:30
Code: [Select]
`Function isEven(num%) For i = -2147483648 To +2147483647 Step 2 If num = i Then Return True Next Return FalseEnd FunctionFunction isOdd(num%) For i = -2147483647 To +2147483647 Step 2 If num = i Then Return True Next Return FalseEnd Function`
That's my boy! true brute force at it's best ;D ( even with a bug )
Title: Re: determine if an integer value is odd or is even
Post by: Steve Elliott on October 23, 2019, 23:00:56
lol  :o
Title: Re: determine if an integer value is odd or is even
Post by: TomToad on October 24, 2019, 09:07:02
Ohh no ... seems to contain a mistake.

That's my boy! true brute force at it's best ;D ( even with a bug )

I don't see the bug.  Tried replacing the For loops with
For i = -648 to +647 step 2
For i = -647 to +647 step 2
So that I wouldn't have to wait a month for a result, and the values return correct results.

Edit: Here's another way to determine if a number is odd or even
Code: [Select]
`Function isEven(num%) If Instr("02468",Right(Str(num),1)) > 0 Then Return True Return FalseEnd FunctionFunction isOdd(num%) If Instr("13579",Right(Str(num),1)) > 0 Then Return True Return FalseEnd Function`
Rube Goldberg use to find complex ways of solving simple problems.  I guess this is the programmer's version of it? :D

Title: Re: determine if an integer value is odd or is even
Post by: Derron on October 24, 2019, 09:30:14
Yepp, no bug - just compared the numbers ("- ...8 to ..." and "- ...7 to ..." and did not grasp the "roll over" at "2^31 -1" +1).

See - your algorithm was already over my head, so it must be the perfect one :)

bye
Ron
Title: Re: determine if an integer value is odd or is even
Post by: iWasAdam on October 24, 2019, 10:48:21
or...
Code: [Select]
`method IsEven:bool( input:int )  local check:float = input / 2  return int(check) = checkend method`
@Qube - Brilliant!  :))
Title: Re: determine if an integer value is odd or is even
Post by: Derron on October 24, 2019, 11:23:04
"int/2" = int (at least in BlitzMax)

so better write "int/2.0" to return the float value.

Yet your
Code: [Select]
`method IsEven:bool( input:int )  local check:float = input / 2.0  return int(check) = checkend method`might even be faster than the modulo :)

alternative to yours:
Code: [Select]
`method IsEven:bool( input:int )  return int(input / 2.0)*2 = inputend method`

Back to serious mode:
https://stackoverflow.com/questions/2229107/what-is-the-fastest-way-to-find-if-a-number-is-even-or-odd

wich exposes that "number & 1" might fail on "one complements" computers - and that "number % 2" could be optimized correctly by the final compiler then ...

And the thread there contains another funny option - recursion:
Code: [Select]
`Function IsOdd:int(n:int)   if n = 0       return 0   elseif n = 1      return 1   else      return not IsOdd(n - 1)   endifEnd Function`

bye
Ron
Title: Re: determine if an integer value is odd or is even
Post by: RemiD on October 24, 2019, 17:45:36
it seems that you have the topic for the next competition :
each participant much create 2 procedures to do something,
one very optimized and minimalist (and fast)
one very bullshity and bulky (and slow)
;D
SimplePortal 2.3.6 © 2008-2014, SimplePortal