Challenge: Algorithms having only one line of code. - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QbasicNews.Com (http://qbasicnews.com/newforum/forum-3.html) +--- Forum: Challenges (http://qbasicnews.com/newforum/forum-10.html) +--- Thread: Challenge: Algorithms having only one line of code. (/thread-1306.html) |
Challenge: Algorithms having only one line of code. - Moneo - 06-24-2003 Code: Ok, here's the rules: Challenge: Algorithms having only one line of code. - na_th_an - 06-24-2003 The cycling counter: Code: counter% = (counter% + 1) MOD max% If max% is a power of two it is better to do... Code: counter% = (counter% + 1) AND (max% - 1) Does this one count? Challenge: Algorithms having only one line of code. - Moneo - 06-24-2003 Yes nathan, it may count if I can figure out what you mean by a "cycling counter". Does your one instruction finish the whole idea? ***** EDIT: I just figured out with pencil and paper what you want to do. It's brilliant! You want to do (or cycle) some code max number of times, where max is a power of 2. Actually, because of the logic, you need to do this from zero to max-1. You need the following setup or initialization. max = (some power of 2) - 1 counter = 0 gosub logic Then, your one line algorittm embedded in a loop becomes: loop: counter = (counter + 1) and max gosub logic goto loop In this manner, your one line instruction to increment and reset will enable us to perform the subroutine called "logic" repeatedly for values of zero to max. The rest of you should notice the subtle point of his instruction, in that when the counter is incremented 1 beyond max, the "and" wraps it around to zero. ***** Challenge: Algorithms having only one line of code. - seph - 06-24-2003 I've tried the code and basically it counts starting at counter% until it hits max%, at which point it sets counter% to 0. Basically a normal counter. Works best in DO ... LOOPs. Might be used for a delay method. Challenge: Algorithms having only one line of code. - oracle - 06-24-2003 Here's mine, to turn a switch on if off, or off if on, without using XOR. x = 1 if on, and 0 if off. Code: x = ((x = 0) AND 1) OR (1 AND (X = 0)) Though I suspect there may be an easier way, the logical (x=0) may be a hint to others about other algos. EDIT: Aah. I've just seen Moneo's other way in the XOR thread. But my way still works. Challenge: Algorithms having only one line of code. - Moneo - 06-24-2003 Oracle, I didn't complie and check yours out, but it looks like you're trying to implement an XOR without using it, which would be like: a=a XOR b is the same as a=(a and not(b)) or (b and not(a)) So, let's substitute your x for a in the above, and 1 for b in above. So then we have what I think you wanted: x=(x and (not(1)) or (1 and not(x)) That looks awful. It's late here, let me compile and test it tomorrow. ***** Challenge: Algorithms having only one line of code. - Agamemnus - 06-24-2003 Is this good? :| Code: CLS To Oracle: 2nd look at your code. - Moneo - 06-24-2003 Quote:Here's mine, to turn a switch on if off, or off if on, without using XOR. x = 1 if on, and 0 if off. I was wrong. You are not implementing an XOR. Your code above is a very good approach and it works. However, the second expression after the OR is exactly the same as the first expression, and therefore you can just exclude it, leaving the resultant code as: Code: x = ((x = 0) AND 1) Another observation: You have introduced the fact that any expression, like (x=0), results in a true (-1) or false (0) condition. For example: result = (a>b) if a is greater than b, result is -1, true if a is not greater than b, then result is 0, false. You did great, Oracle! ***** To Seph and Agamemnus: - Moneo - 06-24-2003 Quote:I've tried the code and basically it counts starting at counter% until it hits max%, at which point it sets counter% to 0. Basically a normal counter. Works best in DO ... LOOPs. Might be used for a delay method. Good observations, Seph. Another use of this "increment and auto wrap at max" logic could be when you are processing a circular buffer whose size is a power of 2. Every time you access the buffer, you don't need to ask "I am at the end of the buffer?" and waste the execution time to test it. ***** TO AGAMEMNUS: It might be good if you gave us a hint as to what you want to do. Also, the clause after the ELSE has 2 instructions on it, which are not allowed. ***** factorial. - Agamemnus - 06-24-2003 d'oh! It's factorial!! Well, I have no idea how to code it otherwise. :| |