September 16, 2021, 23:42:37

### Author Topic: determine if an integer value is odd or is even  (Read 5649 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 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`
« 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: 3224
• 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 TrueEnd 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: 604
##### 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 ) = 0End Function`

#### Steve Elliott

• Hero Member
• Posts: 3224
• 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

• 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: 604
##### 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: 3224
• 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: 3224
• 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: 3224
• 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

• 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 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`
------------------------------------------------
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 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

#### Qube

• Hero Member
• Posts: 2764
• 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 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 ( 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.