Eneko Alonso

un Navarro en California

Projects

¿Eres español y vives fuera de España? ¿Estás pensando en salir una temporada a trabajar o estudiar en el extranjero? Si es así, no dejes de visitar Spaniards.es, la Comunidad de Españoles en el Mundo
spaniards.es

Recent comments

15:46 America/Los_Angeles


I love recursion

Since the first time I used it, many, many years ago, I loved recursion. Recursion is so powerful than can give you a stack overflow (hahaha... silly joke).

I was studying some algorithms when I saw this non-recursive-but-still-pretty-cool implementation of a GCD algorithm. GCD as you know calculates the gratest common divisor between two numbers. Piece of cake, but fun to play with :)

Here is the non recursive version:

7 function GCD( x, y) {
8      while(y>0) {
9          x = x%y;
10          x^=y;y^=x;x^=y; // XOR swap
11      }
12      return x;
13 }

And here is the recursive version:

23 function GCD(x,y) {
24    return (y==0)? x : GCD(y, x%y);
25 }

As I said, I love recursion, so I will pick the second version :)

You can test both of them on tide4javascript.com, which has a pretty good step-by-step javascript debugger, alongside with a lot of algorithms you may want to study :)

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.