Qbasicnews.com
February 22, 2020, 06:56:06 AM
 Pages: 1 2 [3] 4 5 ... 7
 Author Topic: Challenge: Algorithms having only one line of code.  (Read 34943 times)
Moneo
Na_th_an

Posts: 1971

 « Reply #30 on: June 27, 2003, 07:08:32 PM »

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.
*****
 Logged
LooseCaboose
I hold this place together

Posts: 981

 « Reply #31 on: June 27, 2003, 09:45:32 PM »

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
 Logged

esus saves.... Passes to Moses, shoots, he scores!
oracle
*/-\*

Posts: 3652

 « Reply #32 on: June 27, 2003, 09:50:16 PM »

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

Moneo
Na_th_an

Posts: 1971

 « Reply #33 on: June 27, 2003, 11:04:29 PM »

Quote from: "LooseCaboose"

..... 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

*****
 Logged
na_th_an
*/-\*

Posts: 8244

 « Reply #34 on: June 28, 2003, 01:12:55 AM »

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).
 Logged

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

Posts: 3652

 « Reply #35 on: June 28, 2003, 03:07:21 AM »

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...
 Logged

Moneo
Na_th_an

Posts: 1971

 « Reply #36 on: June 28, 2003, 07:10:09 PM »

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?
*****
 Logged
ak00ma
Ancient Guru

Posts: 669

 « Reply #37 on: June 29, 2003, 06:29:06 AM »

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....
 Logged

B 4 EVER
Ninkazu
Been there, done that

Posts: 1169

 « Reply #38 on: June 29, 2003, 03:02:53 PM »

That doesn't work at all...
 Logged

am an asshole. Get used to it.
ak00ma
Ancient Guru

Posts: 669

 « Reply #39 on: June 29, 2003, 04:53:24 PM »

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
 Logged

B 4 EVER
DrV
Na_th_an

Posts: 1553

 « Reply #40 on: June 29, 2003, 05:01:56 PM »

to have a sine that uses degrees, you must convert the degrees to radians at some point if you wish to use the internal SIN function, and to do this, you need to know the value of Pi...
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #41 on: June 29, 2003, 07:39:36 PM »

I took out the degrees sign (º), and tried it in QuickBasic, like so:
Pi = n * Sin(180/n)

Pi is defined as double, and n as integer.

Using n=4, it gives an answer of 3.4036-----
Using n=256, it gives.............. 165.5307-----

Something is not right.
*****
 Logged
DrV
Na_th_an

Posts: 1553

 « Reply #42 on: June 29, 2003, 08:07:14 PM »

See ak00ma's post above... you must use a sine function that takes degrees as an argument, and you can't really write one without either making a lookup table, or using the builtin SIN function, using PI so you can convert degrees to radians...  so whip out your old algebra book and start typing...
 Logged
oracle
*/-\*

Posts: 3652

 « Reply #43 on: June 29, 2003, 08:53:35 PM »

That's the trouble isn't it... to convert from degrees to radians you have to have pi, and you are trying to calculate pi anyway, so how can you get the answer anyway, when the answer is part of the question?

Also, the random function is not really random, we had this discussion a little while ago somewhere (me, Hex, Glenn, Aga), all the random function does is go through 16 million numbers and repeat. RANDOMIZE TIMER just chooses a different starting point...
 Logged

Moneo
Na_th_an

Posts: 1971

 « Reply #44 on: June 29, 2003, 11:35:40 PM »

Ok, let's get back to the topic. We haven't had a one-line algorithm since Oracle's calculation for PI.

A few days ago I posted the following:
Code:
Given a positive integer number N, compute the next multiple of X:
Result to M.

defint a-z

M = int((N+X)/X)*X

Note: "next" multiple means that if you're already at a multiple, you move up to the next multiple. Example: if N=5 and X=5, the result will be 10.

If you got that, then make a slight change to the algorithm to compute the "nearest" multiple. Example: if n=5 and x=5, the result is 5 because it is the nearest (you're already there). This is like rounding --- if it's already rounded, then that's the answer.

Nobody came up with the method of computing the "nearest" multiple, so here it is:
Code:

M = int((N+X-1)/X)*X

*****
 Logged
 Pages: 1 2 [3] 4 5 ... 7