Odd or Even numbers

Just starting out? Need help? Post your questions and find answers here.
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Odd or Even numbers

Post by PeterGams »

How can I get PB to distinguish between Odd and Even numbers?
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

One way (with integers): AND with 1 as the low bit is always 0 for evens and 1 for odds. Zero is "even".

Code: Select all

a.l=0

If a & 1
  Debug "odd"
Else
  Debug "even"
EndIf
Dare2 cut down to size
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Thanks for that.

Post by PeterGams »

If a user inputs a number (single digit only) 0 to 9.
I want to be able to add 2 to it until it rotates back to where it started.
eg. Inputed number 4 then it goes 6 8 0 2 back to 4. If odd inputed no. 5 then it will go 7 9 1 3 back to five. Make sense?
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

What you said makes sense however the explanation/concept has nothing to put it into context - which may be relevant to approach. My first thought was a tight loop:

Code: Select all

num = 1            ; or whatever
initialNum = num

Repeat
  Debug Str(num) + " " + Str(initialNum)  ; Or whatever is being done with it
  num + 2
  If num > 9
    num - 10
  EndIf
Until num = initialNum
This is a pretty simplistic method. Regardless, it is an approach that can be used in a broader context.

Hope it is useful. If not, keep asking and a sharper mind than mine will be along shortly, instantly understand what you're after and give you the good oil. :)
Dare2 cut down to size
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Thank's Heaps !

Post by PeterGams »

That's great stuff. Thanks heaps...
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Post by PeterGams »

One more for you if you have time.
What if I wanted to have 3 colomns of the same sort of generated numbers.
eg. 3 4 1
5 6 3
7 8 5
9 0 7
1 2 9
This would be a big help also.
Thanks !!
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Post by PeterGams »

The second row runs into 2 digits. And won't rotate back to start.
Any ideas ?

Code: Select all

num5 = 5 : num6 = 8            ; or whatever 
initialNum6 = num6 
initialNum5 = num5
Repeat 
  Debug Str(num6) + " - " + Str(num5); + Str(initialNum)  ; Or whatever is being done with it 
  num6 + 2 : num5 + 2
  If num6 > 9 :  num6 - 10 
    If num5 > 9 :  num5 - 10

    EndIf
  EndIf 
  
Until num6 = initialNum6 : Or num5 = initialnum5
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

I'm just using the MOD operator, see helpfile. Not tested, but should work with all numbers (greater and smaller than 0), incl. 0.

Code: Select all

Macro isOdd(number)
     (number % 2 AND 1)
EndMacro
PB 4.30

Code: Select all

onErrorGoto(?Fred)
PeterGams
User
User
Posts: 25
Joined: Sun Dec 03, 2006 4:19 am
Location: Australia

Post by PeterGams »

Sorry you've lost me. I can't find MOD in help..
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

It's under Variables, Types and Operators. Look for the percent sign operator:
PB docs wrote:Modulo. Returns the remainder of the RHS by LHS integer division.

Example:
a=16 % 2 ; The value of a will be 0 as 16/2 = 8 (no remainder)
b=17 % 2 ; The value of a will be 1 as 17/2 = 8*2+1 (1 is remaining)
BERESHEIT
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

All in all I come to the cnclusion, that my code and Derek's Code are the fastest. :D

But my one is easier. :wink:

I expanded my

Code: Select all

Macro isOdd(number)
     (number & 1)
EndMacro

Macro isEven(number)
     (number % 2 XOr 1)
EndMacro

Macro isEven_2(number)
     (Not (number % 2))
EndMacro

;- Testing
Debug isOdd(23)
Debug isOdd(24)
Debug isEven(23)
Debug isEven(24)
Debug isEven_2(23)
Debug isEven_2(24)
  • Added isEven_2(), which is working without "XOr 1"
    Changed isOdd(), now working bitwise instead of using modulo (thx2kaeruGaman)
Last edited by AND51 on Mon Dec 04, 2006 6:15 pm, edited 2 times in total.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Why is the "And 1" in there? Number % 2 is all you need.
BERESHEIT
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Not really. My macro just returns 1 or 0.

Let's imagine, we've got 20 % 9, result would be 2.

No, I made my code as every PB Procedure, it just returns 0 or 1. It's better to compare in If's.


@ PeterGums: What about feedback? :lol:
PB 4.30

Code: Select all

onErrorGoto(?Fred)
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

I believe netmaestro means that in your macro the 'And 1' in the statement:

(number % 2 And 1)

is unnecessary, so why have it in there?

cheers
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Oh, ok, now I see what your aiming at:

my code without "And 1" can only return 0 or 1... And that can be achived even without "And 1".... OK, I will leave it out.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Post Reply