finally,
here's our third node program
which is a little more complicated
which is computing the fibonacci numbers
modifying this program will make it relatively
easy
to solve the prime number problem in your
homework
what you really should do is to
open nano fibonacci.js and type in the following
code
if you click this link over here, you will
find
Zed Shaw talking about how typing in code
is similar to
writing in math on pen and paper
which means you sort of get it in your brain
but just for the purposes of this screencast
we're going to wget the file and run it
but let's walk through this very briefly
here what we're doing is we're defining the
fibonacci numbers
or a function that computes them
this wikipedia article will tell you more
about them
and we're doing so in a little bit of an odd
way
we're setting up a variable equal to a function
over here
it's a very common idiom in javascript
we'll talk about this a little more
but this will serve as an odd function definition
just basic if-else over here
basic cases for n
this is a more sophisticated version of computing
the fibonacci numbers
which actually uses the golden ratio
and some theorums around arithmetic series
you can read this article and kinda see this
formula implements what's written in this
article
the main thing of interest is that it's using
the square root round power or something of
that nature, which might be something
you want to use during your prime number computation
finally, this is just a basic for loop
which loops over and just prints out the first
k fibonacci numbers
and does so
and not prints out, but appends them to an
array
it does so by defining an array
running in a loop
and pushing, which is appending, to the end
passing in i for the i-th value in this array
last thing it does is that it prints all these
results to the console
and it uses this format routine over here
to make this look reasonably good
the execution of the script gives you the
first 20 fibonacci numbers
let's see what that looks like online
this is the same code that we entered in
we can see that if we change it to be executable
and then just execute it
it prints out the first 20 fibonacci numbers
as expected
you can now modify this
and you should be able to solve the primes
problem
