Challenge: Algorithms having only one line of code. Moneo Posting Freak Posts: 1,956 Threads: 65 Joined: Jun 2003 03-28-2006, 12:55 AM 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. ***** Ralph Senior Member Posts: 544 Threads: 27 Joined: Jan 2005 03-28-2006, 08:05 AM 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. na_th_an Posting Freak Posts: 6,419 Threads: 74 Joined: Mar 2002 03-28-2006, 07:02 PM 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` SCUMM (the band) on Myspace! ComputerEmuzone Games Studio underBASIC, homegrown musicians [img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i Dr_Davenstein Posting Freak Posts: 1,774 Threads: 62 Joined: Aug 2003 03-29-2006, 03:30 AM Did someone already post this one? Code:`A = NOT A` Moneo Posting Freak Posts: 1,956 Threads: 65 Joined: Jun 2003 03-29-2006, 05:56 AM 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. ***** Moneo Posting Freak Posts: 1,956 Threads: 65 Joined: Jun 2003 03-29-2006, 06:41 AM 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. ***** Moneo Posting Freak Posts: 1,956 Threads: 65 Joined: Jun 2003 03-29-2006, 06:45 AM 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. ***** na_th_an Posting Freak Posts: 6,419 Threads: 74 Joined: Mar 2002 03-29-2006, 05:01 PM 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. SCUMM (the band) on Myspace! ComputerEmuzone Games Studio underBASIC, homegrown musicians [img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i yetifoot Senior Member Posts: 484 Threads: 14 Joined: Apr 2005 03-29-2006, 09:57 PM 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. Anonymous Unregistered   03-29-2006, 10:09 PM 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 « Next Oldest | Next Newest »