# Qbasicnews.com

Full Version: Challenge: Algorithms having only one line of code.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10
Code:
```Ok, here's the rules: 1) It must be considered an algorithm, formula or method.     Add 15 to X   ----   X=X+15    This is not an algorithm, agreed?     If it were, then every line of code in a program would be. 2) You must describe the algorithm first, i.e., what is its purpose. 3) Then show the one line of code. 4) Multiple statments on one line; like: x=x+1 : x=x and 15     are not considered as one line of code. A good example is one we've already seen with the XOR. The purpose is to turn a switch on if it's off, and turn it off if it's on. The one line of code is: sw=sw xor sw or equally cool:              sw=1-sw```
*****
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?
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.
*****
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.
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.
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.
*****
Is this good? :|

Code:
```CLS fact& = 12 fact2& = 1 1 IF fact& = 2 THEN fact2& = fact2& * 2 ELSE fact2& = fact2& * fact&: fact& = fact& - 1: GOTO 1 PRINT fact2&```
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.

Code:
`x = ((x = 0) AND 1) OR (1 AND (X = 0))`

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!
*****
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.
*****
d'oh! It's factorial!! Well, I have no idea how to code it otherwise. :|
Pages: 1 2 3 4 5 6 7 8 9 10