PDA

View Full Version : Is Number Odd?



T.Jackson
- 11th June 2008, 12:16
Is there more than one way to test to see if an integer is even or odd? So far I've only been abe to devise one solution. The solution makes use of the AND bitwise operator.



public boolean isNumOdd(int num)
{
if (1 & num)
return true;
else
return false;
}


PBP code ...



main:
if 1 & num then
oddNum = true
else
oddNum = false
end if


Has anyone got another way of doing this? I'm incredibly keen to hear your thoughts.

Best Regards,

Trent Jackson

Melanie
- 11th June 2008, 13:01
If num.0=1 then
oddNum = true
else
oddNum = false
endif

sayzer
- 11th June 2008, 13:18
GrandPa's way!




OddNum = True
IF Num // 2 = 0 THEN OddNum = False



Does Num = 1 work?
----------------------------

T.Jackson
- 11th June 2008, 13:25
OddNum = True
IF Num // 2 = 0 THEN OddNum = False



Do me a favour and refresh my memory with what "//" does?

Trent Jackson

dhouston
- 11th June 2008, 13:44
Page 32 of the PBP manual defines it as Remainder (Modulus). I believe Modulus or Modulo is the more generally used terminology. It's also more useful, generally, than the other methods suggested as you can use it to test whether any number is evenly divisible by any other number. See...http://en.wikipedia.org/wiki/Modulo_operation

sayzer
- 11th June 2008, 13:49
Page 32 of the PBP manual defines it as Remainder (Modulus). I believe Modulus is the more generally used terminology. It's also more useful, generally, as you can use it to test whether any number is evenly divisible by any other number.


Do me a favour and refresh my memory with what "//" does?

Trent Jackson



I knew it did something for sure; (well, at least at where I am it does).

--------------

precision
- 11th June 2008, 13:50
in VB




If ((Text1.Text Mod 2) = 0) Then
MsgBox "Even"
Else
MsgBox "Odd"
End If




.

T.Jackson
- 11th June 2008, 13:54
in VB




If ((Text1.Text Mod 2) = 0) Then
MsgBox "Even"
Else
MsgBox "Odd"
End If




That's pretty uncanny bud, because I just wrote this ...



Dim num As Integer
Dim oddNum As Boolean

num = 5

If num Mod 2 = 0 Then
oddNum = False
Else
oddNum = True
End If


I reckon that there's a recursive way of doing it too!

Trent Jackson

precision
- 11th June 2008, 14:06
I think only mod formula is easy way to find oddeven.

oddeven = number mod 2
if oddeven = 0 then page is even, otherwise odd

.

skimask
- 11th June 2008, 14:06
How about the most rediculous, long winded, round-about way to do it?

T.Jackson
- 11th June 2008, 14:07
So to sum it up so far we've got the following, verified to be working solutions.

1. AND Bitwise operator (My initial approach)
2. Direct bit checking (Melanie's approach)
3. Modulus remainder operator (Precision & Sayzer's approach)

The goal is 10 solutions -- can we do it?

Trent Jackson

T.Jackson
- 11th June 2008, 14:19
How about the most rediculous, long winded, round-about way to do it?

Any solution that can be verified workable!

Trent Jackson

skimask
- 11th June 2008, 15:38
testnumber var word
main:
if testnumber = 0 then
lcdout "Undefined"
stop
endif
testnumber = testnumber - 1
testnumber = testnumber - 1
if testnumber = 0 then
lcdout "EVEN"
stop
endif
if testnumber = $ffff then
lcdout "ODD"
stop
endif
if testnumber > 0 and testnumber < $ffff then lcdout "Not done yet"
goto main
end

Should work, haven't tried it...but it's surely a waste of resources! :)

T.Jackson
- 11th June 2008, 15:44
testnumber var word
main:
if testnumber = 0 then
lcdout "Undefined"
stop
endif
testnumber = testnumber - 1
testnumber = testnumber - 1
if testnumber = 0 then
lcdout "EVEN"
stop
endif
if testnumber = $ffff then
lcdout "ODD"
stop
endif
if testnumber > 0 and testnumber < $ffff then lcdout "Not done yet"
goto main
end

Should work, haven't tried it...but it's surely a waste of resources! :)

I agree that, that's workable. Continuously subtracting 2 from the target number until you either reach zero, which denotes an even num or -1 equating to an odd num.

Trent Jackson

sayzer
- 11th June 2008, 16:03
I also have another one.

