LaRoza

December 24th, 2007, 08:32 AM

I thought I would add something new to this forum, a discussion on algorithms.

This topic is often overlooked by those learning and is probably the most important thing to learn. Syntax is useless without them. Before this thread hopefully delves deep into the subject, I will start with a simple discussion.

To start the discussion, I will introduce this common and rather simple algorithm. I just was brushing up on my Fortran as you can tell. It is a simple syntax, and you should be able to follow it if you ever used any other statically typed language.

Forgive the odd variable names, but "result" and "sum" are keywords. Also forgive the use of a goto. Although the use of this programming tool is greatly discouraged, Fortran (this is 1977, you know) relies on it. In case you can't tell, it is a "for" loop, rewritten in Fortran.

This algorithm calculated Fibonacci numbers.

double precision function Fibonacci (x)

double precision x,su,i

integer previous, resul

i = 0

previous = -1

resul = 1

10 if (i .le. x) then

i = i + 1

su = resul + previous

previous = resul

resul = su

goto 10

end if

Fibonacci = resul

return

end

Can anyone give any alternatives to this or improve on this, in any language of course.

As you can see, this algorithm is not recursive, even though the Fibonacci series is defined in a recursive manner. Recursion is not used for two reasons:

0. It greatly reduces the efficiency of the program because the program would spawn two more function calls for any number greater than 1, which in turn would call two and etc.

You can guess what the second reason is.

This topic is often overlooked by those learning and is probably the most important thing to learn. Syntax is useless without them. Before this thread hopefully delves deep into the subject, I will start with a simple discussion.

To start the discussion, I will introduce this common and rather simple algorithm. I just was brushing up on my Fortran as you can tell. It is a simple syntax, and you should be able to follow it if you ever used any other statically typed language.

Forgive the odd variable names, but "result" and "sum" are keywords. Also forgive the use of a goto. Although the use of this programming tool is greatly discouraged, Fortran (this is 1977, you know) relies on it. In case you can't tell, it is a "for" loop, rewritten in Fortran.

This algorithm calculated Fibonacci numbers.

double precision function Fibonacci (x)

double precision x,su,i

integer previous, resul

i = 0

previous = -1

resul = 1

10 if (i .le. x) then

i = i + 1

su = resul + previous

previous = resul

resul = su

goto 10

end if

Fibonacci = resul

return

end

Can anyone give any alternatives to this or improve on this, in any language of course.

As you can see, this algorithm is not recursive, even though the Fibonacci series is defined in a recursive manner. Recursion is not used for two reasons:

0. It greatly reduces the efficiency of the program because the program would spawn two more function calls for any number greater than 1, which in turn would call two and etc.

You can guess what the second reason is.