December 10, 2019, 02:25:30 PM

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

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2221
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 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 )
Until the next time...

Offline Steve Elliott

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

Offline TomToad

  • Sr. Member
  • ****
  • Posts: 451
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 ;D ( 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 False
End Function

Function isOdd(num%)
If Instr("13579",Right(Str(num),1)) > 0 Then Return True
Return False
End Function

Rube Goldberg use to find complex ways of solving simple problems.  I guess this is the programmer's version of it? :D

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

Offline Derron

  • Hero Member
  • *****
  • Posts: 2610
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).

See - your algorithm was already over my head, so it must be the perfect one :)

bye
Ron

Online iWasAdam

  • Hero Member
  • *****
  • Posts: 1422
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) = check
end method

@Qube - Brilliant!  :))

Offline Derron

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


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


bye
Ron

Offline RemiD

  • Hero Member
  • *****
  • Posts: 1026
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)
 ;D
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