Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge: Algorithms having only one line of code.
#1
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
*****
Reply
#2
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? Smile
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#3
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.
*****
Reply
#4
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.
earn.
Reply
#5
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.
Reply
#6
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.
*****
Reply
#7
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&
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#8
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!
*****
Reply
#9
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.
*****
Reply
#10
d'oh! It's factorial!! Well, I have no idea how to code it otherwise. :|
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)