SyntaxBomb - Indie Coders

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

Title: determine if an integer value is odd or is even
Post by: RemiD on October 21, 2019, 10:03:41 PM
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 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
Title: Re: determine if an integer value is odd or is even
Post by: Steve Elliott on October 21, 2019, 10:09:45 PM
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 True
End Function
Title: Re: determine if an integer is odd or even
Post by: STEVIE G on October 21, 2019, 10:10:12 PM
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 ) = 0

End Function

Title: Re: determine if an integer value is odd or is even
Post by: Steve Elliott on October 21, 2019, 10:30:45 PM
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, 11:26:34 PM
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, 12:50:29 AM
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 AM
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 PM
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 PM
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: GfK on October 22, 2019, 01:54:29 PM
Code: [Select]
Function IsOdd(num%)
  Return (num & 1)
End Function

Code: [Select]
Function IsEven(num%)
  Return Not (num & 1)
End Function

...or something like that.  Don't use Blitzwhateverstrainthisis any more.
Title: Re: determine if an integer value is odd or is even
Post by: RemiD on October 22, 2019, 05:00:58 PM
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, 08:51:52 PM
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, 06:11:19 PM
@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, 09:24:04 PM

Code: [Select]
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
Code: [Select]
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
Code: [Select]
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
Title: Re: determine if an integer value is odd or is even
Post by: Derron on October 23, 2019, 09:28:38 PM
Code: [Select]
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
Title: Re: determine if an integer value is odd or is even
Post by: Qube on October 23, 2019, 10:56:30 PM
Code: [Select]
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 )
Title: Re: determine if an integer value is odd or is even
Post by: Steve Elliott on October 23, 2019, 11:00:56 PM
lol  :o
Title: Re: determine if an integer value is odd or is even
Post by: TomToad on October 24, 2019, 09:07:02 AM
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 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

Title: Re: determine if an integer value is odd or is even
Post by: Derron on October 24, 2019, 09:30:14 AM
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 AM
or...
Code: [Select]
method IsEven:bool( input:int )
  local check:float = input / 2
  return int(check) = check
end 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 AM
@IwasAdam
"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) = check
end method
might even be faster than the modulo :)


alternative to yours:
Code: [Select]
method IsEven:bool( input:int )
  return int(input / 2.0)*2 = input
end 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)
   endif
End Function


bye
Ron
Title: Re: determine if an integer value is odd or is even
Post by: RemiD on October 24, 2019, 05:45:36 PM
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