I solve the problem with a number of Fibonacci (+ negative). Something like this:

fibonacci 0 = 0 fibonacci 1 = 1 fibonacci n | n == 0 = 1 | n > 0 = fibonacci (n - 1) + fibonacci (n - 2) | n < 0 = fibonacci (n + 2) - fibonacci (n + 1) 

need to increase the speed of rendering. for this, you naturally need a battery.

How to do it?

  • 2
    First, solve the recurrence equation defining this sequence and find the formula for the nth term. - typemoon pm
  • I did not understand anything, so the plus. :) - Nick Volynkin

1 answer 1

Most correctly, of course, use the ready-made formula of the n th member :) But for the case when you need to find it programmatically, the following idea fits (I am writing an algorithm, I do not know Haskell's syntax):

 fib n = helper 0 1 n where helper curr prev n | n == 0 = curr | n > 0 = helper (curr+prev) curr (n-1) | n < 0 = helper prev (curr-prev) (n+1) 

It calculates the sequence in linear time with tail recursion.

  • fibhelper would be worth making fibhelper local identifier, not a global one. Something like this: fib n = helper 0 1 n where helper curr prev n = ... - Pavel Mayorov
  • @PavelMayorov: I don’t know the syntax, honestly, and I don’t even have a place to try. If you know how to fix it, correct it right in the answer, ok? (For example, I can not assess the quality of this edit .) - VladD
  • @PavelMayorov: Thank you! - VladD