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

needed this today, and i knew that i had the functions on my harddrive, somewhere :
;determine if an integer value is odd or is even 20160101
Graphics3D(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()
Next
End()
Function IsOdd(TInt%)
R% = Abs(TInt) Mod 2
If(R = 0)
Return False
ElseIf(R = 1)
Return True
EndIf
End Function
Function IsEven(TInt%)
R% = Abs(TInt) Mod 2
If(R = 0)
Return True
ElseIf(R = 1)
Return False
EndIf
End Function

I always think it's tidier/less code to not use else in cases when there are only 2 possibilities.
Function IsOdd(TInt%)
R% = Abs(TInt) Mod 2
If(R = 0)
Return False
Endif
Return True
End Function

Why not use one simple function ... if it isn't even it's odd. ;D
Function IsEven( I% )
Return ( Abs(I) Mod 2 ) = 0
End Function

Yes you could rewrite it completely, simplify even more and use a not if required.
If( Not IsEven( num ) ) ; odd number

If number & 1
Number is odd
Else
Number is even

If number & 1
Number is odd
Else
Number is even
;D I knew there must be a bit shift variant.

Haha, nice one. I love these little efficiencies. ;D

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)

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 addup. No excuse for ugly bloaty routines when you were given some good advice, and like you mentioned you can store and reuse them again.
Thanks guys, I for one enjoyed your input.

thanks for teaching me how to behave and programming "Steve Elliott", i feel better now. i am really grateful to you. :P

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.

@steve elliott>>you are too smart for me, i give up :*

Function isEven(num%)
If num Shr 1 Shl 1 = num Then Return True
Return False
End Function
Function isOdd(num%)
If num Shr 1 Shl 1 <> num Then Return True
Return False
End Function
For i = 0 To 100
If isEven(i)
Print i+" is even"
ElseIf isOdd(i)
Print i+" is odd"
Else
Print i+" is nothing"
EndIf
Next
Function isEven(num%)
If ((num  1) Xor num) <> 1 Then Return True
Return False
End Function
Function isOdd(num%)
If ((num  1) Xor num) = 1 Then Return True
Return False
End Function
Function isEven(num%)
For i = 2147483648 To +2147483647 Step 2
If num = i Then Return True
Next
Return False
End Function
Function isOdd(num%)
For i = 2147483647 To +2147483647 Step 2
If num = i Then Return True
Next
Return False
End Function

Function isEven(num%)
For i = 2147483648 To +2147483647 Step 2
If num = i Then Return True
Next
Return False
End Function
Function isOdd(num%)
For i = 2147483647 To +2147483647 Step 2
If num = i Then Return True
Next
Return False
End 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

Function isEven(num%)
For i = 2147483648 To +2147483647 Step 2
If num = i Then Return True
Next
Return False
End Function
Function isOdd(num%)
For i = 2147483647 To +2147483647 Step 2
If num = i Then Return True
Next
Return False
End Function
That's my boy! true brute force at it's best ;D ( even with a bug )

lol :o

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
Function isEven(num%)
If Instr("02468",Right(Str(num),1)) > 0 Then Return True
Return False
End Function
Function isOdd(num%)
If Instr("13579",Right(Str(num),1)) > 0 Then Return True
Return False
End Function
Rube Goldberg use to find complex ways of solving simple problems. I guess this is the programmer's version of it? :D

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

or...
method IsEven:bool( input:int )
local check:float = input / 2
return int(check) = check
end method
@Qube  Brilliant! :))

@IwasAdam
"int/2" = int (at least in BlitzMax)
so better write "int/2.0" to return the float value.
Yet your
method IsEven:bool( input:int )
local check:float = input / 2.0
return int(check) = check
end method
might even be faster than the modulo :)
alternative to yours:
method IsEven:bool( input:int )
return int(input / 2.0)*2 = input
end method
Back to serious mode:
https://stackoverflow.com/questions/2229107/whatisthefastestwaytofindifanumberisevenorodd
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:
Function IsOdd:int(n:int)
if n = 0
return 0
elseif n = 1
return 1
else
return not IsOdd(n  1)
endif
End Function
bye
Ron

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