Mine does two calculations and compares the results in case the processor is going weird.
And even more, has a bug report feature.

Ski, is this ridiculous enough? I can go deeper.





</i></font>TestNumber <font color="#000080"><b>VAR WORD
</b></font>Index <font color="#000080"><b>VAR BYTE
</b></font>Result1 <font color="#000080"><b>VAR BIT
</b></font>Result2 <font color="#000080"><b>VAR BIT
</b></font>FinalResult <font color="#000080"><b>VAR BIT
</b></font>Even <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>0
</b></font>Odd <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>1
</b></font>Err <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>2

</b></font>Begin:

Result1 = Even
<font color="#000080"><b>FOR </b></font>Index = <font color="#FF0000"><b>1 </b></font><font color="#000080"><b>TO </b></font><font color="#FF0000"><b>9 </b></font><font color="#000080"><b>STEP </b></font><font color="#FF0000"><b>2
</b></font><font color="#000080"><b>IF </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= Index <font color="#000080"><b>THEN
</b></font>Result1 = Odd
Index = <font color="#FF0000"><b>11 </b></font><font color="#000080"><i>' Exit loop.
</i><b>ENDIF
NEXT </b></font>Index


<font color="#000080"><i>' To make sure, do another calculation !!

</i></font>Result2 = Odd
<font color="#000080"><b>IF </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>0 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>2 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>4 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>6 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>8 </b></font><font color="#000080"><b>THEN </b></font>Result2 = Even


<font color="#000080"><i>' And finally, compare the results;

</i><b>IF </b></font>Result1 = Result2 <font color="#000080"><b>AND </b></font>Result1 = Odd <font color="#000080"><b>THEN
</b></font>FinalResult = Odd
<font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>1</b></font>, <font color="#008000"><b>&quot;Result1 :&quot;</b></font>,#Result1
<font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$c0</b></font>,<font color="#008000"><b>&quot;Result2 :&quot;</b></font>,#Result2
<font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$94</b></font>,<font color="#008000"><b>&quot;FinalResult:&quot;</b></font>,#FinalResult
<font color="#000080"><b>ELSE
</b></font>FinalResult = Err
<font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>1</b></font>, <font color="#008000"><b>&quot;Unknown Error Occured!&quot;
</b></font><font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$c0</b></font>,<font color="#008000"><b>&quot;Reporting to support page...&quot;
</b></font><font color="#000080"><b>HSEROUT </b></font>[<font color="#008000"><b>&quot;weblink:www.picbasic.co.uk/forum&quot;</b></font>]
<font color="#000080"><b>PAUSE </b></font><font color="#FF0000"><b>100
</b></font><font color="#000080"><b>HSEROUT </b></font>[<font color="#008000"><b>&quot;@Msg:alert@bug found&quot;</b></font>]
<font color="#000080"><b>ENDIF



GOTO </b></font>Begin

T.Jackson
- 11th June 2008, 16:13
I also have another one.

Mine does two calculations and compares the results in case the processor is going weird.
And even more, has a bug report feature.

Ski, is this ridiculous enough? I can go deeper.





</i></font>TestNumber <font color="#000080"><b>VAR WORD
</b></font>Index <font color="#000080"><b>VAR BYTE
</b></font>Result1 <font color="#000080"><b>VAR BIT
</b></font>Result2 <font color="#000080"><b>VAR BIT
</b></font>FinalResult <font color="#000080"><b>VAR BIT
</b></font>Even <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>0
</b></font>Odd <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>1
</b></font>Err <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>2

</b></font>Begin:

Result1 = Even
<font color="#000080"><b>FOR </b></font>Index = <font color="#FF0000"><b>1 </b></font><font color="#000080"><b>TO </b></font><font color="#FF0000"><b>9 </b></font><font color="#000080"><b>STEP </b></font><font color="#FF0000"><b>2
</b></font><font color="#000080"><b>IF </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= Index <font color="#000080"><b>THEN
</b></font>Result1 = Odd
Index = <font color="#FF0000"><b>11 </b></font><font color="#000080"><i>' Exit loop.
</i><b>ENDIF
NEXT </b></font>Index


<font color="#000080"><i>' To make sure, do another calculation !!

</i></font>Result2 = Odd
<font color="#000080"><b>IF </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>0 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>2 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>4 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>6 </b></font><font color="#000080"><b>OR </b></font>TestNumber <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>8 </b></font><font color="#000080"><b>THEN </b></font>Result2 = Even


<font color="#000080"><i>' And finally, compare the results;

</i><b>IF </b></font>Result1 = Result2 <font color="#000080"><b>AND </b></font>Result1 = Odd <font color="#000080"><b>THEN
</b></font>FinalResult = Odd
<font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>1</b></font>, <font color="#008000"><b>&quot;Result1 :&quot;</b></font>,#Result1
<font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$c0</b></font>,<font color="#008000"><b>&quot;Result2 :&quot;</b></font>,#Result2
<font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$94</b></font>,<font color="#008000"><b>&quot;FinalResult:&quot;</b></font>,#FinalResult
<font color="#000080"><b>ELSE
</b></font>FinalResult = Err
<font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>1</b></font>, <font color="#008000"><b>&quot;Unknown Error Occured!&quot;
</b></font><font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$c0</b></font>,<font color="#008000"><b>&quot;Reporting to support page...&quot;
</b></font><font color="#000080"><b>HSEROUT </b></font>[<font color="#008000"><b>&quot;weblink:www.picbasic.co.uk/forum&quot;</b></font>]
<font color="#000080"><b>PAUSE </b></font><font color="#FF0000"><b>100
</b></font><font color="#000080"><b>HSEROUT </b></font>[<font color="#008000"><b>&quot;@Msg:alert@bug found&quot;</b></font>]
<font color="#000080"><b>ENDIF



GOTO </b></font>Begin



Reasoning looks good. No need for the second test and comparisons though.

Trent Jackson

skimask
- 11th June 2008, 16:25
Ski, is this ridiculous enough? I can go deeper.
Keep going!!!

(What do they call those 'machines' that take the most indirect route to accomplish something? Remember that kids game 'Mousetrap'?)

My next submission:


testnumber var word
onescount var byte
onescount1 var byte
loopvar var byte

testnumber = 12345

main:
for loopvar = 0 to 15 'counts bits set in the testnumber
if testnumber.0[loopvar] = 1 then
onescount = onescount + 1
endif
next loopvar

testnumber = testnumber >> 1 'shifts the LSB out
testnumber = testnumber << 1 'shifts a zero bit back in

for loopvar = 0 to 15 'recounts bits set in the testnumber
if testnumber.0[loopvar] = 1 then
onescount1 = onescount1 + 1
endif
next loopvar

if onescount = onescount1 then lcdout "EVEN" 'if the number of set bits is the same, its even
if onescount <> onescount1 then lcdout "ODD"
STOP 'please stop!

sayzer
- 11th June 2008, 16:31
Keep going!!!

(What do they call those 'machines' that take the most indirect route to accomplish something? Remember that kids game 'Mousetrap'?)

Here they call it "Stupiditer II".

:)

skimask
- 11th June 2008, 16:35
Here they call it "Stupiditer II".
:)

Rube Goldberg machines...

http://en.wikipedia.org/wiki/Rube_Goldberg_machine

SteveB
- 11th June 2008, 16:49
Here's another approach:


TestNumber as word
TestVal as word

If TestNumber = 0 then Zero
TestVal = 2
Test:
If TestNumber = TestVal Then
LCDOUT "Even"
EndProgram
Else
If TestNumber = TestVal - 1 then
LCDOUT "Odd"
EndProgram
Else
TestVal = TestVal + 2
Test
EndiF
Endif

Zero:
LCDOUT "Zero"
EndProgram

EndProgram:
end

T.Jackson
- 11th June 2008, 16:50
Keep going!!!

(What do they call those 'machines' that take the most indirect route to accomplish something? Remember that kids game 'Mousetrap'?)

My next submission:


testnumber var word
onescount var byte
onescount1 var byte
loopvar var byte

testnumber = 12345

main:
for loopvar = 0 to 15 'counts bits set in the testnumber
if testnumber.0[loopvar] = 1 then
onescount = onescount + 1
endif
next loopvar

testnumber = testnumber >> 1 'shifts the LSB out
testnumber = testnumber << 1 'shifts a zero bit back in

for loopvar = 0 to 15 'recounts bits set in the testnumber
if testnumber.0[loopvar] = 1 then
onescount1 = onescount1 + 1
endif
next loopvar

if onescount = onescount1 then lcdout "EVEN" 'if the number of set bits is the same, its even
if onescount <> onescount1 then lcdout "ODD"
STOP 'please stop!


