Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is lots of information about the Fibonacci Sequence on <a href="http://en.wikipedia.org/wiki/Fibonacci_number" rel="noreferrer">wikipedia</a> and on <a href="http://mathworld.wolfram.com/FibonacciNumber.html" rel="noreferrer">wolfram</a>. A lot more than you may need. Anyway it is a good thing to learn how to use these resources to find (quickly if possible) what you need.</p> <h2>Write Fib sequence formula to infinite</h2> <p>In math, it's given in a recursive form:</p> <p><img src="https://upload.wikimedia.org/math/7/6/f/76f99713cf111eb035d908228c351710.png" alt="fibonacci from wikipedia"></p> <p>In programming, <strong>infinite</strong> doesn't exist. You can use a recursive form translating the math form directly in your language, for example in Python it becomes:</p> <pre><code>def F(n): if n == 0: return 0 elif n == 1: return 1 else: return F(n-1)+F(n-2) </code></pre> <p>Try it in your favourite language and see that this form requires <strong>a lot</strong> of time as n gets bigger. In fact, this is O(2<sup>n</sup>) in time.</p> <p>Go on on the sites I linked to you and will see this (on <a href="http://mathworld.wolfram.com/images/equations/FibonacciNumber/NumberedEquation6.gif" rel="noreferrer">wolfram</a>):</p> <p><a href="https://i.stack.imgur.com/SPYOU.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/SPYOU.gif" alt="Fibonacci Equation"></a></p> <p>This one is pretty easy to implement and very, very fast to compute, in Python:</p> <pre><code>from math import sqrt def F(n): return ((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5)) </code></pre> <p>An other way to do it is following the definition (from <a href="http://en.wikipedia.org/wiki/Fibonacci_number" rel="noreferrer">wikipedia</a>):</p> <blockquote> <p>The first number of the sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the previous two numbers of the sequence itself, yielding the sequence 0, 1, 1, 2, 3, 5, 8, etc.</p> </blockquote> <p>If your language supports iterators you may do something like:</p> <pre><code>def F(): a,b = 0,1 while True: yield a a, b = b, a + b </code></pre> <h2>Display startNumber to endNumber only from Fib sequence.</h2> <p>Once you know how to generate Fibonacci Numbers you just have to cycle trough the numbers and check if they verify the given conditions.</p> <p>Suppose now you wrote a f(n) that returns the n-th term of the Fibonacci Sequence (like the one with sqrt(5) )</p> <p>In most languages you can do something like:</p> <pre><code>def SubFib(startNumber, endNumber): n = 0 cur = f(n) while cur &lt;= endNumber: if startNumber &lt;= cur: print cur n += 1 cur = f(n) </code></pre> <p>In python I'd use the iterator form and go for:</p> <pre><code>def SubFib(startNumber, endNumber): for cur in F(): if cur &gt; endNumber: return if cur &gt;= startNumber: yield cur for i in SubFib(10, 200): print i </code></pre> <p>My hint is to <em>learn to read</em> what you need. Project Euler (google for it) will train you to do so :P Good luck and have fun!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload