Is Number Odd?


Closed Thread
Results 1 to 30 of 30

Thread: Is Number Odd?

Hybrid View

  1. #1
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Talking

    How about the most rediculous, long winded, round-about way to do it?

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    How about the most rediculous, long winded, round-about way to do it?
    Any solution that can be verified workable!

    Trent Jackson

  3. #3
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default Double subtraction

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

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


    Did you find this post helpful? Yes | No

    Default

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

  5. #5
    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

  6. #6
    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.

  7. #7
    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.

  8. #8
    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

  9. #9
    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

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