Fibonacci - Recursion - Pencil Code

52 views
Skip to first unread message

sunnyd...@gmail.com

unread,
Sep 27, 2015, 6:30:50 PM9/27/15
to Pencil Code
I want to create fibonacci series with the pencil code and with recursion. Any help?

David Bau

unread,
Sep 27, 2015, 10:21:36 PM9/27/15
to sunnyd...@gmail.com, Pencil Code
Google for [fibonacci pencil code] - the first example by stezel is nice.  Other things in that directory are also pretty useful.

On Sun, Sep 27, 2015 at 6:30 PM, <sunnyd...@gmail.com> wrote:
I want to create fibonacci series with the pencil code and with recursion. Any help?

--
You received this message because you are subscribed to the Google Groups "Pencil Code" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pencilcode+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Bau

unread,
Sep 28, 2015, 1:28:42 PM9/28/15
to Sunny Desai, Pencil Code
+the group.

Hello Sunny - it turns out that everything that is done with "await" can be done in plain javascript without "await", although it can be a little more inconvenient.

The key thing to understand is that, in javascript, functions like "readnum" do not return a value: instead, they call a callback function to provide its value.

So you could write something like this:


function fibonacci(n) {
  if (n <= 2) {
    return 1;
  } else {
    return fibonacci(n-1) + fibonacci(n-2);
  }
}

write("This program generates the Fibonacci Sequence");
write("How far in the sequence do you wish to see?");
read(function(N) {
  for (var j = 1; j <= N; ++j) {
    write(fibonacci(j));
  }
});


Note the last call to "read" takes a single argument, which is the function defining what to do after the user enters a value.

"await" is very helpful for learning, and that is one of the reasons I like beginning in CoffeeScript before moving to Javascript.   Javascript requires understanding callback functions very early on.

David

On Mon, Sep 28, 2015 at 3:35 AM, Sunny Desai <sunnyd...@gmail.com> wrote:
I am trying to create with javascript. Some of the options like await readnum is not available. Even I want to use return. I will really appreciate if you can help. I can provide you the code with which I am trying to achieve it.

Regards,
Sunny Desai

Reply all
Reply to author
Forward
0 new messages