Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge: Algorithms having only one line of code.
#21
Quote: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
*****
Reply
#22
Quote: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?
*****
Reply
#23
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.K...ml#Rabbits
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#24
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.
*****
Reply
#25
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.
Reply
#26
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).
esus saves.... Passes to Moses, shoots, he scores!
Reply
#27
now make a one-line quicksort algorithm that sorts two linked arrays. (eg. "Joe", 90, "Aga", 89.9999, "Wildcard", 101.1

:o
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.
Reply
#28
What is Statlib?
*****
Reply
#29
Quote: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.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#30
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.
*****
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)