determine if an integer value is odd or is even

Started by RemiD, October 21, 2019, 22:03:41

Previous topic - Next topic

RemiD

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

Steve Elliott

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
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

STEVIE G

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



Steve Elliott

Yes you could re-write it completely, simplify even more and use a not if required.


If( Not IsEven( num ) )  ; odd number
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

TomToad

If number & 1
    Number is odd
Else
    Number is even
------------------------------------------------
8 rabbits equals 1 rabbyte.

STEVIE G

Quote from: TomToad on October 21, 2019, 23:26:34
If number & 1
    Number is odd
Else
    Number is even

;D I knew there must be a bit shift variant.

Steve Elliott

#6
Haha, nice one.  I love these little efficiencies.   ;D
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

RemiD

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)

Steve Elliott

#8
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.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

RemiD

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

Steve Elliott

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.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

RemiD

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

TomToad


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
------------------------------------------------
8 rabbits equals 1 rabbyte.

Derron

Quote from: TomToad on October 23, 2019, 21:24:04
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

Qube

Quote from: TomToad on October 23, 2019, 21:24:04
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 )
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD,
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32G DDR5 RAM 1T PCIe 4.0 SSD
MSI MEG 342C 34" QD-OLED Monitor

Until the next time.