Posts: 1,956
Threads: 65
Joined: Jun 2003
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.
*****
Posts: 544
Threads: 27
Joined: Jan 2005
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.
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Posts: 6,419
Threads: 74
Joined: Mar 2002
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.
*****
Posts: 1,774
Threads: 62
Joined: Aug 2003
Did someone already post this one?
Posts: 1,956
Threads: 65
Joined: Jun 2003
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.
*****
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.
*****
Posts: 1,956
Threads: 65
Joined: Jun 2003
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.
*****
Posts: 1,956
Threads: 65
Joined: Jun 2003
Quote:Did someone already post this one?
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.
*****
Posts: 6,419
Threads: 74
Joined: Mar 2002
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.
*****
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.
Posts: 484
Threads: 14
Joined: Apr 2005
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
EVEN MEN OF STEEL RUST.
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
|