Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
'alr approved some time ago'
I still don't get where the
(b - 1) * fib(n + 2) = (b - 1) * fib(n) + (b - 1) * fib(n + 1)
comes from and why should I add this to the original equation. Please, explain in details or give me link to a book or some article where it is well-explained.@Technetium_Phenol Oh, C'mon :D It's 21st century, forget Fortran and come to Scala :D
The description is not correct.
If one translates the above definition directly into a recursive function, the result is not very efficient. One can try memoization, but that requires lots of space and is not necessary. So, first step is to try and find a tail recursive definition. In order to do that we try to write both sides of equation 3) on the same form. Currently, the left side of the equation contains a single term, whereas the right side is the sum of two terms. A first attempt is to add fib(n + 1) to both sides of the equation:they have fib(0) = 1
and there code doesn't calculate the fibonnaci series.
Here is my alternative expansion.
The function is recursively defined by:
3. fib(n + 1) + fib(n + 2) = fib(n) + 2 * fib(n + 1)
The two sides of the equation look much more alike, but there is still an essential difference, which is the coefficient of the second term of each side. On the left side of the equation, it is 1 and, on the right, it is 2. To remedy this, we can introduce a variable b:
3. b * fib(n + 1) + b * fib(n + 2) = b * fib(n) + (b + b) * fib(n + 1)
We then subtract both sides by b * fib(n+1)
3. b * fib(n + 2) = b * fib(n) + (b + b)
To try and return the tail to the function we add both sides by a * fib(n+1) to each side:
3. a * fib(n + 1) + b * fib(n + 2) = b * fib(n) + (a + b) * fib(n + 1)
Now the two sides have the same form (call it F), this function has the form:
3. F(c, d, e) = F(a, b, n+1) = F(a, a+b, n) = c * fib(e) + d * fib(e + 1)
(Note if your confused by the above equation substitute the values into the furthest right function and you get)
3. a * fib(n + 1) + b * fib(n + 2) = b * fib(n) + (a + b) * fib(n + 1) = c * fib(n) + d * fib(n + 1)
Which we can simplify to:
3. F(a, b, n + 1) = F(b, a + b, n)
We also have, by definition of F and fib:
4. F(a, b, 0) = a * fib(0) + b * fib(1) = b
Also, by definition of F:5\. fib(n) = F(1, 0, n) with a = 1, b = 0
6\. F(a, b, 1) = a * fib(1) + b * fib(2) = a + b
The next step is to translate the above into code:
The final step (optional for languages that support tail call optimization) is to replace the tail recursive function F with a loop:
(1) that each golfer plays "exactly once every day"
That means same set of players everyday.
Thanks, approved. I don't know a thing about Fortran, so hopefully it's ok... :)
Thank you, if someone raises an issue I'll post the comment in here so that you get a notification, if thats OK.
Thanks once again for the translation.
Thank you so much for the translation. Please could you keep an eye on it for a few weeks, in case anybody raises an issue as I have little exposure with Go.
Use https://github.com/Codewars/codewars-runner-cli/issues/768 for further discussion/updates.
5.4.0
https://packages.ubuntu.com/xenial/gfortran-5We can support multiple versions. So
-std=f95
,-std=f2003
,-std=f2008
, and-std=f2018
can be supported and the version can be enabled for the kata based on if the reference solution and the tests compiles with the version. Other languages works like this.Without
-std=
, it seems to default to-std=gnu
which specifiesWhat "the latest Fortran standard" means depends on the specific version of
gfortran
and I'd like to avoid that so we have better idea what exactly is supported.I'll update
gfortran
to the latest and experiment with options.How did you find this super old post? :/
You can open an issue to request to add those Fortran versions on runner repository. There's an issue template for that.
It currently just does
gfortran -o test solution.f95 tests.f95 && exec ./test
. I'm assuming we can add variations by using-std=f2008
. We can discuss what needs to be done to add support once you open the issue.You can fork it
The explanation does not say to add
b
, but to introduce a variableb
. You havefib(n + 1) + fib(n + 2) = fib(n) + 2 * fib(n + 1)
. Add this equation to the equation(b - 1) * fib(n + 2) = (b - 1) * fib(n) + (b - 1) * fib(n + 1)
and you get:fib(n + 1) + b * fib(n + 2) = b * fib(n) + (b + 1) * fib(n + 1)
. For the next step, add(a - 1) * fib(n + 1)
to both sides of the last equation.