October 28, 2020, 11:06:14 PM

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

#### Qube

• Hero Member
• Posts: 2535
• I mostly code at night... Mostly.
##### Re: determine if an integer value is odd or is even
« Reply #15 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 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
Commodore VIC-20, 1.1Mhz MOS 6502 CPU, 5KB RAM, VIC ( 6560 ) GPU

Until the next time...

#### Steve Elliott

• Hero Member
• Posts: 2821
• elgol 2021
##### Re: determine if an integer value is odd or is even
« Reply #16 on: October 23, 2019, 11:00:56 PM »
lol
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Catalina, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry Pi 3 and 4

• Hero Member
• Posts: 517
##### Re: determine if an integer value is odd or is even
« Reply #17 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 ( 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 FalseEnd FunctionFunction isOdd(num%) If Instr("13579",Right(Str(num),1)) > 0 Then Return True Return FalseEnd Function`
Rube Goldberg use to find complex ways of solving simple problems.  I guess this is the programmer's version of it?

------------------------------------------------
8 rabbits equals 1 rabbyte.

#### Derron

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

bye
Ron

• Hero Member
• Posts: 1978
##### Re: determine if an integer value is odd or is even
« Reply #19 on: October 24, 2019, 10:48:21 AM »
or...
Code: [Select]
`method IsEven:bool( input:int )  local check:float = input / 2  return int(check) = checkend method`
@Qube - Brilliant!

#### Derron

• Hero Member
• Posts: 3237
##### Re: determine if an integer value is odd or is even
« Reply #20 on: October 24, 2019, 11:23:04 AM »
"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) = checkend method`might even be faster than the modulo

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

bye
Ron

#### RemiD

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

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