Ooops
July 28, 2021, 06:22:02

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

#### Steve Elliott

• Hero Member
• Posts: 3146
• elgol
##### Re: determine if an integer value is odd or is even
« Reply #15 on: October 23, 2019, 23:00:56 »
lol
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 400, Pi4, BBC B, C64, ZX Spectrum

• Hero Member
• Posts: 553
##### Re: determine if an integer value is odd or is even
« Reply #16 on: October 24, 2019, 09:07:02 »
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?

« Last Edit: October 24, 2019, 09:19:58 by TomToad »
------------------------------------------------
8 rabbits equals 1 rabbyte.

#### Derron

• Hero Member
• Posts: 3614
##### Re: determine if an integer value is odd or is even
« Reply #17 on: October 24, 2019, 09:30:14 »
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: 2346
##### Re: determine if an integer value is odd or is even
« Reply #18 on: October 24, 2019, 10:48:21 »
or...
Code: [Select]
`method IsEven:bool( input:int )  local check:float = input / 2  return int(check) = checkend method`
@Qube - Brilliant!

#### Derron

• Hero Member
• Posts: 3614
##### Re: determine if an integer value is odd or is even
« Reply #19 on: October 24, 2019, 11:23:04 »
"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
« Last Edit: October 24, 2019, 11:37:11 by Derron »

#### RemiD

• Hero Member
• Posts: 1273
##### Re: determine if an integer value is odd or is even
« Reply #20 on: October 24, 2019, 17:45:36 »
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