Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge: Algorithms having only one line of code.
#31
Sorry, LooseCaboose, but this challenge is for Basic programs exclusively. Otherwise we would need every compiler in the universe to check out submitted code entries.
*****
Reply
#32
Quote:Sorry, LooseCaboose, but this challenge is for Basic programs exclusively.

Oh well, I was just being pedantic because your rules don't state what langauge I have to write in. I have tested the code an it does work.

Like I said, its really hard to write one line algorithms in basic because all of the constructs (if, while, select, etc) require more than one line. Na_th_an's modulo counter was cool and Oracle's bit flip thing was neat, but as I said before the following works almost as well for flipping between an on and off state:
Code:
x = NOT x
The advantage of Oracle's approach is that any non-zero value will be flipped to zero, whereas my example flips between x and -x only.

Antoni's and Na_th_an's recursive functions are very clever, but don't actually obey your rules because they both use either the colon line separator or three lines with a proper function body, however seeing as you are accepting them heres one which calculates the sum of the values 1 to n:
Code:
function sum%(n as integer)
  if n = 1 then sum% = 1 else sum% = n + sum%(n - 1)
end function
esus saves.... Passes to Moses, shoots, he scores!
Reply
#33
Quote:What is Statlib?
*****

StatLib was born out of the statistical challenge mentioned before (which na_th_an won), and also out of a wonderful idea by Neo (who is away on holiday at the moment). Basically put, it is a library used for calculating many complicated statistical functions, like factorials, binomial theorem, array sorting and graph drawing, but the difference is Neo's idea, which stores all numbers as strings instead of the typical data types, making calculation of numbers like 2000! and the 3000th fibonacci number a snap in QBasic.

Unfortunately I'm very busy at the mo so I'm ignoring it most of the time, but if I had a spare day or two the engine would be done and then only the actual statistical functions would be left.
Reply
#34
Quote:..... but as I said before the following works almost as well for flipping between an on and off state:
Code:
x = NOT x
Yery good, LooseCaboose. Believe it or not, your aproach is probably the most commonly used. Most programmers consider TRUE as -1, and FALSE as 0. In that manner they can say: if x then .... when they're testing for true. But you have to be careful because the if x then .... will consider x as true for ANY NON-ZERO VALUE.

In order for your fliipflop to work right, you must start out with x equal to 0 or -1, use your code to flip it, and only reset it, if needed, to 0 or -1.

To make the code more understandable, I usually do:
Code:
defint a-z

const false = 0
const true  = not(false)

switch = false   'asumming we want to start off as false
.....
if switch = false then ..... 'to test false
.....
if switch = true then .....  'to test true
.....
switch = not switch         'to flip it (your way)
.....
switch = true                   'to unconditionally set to true at any time
*****
Reply
#35
To do a bullet proof flip-flop in QB where -1 is TRUE and 0 is FALSE (for convention, as -1 has all bits set)...

Code:
x% = NOT -ABS(SGN(x%))

That way, if x%<>0 -> it will turn 0 (FALSE), and if x%=0 it will turn -1 (TRUE).

I know that any nonzero value is equal to an IF, but if you are gonna do bitwise operations later you'll have problems if that number isn't -1 (all bits set).
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#36
Aah.. I've been looking for something like that...

Ye olde random number generator function... very old this...

Code:
DEF FNran (x) = INT(RND(1) * x) + 1

Calculate pi exactly

Code:
pi# = 4 * ATN(1)

Both pretty useless, but there they are...
Reply
#37
Your one liner to generate PI is pretty slick. I never saw that before. I checked your reslult against a calculator program, and it matches exactly to 15 decimal places. Excellent!

Regarding the random number generator, I'll have to assume it works 'cause it's very hard to check out; that is, how truly random is a random number?
*****
Reply
#38
There's another pi-generator formula, MariuZ and I made, but if you do it in QB, you will have to code a new Sinus function, because the formula uses angles expressed in degrees and not in radians:

Code:
Pi = n * Sin(180°/n)

n is the number of edges of a polygon...the higher n is, the exacter Pi will be generated....
B 4 EVER
Reply
#39
That doesn't work at all...
am an asshole. Get used to it.
Reply
#40
you can't use the Sin function of QB, I said this...you must take a sin function that uses degrees and not radians...try it
B 4 EVER
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)