Qbasicnews.com

Full Version: Challenge: Algorithms having only one line of code.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10
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...
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.
*****
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...
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...
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
*****
I already made a function to do that for statlib... pity I'll have to rewrite it because I'm using strings now...

Functions for finding roots of quadratic equations. Two needed: one for each root.

Code:
a = 1
b = 4
c = 2
' equivilant to x²+4x+2

r1 = (-b + (SQR(b ^ 2 - (4 * a * c)))) / (2 * a)
r2 = (-b - (SQR(b ^ 2 - (4 * a * c)))) / (2 * a)
PRINT r1, r2
' returns -1, -3
I only need a sine algorithm that uses degrees and not radians...then I'll code it
Quote:I already made a function to do that for statlib... pity I'll have to rewrite it because I'm using strings now...

Functions for finding roots of quadratic equations. Two needed: one for each root.

Code:
a = 1
b = 4
c = 2
' equivilant to x²+4x+2

r1 = (-b + (SQR(b ^ 2 - (4 * a * c)))) / (2 * a)
r2 = (-b - (SQR(b ^ 2 - (4 * a * c)))) / (2 * a)
PRINT r1, r2
' returns -1, -3
That only finds the real roots... I suppose we'll let you get away with it this time... Wink
I know that... *shoots DrV*. How d'you expect me to find complex roots, or even represent them in QB? I suppose it could be done using the symmetrical property of the roots and polar form, but then it wouldn't be one line.
Here's a rather complex one-liner to determine if a given number N is a power of 2.
Code:
defLNG a-z

if 2^(int(log(N)/log(2)+.5)) = N then print n;" is a power of 2"

Note: My original code did not add the .5 to round the result. However, further testing showed that for certain values of N, like 128, the arithmetic failed. Can anyone tell me why 128 fails without the rounding?
*****
Pages: 1 2 3 4 5 6 7 8 9 10