Qbasicnews.com
October 18, 2019, 02:00:27 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 ... 4 5 [6] 7
  Print  
Author Topic: Challenge: Algorithms having only one line of code.  (Read 33791 times)
Moneo
Na_th_an
*****
Posts: 1971


« Reply #75 on: March 28, 2006, 10:41:23 PM »

Quote from: "Ralph"
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.
*****
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #76 on: March 28, 2006, 10:45:35 PM »

Quote from: "Dr_Davenstein"
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.
*****
Logged
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #77 on: March 29, 2006, 09:01:58 AM »

Quote from: "Moneo"
Quote from: "na_th_an"
Quote from: "Moneo"
Quote from: "Ralph"
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.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
yetifoot
Ancient Guru
****
Posts: 575



« Reply #78 on: March 29, 2006, 01:57:21 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
Logged

EVEN MEN OF STEEL RUST.
Anonymous
Guest
« Reply #79 on: March 29, 2006, 02:09:10 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
Logged
yetifoot
Ancient Guru
****
Posts: 575



« Reply #80 on: March 29, 2006, 02:19:19 PM »

if you use mod then it reverts to integer, so i prefer the other way to keep the double precision.  It only needs the +360 when it returns a value from -180 To 0 which is why i used the < 0 compare.   Your version is a true one liner though which is cool.
Logged

EVEN MEN OF STEEL RUST.
Anonymous
Guest
« Reply #81 on: March 29, 2006, 02:24:43 PM »

yeah... well coderJeff showed us both up with this one:

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


haha, that guys a ninja


edit upon testing, that didn't work! so i borrowed a bit from dr_d, that is, making radian transform a literal. here ya go:

Code:
angle =  180 - ATan2 ( v.x - u.x, v.y - u.y ) * 57.29577951308232
Logged
yetifoot
Ancient Guru
****
Posts: 575



« Reply #82 on: March 29, 2006, 02:46:22 PM »

Quote
angle =  180 - ATan2 ( v.x - u.x, v.y - u.y ) * 57.29577951308232


this one gives me the wrong answer when angle = 0, it returns -6.24.....e015 or something
Logged

EVEN MEN OF STEEL RUST.
Anonymous
Guest
« Reply #83 on: March 29, 2006, 02:52:39 PM »

correct you are. but i mean thats like

.0000000000000006


...so its pretty close to zero, hahahah. all other angles work.
Logged
neuro
Forum Regular
**
Posts: 114



WWW
« Reply #84 on: March 29, 2006, 04:08:42 PM »

Here's my one-liner:

Draw a circle, without using the CIRCLE command:

Code:
SCREEN 12: h = 200: k = 200: r = 100: c = 13: FOR x = 0 TO INT(r / SQR(2)) + 1: y = SQR(r ^ 2 - x ^ 2): FOR a = -1 TO 1 STEP 2: FOR b = -1 TO 1 STEP 2: PSET (h + x * a, k + y * b), c: PSET (h + y * a, k + x * b), c: NEXT: NEXT: NEXT


draws a circle with center at (h, k), radius r and color c without using the CIRCLE command, or SIN or COS, and just two PSET commands.

It may seems like a long line but it fits inside a QBASIC IDE line of code. ;)

 - neuro


[edit]: I see one of the rules is, no compounded lines (with colons)...

Here's another of my favorite algorithms:

purpose: hang the computer uninterruptibly (written in C):

   for ( ; ; fork() ) ;

or this one, which goes on and on 4ever... (or just uses up all stack space):

int func(int x) return func(x+1);
Logged

ignatures suck
Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #85 on: March 29, 2006, 04:13:30 PM »

Old thread, I don't know if i have posted this before:
Insert this in a line printing loop to print page by page
Code:

if lcnt=maxlines then print "Press a key";: x$=input$(1):lcnt=0:cls else lcnt=lcnt+1
Logged

Antoni
Moneo
Na_th_an
*****
Posts: 1971


« Reply #86 on: March 29, 2006, 05:26:31 PM »

Quote from: "Antoni Gual"
Old thread, I don't know if i have posted this before:
Insert this in a line printing loop to print page by page
Code:

if lcnt=maxlines then print "Press a key";: x$=input$(1):lcnt=0:cls else lcnt=lcnt+1

Nice bit of code, Antoni, but it's not one line of code by the established rules.
*****
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #87 on: March 29, 2006, 11:24:58 PM »

Code:
x = y * (((z AND 1) = 1) OR 1)


x = -y or + y depending on z being odd or even
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Moneo
Na_th_an
*****
Posts: 1971


« Reply #88 on: March 30, 2006, 08:52:15 PM »

Quote from: "relsoft"
Code:
x = y * (((z AND 1) = 1) OR 1)


x = -y or + y depending on z being odd or even

Ok, but what would I use this algorithm for?
*****
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #89 on: March 31, 2006, 03:09:34 AM »

GFX effects. :*)

Mostly plasmas and sinewave displacement.
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Pages: 1 ... 4 5 [6] 7
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!