That submission is void. The method for direct bit testing has already recognized. You were actually in the running for first prize of ($100,000) for your first submission. But unfortunately you have been suspended from the competition for acts plagiarism.

Naughty, naughty

Trent Jackson

T.Jackson
- 11th June 2008, 16:57
Here's another approach:


TestNumber as word
TestVal as word

If TestNumber = 0 then Zero
TestVal = 2
Test:
If TestNumber = TestVal Then
LCDOUT "Even"
EndProgram
Else
If TestNumber = TestVal - 1 then
LCDOUT "Odd"
EndProgram
Else
TestVal = TestVal + 2
Test
EndiF
Endif

Zero:
LCDOUT "Zero"
EndProgram

EndProgram:
end

Excellent! That's called a recursive method.

Trent Jackson

skimask
- 11th June 2008, 17:00
The method for direct bit testing has already recognized.

Aye lad...but if you'll notice...the direct bit checking is not used to determine the final result! :D A bit like using a pencil to design a machine to manufacture more pencils.
This could get fun...

SteveB
- 11th June 2008, 17:06
Aye lad...but if you'll notice...the direct bit checking is not used to determine the final result! :D A bit like using a pencil to design a machine to manufacture more pencils.
This could get fun...

My approach is also somewhat indirect, in that it does not directly check if the number is odd. Rather, it sequentially checks to see if the test number matches a value which is known to be either even or odd.

T.Jackson
- 11th June 2008, 17:13
My approach is also somewhat indirect, in that it does not directly check if the number is odd. Rather, it sequentially checks to see if the test number matches a value which is known to be either even or odd.

You're pretty much doing the same as Sayzer (comparing with known to be odd / even numbers) -- but the way in which you have it configured to call itself repeatedly until the operation has completed is considered to be recursion. However, normally recursive methods perform the work on the subject and not references.

Trent Jackson

SteveB
- 11th June 2008, 17:22
Another recursive method. However this time it uses the unique feature of Variable Rollover. Also, if nothing is found at the end of checking all possible values for a WORD, the outcome must be Zero.


TestNumber as word
TestVal as word

For TestVal = 65534 to 2 step -2
If (TestNumber + TestVal)= 0 Then
LCDOUT "Even"
EndProgram
Else
If (TestNumber + TestVal) + 1 = 0 then
LCDOUT "Odd"
EndProgram
Else
Endif
Next TestVal
LCDOUT "Zero"
EndProgram:
end

EDIT: This may be premature, I think I left a hole at the value of "65535". Standby

T.Jackson
- 11th June 2008, 17:28
Another recursive method. However this time it uses the unique feature of Variable Rollover. Also, if nothing is found at the end of checking all possible values for a WORD, the outcome must be Zero.


TestNumber as word
TestVal as word

For TestVal = 65534 to 2 step -2
If (TestNumber + TestVal)= 0 Then
LCDOUT "Even"
EndProgram
Else
If (TestNumber + TestVal) + 1 = 0 then
LCDOUT "Odd"
EndProgram
Else
Endif
Next TestVal
LCDOUT "Zero"
EndProgram:
end

I don't consider that to be recursive. To be recursive means to have a procedure (method in Java's case) -- that repeatedly calls itself until the operation is complete.

Kinda like getting a pie that you need to cut up into (n) pieces. Instead of cutting out each piece you cut the whole thing repeadedly until you have (n) pieces.

Trent Jackson

SteveB
- 11th June 2008, 17:31
OK, This is better. It has to do one final check after the FOR..NEXT loop.


TestNumber as word
TestVal as word

For TestVal = 65534 to 2 step -2
If (TestNumber + TestVal)= 0 Then
LCDOUT "Even"
EndProgram
Else
If (TestNumber + TestVal) + 1 = 0 then
LCDOUT "Odd"
EndProgram
Else
Endif
Next TestVal
If (TestNumber + TestVal) - 1 = 0 then
LCDOUT "Odd"
EndProgram
Else
LCDOUT "Zero"
Endif
EndProgram:
end

T.Jackson
- 11th June 2008, 17:40
Recursive example ...



public int divideBy(int n)
{
num = num / n
if(num > 0)
divideBy(n);
}


It keeps calling itself until we have arrived at something. In PBP you would of course need to exchange the "public int divideBy(int n)" with a label and swap "divideBy(n)" with a goto.

Trent Jackson

precision
- 11th June 2008, 17:53
Hi Trent, I think...

There'r Out of 10's methods. You want's onlyup to 10th.
So should Listing now.

.