September 20, 2021, 09:56:12

### Author Topic: determine if an integer value is odd or is even  (Read 5666 times)

#### RemiD

• Hero Member
• Posts: 1282
##### determine if an integer value is odd or is even
« 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 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
« Last Edit: October 21, 2019, 22:06:48 by RemiD »
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### Steve Elliott

• Hero Member
• Posts: 3225
• elgol
##### Re: determine if an integer value is odd or is even
« Reply #1 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 True
End Function
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry pi 3, pi 4, pi 400, BBC B, C64, ZX Spectrum

#### STEVIE G

• Hero Member
• Posts: 608
##### Re: determine if an integer is odd or even
« Reply #2 on: October 21, 2019, 22:10:12 »
Why not use one simple function ... if it isn't even it's odd.

Code: [Select]
Function IsEven( I% )

Return ( Abs(I) Mod 2 ) = 0

End Function

#### Steve Elliott

• Hero Member
• Posts: 3225
• elgol
##### Re: determine if an integer value is odd or is even
« Reply #3 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
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry pi 3, pi 4, pi 400, BBC B, C64, ZX Spectrum

#### TomToad

• Hero Member
• Posts: 554
##### Re: determine if an integer value is odd or is even
« Reply #4 on: October 21, 2019, 23:26:34 »
If number & 1
Number is odd
Else
Number is even
------------------------------------------------
8 rabbits equals 1 rabbyte.

#### STEVIE G

• Hero Member
• Posts: 608
##### Re: determine if an integer value is odd or is even
« Reply #5 on: October 22, 2019, 00:50:29 »
If number & 1
Number is odd
Else
Number is even

I knew there must be a bit shift variant.

#### Steve Elliott

• Hero Member
• Posts: 3225
• elgol
##### Re: determine if an integer value is odd or is even
« Reply #6 on: October 22, 2019, 08:00:10 »
Haha, nice one.  I love these little efficiencies.
« Last Edit: October 22, 2019, 08:31:01 by Steve Elliott »
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry pi 3, pi 4, pi 400, BBC B, C64, ZX Spectrum

#### RemiD

• Hero Member
• Posts: 1282
##### Re: determine if an integer value is odd or is even
« Reply #7 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)
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### Steve Elliott

• Hero Member
• Posts: 3225
• elgol
##### Re: determine if an integer value is odd or is even
« Reply #8 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.
« Last Edit: October 22, 2019, 12:23:08 by Steve Elliott »
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry pi 3, pi 4, pi 400, BBC B, C64, ZX Spectrum

#### RemiD

• Hero Member
• Posts: 1282
##### Re: determine if an integer value is odd or is even
« Reply #9 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.
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### Steve Elliott

• Hero Member
• Posts: 3225
• elgol
##### Re: determine if an integer value is odd or is even
« Reply #10 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.
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry pi 3, pi 4, pi 400, BBC B, C64, ZX Spectrum

#### RemiD

• Hero Member
• Posts: 1282
##### Re: determine if an integer value is odd or is even
« Reply #11 on: October 23, 2019, 18:11:19 »
@steve elliott>>you are too smart for me, i give up
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

#### TomToad

• Hero Member
• Posts: 554
##### Re: determine if an integer value is odd or is even
« Reply #12 on: October 23, 2019, 21:24:04 »

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

#### Derron

• Hero Member
• Posts: 3651
##### Re: determine if an integer value is odd or is even
« Reply #13 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 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

• Administrator
• Hero Member
• Posts: 2768
• I mostly code at night... Mostly.
##### Re: determine if an integer value is odd or is even
« Reply #14 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 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 ( even with a bug )
Mac mini ( 2018 / 2020 ), 3 GHz 6-Core Intel Core i5, 16 GB 2667 MHz DDR4, 1TB NVMe, eGPU Radeon Pro 580 8 GB, LG Ultragear 27GL83A-B 27 Inch
Mac mini (2020 ), M1 SoC, 8 GB LPDDR4, 512GB NVMe
Commodore VIC-20, 1.1Mhz MOS 6502 CPU, 5KB RAM, VIC ( 6560 ) GPU

Until the next time.

SimplePortal 2.3.6 © 2008-2014, SimplePortal