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
Quote:'
coloums% = 6: rows% = 42:odds& = 1:FOR x = 1 TO coloums: odds& = odds& * ((rows + 1) - x) / x:NEXT:CLS : PRINT odds&

This is an algorithm for finding the odds of a lottery wheel.

That looks like a cool algorithm, EXCEPT it is not on only one line of code, as per the original rules for this thread, which stated:

4) Multiple statements on one line; like:
x=x+1 : x=x and 15
are not considered as one line of code.

The idea of one line algorithms is that they're simple, most people can immediately understand them, they serve as an example for similar simple logic, and people can later use them in their programs when needed.
*****
Moneo:

Here's another one-liner to turn a switch on and off, in accordance with, and I quote you, "Most programmers consider TRUE as -1, and FALSE as 0."

x = -1 - x

Starting with x = 0, the next time it encounters the above, the new value will be, x = - 1, then, x = 0, then, x = -1, and so on...

In other words, as long as the previous value of x is 0 or -1, the one-liner will toggle the value of x to either -1 or 0, etc.
Quote:
Ralph Wrote:In the spirit of the originally stated "to turn a switch on and off", would the very often used following line qualify?
Code:
IF A = 1 THEN A = 0 ELSE A = 1
Yeah, that works Ralph, but we have been considering as algorithms that code which does not have any IF's. That is, strictly computational, either arithmetic or logical.
*****

Code:
A = 1 - A
Did someone already post this one?

Code:
A = NOT A
Quote:
Moneo Wrote:
Ralph Wrote:In the spirit of the originally stated "to turn a switch on and off", would the very often used following line qualify?
Code:
IF A = 1 THEN A = 0 ELSE A = 1
Yeah, that works Ralph, but we have been considering as algorithms that code which does not have any IF's. That is, strictly computational, either arithmetic or logical.
*****

Code:
A = 1 - A

Yeah, Nathan, that's very good. But we should always define that this algorithm will flip a switch which is either 0 or 1. BTW I forgot to mention this in the examples of the original rules of the thread.

NOTE: Speaking of those examples, the following code is incorrect for flipping a 0 or 1 switch called sw:
sw = sw xor sw
It should be:
sw = sw xor 1

I'm surprised nobody caught that.
*****
Quote:Moneo:

Here's another one-liner to turn a switch on and off, in accordance with, and I quote you, "Most programmers consider TRUE as -1, and FALSE as 0."

x = -1 - x
Very good Ralph, I never saw that before.
*****
Quote:Did someone already post this one?

Code:
A = NOT A
Yes, LooseCaboose posted it back in the early days of the thread. But that's ok, it happens to be my favorite. Of course we're talking about flipping a switch that's 0 or -1.
*****
Quote:
na_th_an Wrote:
Moneo Wrote:
Ralph Wrote:In the spirit of the originally stated "to turn a switch on and off", would the very often used following line qualify?
Code:
IF A = 1 THEN A = 0 ELSE A = 1
Yeah, that works Ralph, but we have been considering as algorithms that code which does not have any IF's. That is, strictly computational, either arithmetic or logical.
*****

Code:
A = 1 - A

Yeah, Nathan, that's very good. But we should always define that this algorithm will flip a switch which is either 0 or 1. BTW I forgot to mention this in the examples of the original rules of the thread.

NOTE: Speaking of those examples, the following code is incorrect for flipping a 0 or 1 switch called sw:
sw = sw xor sw
It should be:
sw = sw xor 1

I'm surprised nobody caught that.
*****

Sure, that's what it does. Flip 0 to 1, 1 to 0... It was just an oneliner for the multiline IF version.
Heres one that CoderJeff posted over at freebasic.net in a discussion on working out the angle between two points.

Code:
angle = Atan2(v.x - u.x, -(v.y - u.y)) * (180 / PI)

It returns a value from -180 To 180, so needs a small adjustment if you want to use 0-360, but i thought this was a great one liner.

Code:
If angle < 0 Then angle += 360

Anonymous

i saw this on fb,.net also. but, shouldnt it be angle += 360, regardless? because the way you have it now, -120, say, and 60, will return the same value.

angle = ( ( Atan2(v.x - u.x, -(v.y - u.y)) * (180 / PI) + 360 ) mod 360 )

not tested ;p
Pages: 1 2 3 4 5 6 7 8 9 10