Is Number Odd?


Closed Thread
Results 1 to 30 of 30

Thread: Is Number Odd?

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    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.


    Code:
    </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
    Last edited by sayzer; - 11th June 2008 at 16:05.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  2. #2
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    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.


    Code:
    </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
    Last edited by T.Jackson; - 11th June 2008 at 16:16.

  3. #3
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    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:
    Code:
    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!
    Last edited by skimask; - 11th June 2008 at 16:34.

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    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".

    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    Here they call it "Stupiditer II".
    Rube Goldberg machines...

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

  6. #6
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Here's another approach:

    Code:
    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

  7. #7
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by SteveB View Post
    Here's another approach:

    Code:
    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

  8. #8
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    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:
    Code:
    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

  9. #9
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by T.Jackson View Post
    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! A bit like using a pencil to design a machine to manufacture more pencils.
    This could get fun...

  10. #10
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Aye lad...but if you'll notice...the direct bit checking is not used to determine the final result! 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.

  11. #11
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by SteveB View Post
    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

  12. #12
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    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.

    Code:
    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
    Last edited by SteveB; - 11th June 2008 at 17:26.

Similar Threads

  1. Dynamic USB Serial Number (PIC18F4550)
    By awmt102 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th July 2009, 17:03
  2. dec number to show on lcd (maths)
    By savnik in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th August 2007, 19:18
  3. Working with the random number generator
    By kwelna in forum mel PIC BASIC
    Replies: 9
    Last Post: - 16th January 2007, 17:50
  4. Random number results
    By bartman in forum mel PIC BASIC
    Replies: 3
    Last Post: - 9th March 2005, 17:39
  5. more random number questions
    By bartman in forum mel PIC BASIC
    Replies: 1
    Last Post: - 14th November 2004, 17:55

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts