Qbasicnews.com
May 30, 2020, 12:04:36 PM
 Pages: 1 [2] 3 4 ... 7
 Author Topic: Challenge: Algorithms having only one line of code.  (Read 35788 times)
Moneo
Na_th_an

Posts: 1971

 « Reply #15 on: June 25, 2003, 06:33:46 PM »

Antoni, What I tested was the your one line of code in between a standard Function definition line and a standard End Function line.
It's still a one line function.
*****
 Logged
Antoni Gual
Na_th_an

Posts: 1434

 « Reply #16 on: June 25, 2003, 06:55:40 PM »

Thanks for considering it like this!
 Logged

Antoni
Agamemnus
x/ \z

Posts: 3491

 « Reply #17 on: June 25, 2003, 10:15:07 PM »

HEY! NO FAIR!

IT's THREE LINES OF CODE!
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
seph
Na_th_an

Posts: 1915

 « Reply #18 on: June 26, 2003, 01:57:29 AM »

I don't understand what that function does or what a factoral is. Please explain EVERYTHING IN THE UNIVERSE to me.

Nevermind. Some jerk named dictionary.com very rudely explained it to me...
 Logged

earn.
na_th_an
*/-\*

Posts: 8244

 « Reply #19 on: June 26, 2003, 09:59:52 AM »

Well, in the line of Antoni's, here's my very own fibonnaci series callculator:

Code:
FUNCTION fibonacci& (n&)
IF n& < 2 THEN fibonacci& = 1 ELSE fibonacci& = fibonacci&(n& - 1) + fibonacci&(n& - 2)
END FUNCTION

You use it this way (for example):

Code:
FOR i& = 0 TO 10
PRINT fibonacci(i&)
NEXT
 Logged

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

Posts: 1971

 « Reply #20 on: June 26, 2003, 01:06:03 PM »

Quote from: "Agamemnus"
HEY! NO FAIR!

IT's THREE LINES OF CODE!

Technically, you're right, but Antoni's approach still merits recognition within the constraints of this challange.

Your approach also deserves merit for its simplicity, even though it needs the extra GOTO instruction to work.
I recommend making it a little shorter as follows:
Code:

REM 1 IF fact& = 2 THEN fact2& = fact2& * 2 ELSE fact2& = fact2& * fact&: fact& = fact& - 1: GOTO 1

1 IF fact& > 1 THEN fact2& = fact2& * fact&: fact& = fact& - 1: GOTO 1

*****
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #21 on: June 26, 2003, 03:57:43 PM »

Quote from: "na_th_an"
Well, in the line of Antoni's, here's my very own fibonnaci series callculator:
Code:
FUNCTION fibonacci& (n&)
IF n& < 2 THEN fibonacci& = 1 ELSE fibonacci& = fibonacci&(n& - 1) + fibonacci&(n& - 2)
END FUNCTION

Excellent na-th-an! Here again we have a brilliant recursive function.

How many of you know what a Fibonacci series is? It's a series where any number in the series is the sum of the two preceding numbers., i.e., 1,1,2,3,5,8,13 .....

What would you use a Fibonacci series for? What is a practical use?
*****
 Logged
na_th_an
*/-\*

Posts: 8244

 « Reply #22 on: June 26, 2003, 04:53:30 PM »

Well, it is very used to teach recursion and algorithmics, but it came from a simple problem:

Fibonacci wanted to determine the rate at which pairs of rabbits would reproduce.

Info here : http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibnat.html#Rabbits
 Logged

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

Posts: 1971

 « Reply #23 on: June 26, 2003, 07:15:44 PM »

I agree that Fibonacci first developed his famous series to see the reproduction rate of rabbits.

But, what would I use this series for today in computer programming applications?

I personally have only seen one practical use of the series, and that was for a polyphase merge sort used on an IBM mainframe with only 3 tape drives available. The Fibonacci series determined how many internally sorted "runs" were distributed onto each of the tapes, so that only one of the 3 tapes (the one with the least number of runs) had to be rewound at the end of a given sort pass.

Polyphase merge sorts have become a popular academic topic. Many implementations using more than 3 tapes still continue to use the Fibonacci series. It works, although studies have shown that this series is not the most efficient for over 3 tapes.
*****
 Logged
oracle
*/-\*

Posts: 3652

 « Reply #24 on: June 27, 2003, 04:57:34 AM »

I use the fibonacci series to test StatLib. Though that may not be useful in real life...

StatLib nearly has negative and decimal support! (sorry, spamming...)

The factorial method came up in my other challenge, the statistical one that started statlib off, that's why so many people have been repoducing it. I think only na_th_an should get the recognition for it, because he won.
 Logged

LooseCaboose
I hold this place together

Posts: 981

 « Reply #25 on: June 27, 2003, 05:55:16 AM »

Basic is kinda hopeless for this task for all but the very simplest algorithms because even a loop construct requires more than one line:
Code:

while condition : wend

Futhermore you cant make assignments within the condition part of a loop because Basic interprets this as a test for equality.

So, seeing as your rules didn't actually specify that I have to write Basic code, heres a pseudo bubble sort in one line of Java or C++ code (Wont work in C because you arent allow to declare variables within the expression list of a for loop:
Code:

for(int a[5]={2,4,3,5,1},int t,int i=0;i<4;i++){if(a[i]<a[i+1]){(t=a[i])&&(a[i]=a[i+1])&&(a[i+1]=t)&&(i=-1);}}

Sorts the array a in descending order. It differs from most bubble sort implementations becuase it only uses a single loop (by resetting i every time it does a swap). On the plus side, in the best case (data is already sorted) the algorithm works in linear O(n) time, however in the worst case it runs in O(n ^ 2).
 Logged

esus saves.... Passes to Moses, shoots, he scores!
Agamemnus
x/ \z

Posts: 3491

 « Reply #26 on: June 27, 2003, 09:17:07 AM »

now make a one-line quicksort algorithm that sorts two linked arrays. (eg. "Joe", 90, "Aga", 89.9999, "Wildcard", 101.1

 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Moneo
Na_th_an

Posts: 1971

 « Reply #27 on: June 27, 2003, 02:39:42 PM »

What is Statlib?
*****
 Logged
na_th_an
*/-\*

Posts: 8244

 « Reply #28 on: June 27, 2003, 05:14:21 PM »

Quote from: "Moneo"
But, what would I use this series for today in computer programming applications?

This one is for Moneo. Sorry, it is in spanish. I took this off a web:

Quote
La serie de Fibonacci

Esta serie fue descubierta por un matemático italiano del siglo XIII, llamado Fibonacci. Cada número de la serie es el resultado de la suma de los dos anteriores. Veamos que aspecto tiene:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

Esta serie posee propiedades muy interesantes entre las cuales vamos a destacar la relación existente entre los números que la componen:
si dividimos los números que son consecutivos de la serie, es decir, 1/1, ½, 2/3, 3/5, 5/8, 8/13, etc. veremos que el resultado obtenido tiende al número 0.618.

si dividimos los números no consecutivos de la serie, es decir, ½, 1/3, 2/5, 3/8, 5/13, 8/21, etc. veremos que el resultado obtenido tiende al número 0.382.

si calculamos ahora la razón de cualquier número de la serie al siguiente número más bajo, es decir, 21/13, 13/8, 8/5 ... el resultado tiende a 1.618, que es el inverso de 0.618.

si calculamos ahora la razón de cualquier número de la serie al siguiente número más bajo no consecutivo, es decir, 21/8, 13/5, 8/3 ... el resultado tiende a 2.618, que es el inverso de 0.382.

De este modo hemos obtenido un conjunto de ratios que ya eran conocidos y utilizados con anterioridad a Fibonacci por los antiguos griegos y egipcios. Conceptos derivados de estos ratios como la proporción áurea fueron aplicadas en construcciones como la pirámide de Gizeh o el Partenón. Nosotros vamos a mostrar como estos ratios van a tener aplicación directa como herramientas de análisis a la hora de determinar porcentajes de corrección contra la tendencia principal. ¿Dónde está el punto de unión de lo expuesto en esta primera parte y el mercado de valores? La explicación que podemos ofrecer a priori resulta un poco esotérica pero confiamos en que los lectores, con el tiempo, y el uso de esta técnica, constaten de modo empírico su utilidad. Mientras tanto, la idea que subyace tras esta serie es que la espiral logarítmica que puede construirse en base a ella, describe parte de la figura de crecimiento que se puede apreciar en todo el Universo. Ejemplos de manifestación de esta espiral se ven en las conchas, el caparazón del caracol, la forma de la galaxia, el oído humano. Por tanto, se cree que el mercado de valores, además de representar un ejemplo del comportamiento de la masa humana, es una manifestación del fenómeno de crecimiento natural que caracteriza todo progreso humano.
 Logged

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

Posts: 1971

 « Reply #29 on: June 27, 2003, 06:30:28 PM »

Thanks, na_th_an,
There is a vast amount of information published regarding the Fibonacci series, but mostly scientific. Only a few very elite computer programmers are afforded the opportunity to participate in such scientific projects. So, aside from the polyphase merge which I already mentioned, I would like to know of other "practical" applications.
*****
 Logged
 Pages: 1 [2] 3 4 ... 7