Note that there are some explanatory texts on larger screens.

plurals
  1. POAlgorithm for k-Fibonacci
    primarykey
    data
    text
    <p>We all know fibonacci series, when k = 2.</p> <p>I.e.: <code>1,1,2,3,5,8,13</code></p> <p>But this is the 2-fibonacci. Like this, I can count the third-fibonacci:</p> <pre><code>1,1,2,4,7,13,24 </code></pre> <p>And the 4-fibonacci:</p> <pre><code>1,1,2,4,8,15,29 </code></pre> <p>...and so goes on</p> <p>What I'm asking is an algorithm to calculate an 'n' element inside a k-fibonacci series.</p> <p>Like this: if I ask for <code>fibonacci(n=5,k=4)</code>, the result should be: <code>8</code>, i.e. the fifth element inside the 4-fibonacci series.</p> <p>I didn't found it anywhere web. A resouce to help could be <a href="http://mathworld.wolfram.com/Fibonaccin-StepNumber.html" rel="noreferrer">mathworld</a></p> <p>Anyone? And if you know python, I prefer. But if not, any language or algorithm can help.</p> <p>Tip I think that can help: Let's analyze the k-fibonacci series, where k will go from 1 to 5</p> <pre><code>k fibonacci series 1 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, ... 2 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... 3 1, 1, 2, 4, 7, 13, 24, 44, 81, ... 4 1, 1, 2, 4, 8, 15, 29, 56, 108, ... 5 1, 1, 2, 4, 8, 16, 31, 61, 120, ... </code></pre> <p>Analyzing this, we can see that the array [0:k] on the k-fibonacci series is equal to the previous fibonacci series, and it goes on till the k=1</p> <p>i.e. (I'll try to show, but I'm not finding the right way to say it):</p> <pre><code>k fibonacci series 1 1, 2 1, 1, 3 1, 1, 2, 4 1, 1, 2, 4, 5 1, 1, 2, 4, 8, </code></pre> <p>Hope I've helped somehow to solve this.</p> <p><strong>[SOLUTION in python (if anyone needs)]</strong></p> <pre><code>class Fibonacci: def __init__(self, k): self.cache = [] self.k = k #Bootstrap the cache self.cache.append(1) for i in range(1,k+1): self.cache.append(1 &lt;&lt; (i-1)) def fib(self, n): #Extend cache until it includes value for n. #(If we've already computed a value for n, we won't loop at all.) for i in range(len(self.cache), n+1): self.cache.append(2 * self.cache[i-1] - self.cache[i-self.k-1]) return self.cache[n] #example for k = 5 if __name__ == '__main__': k = 5 f = Fibonacci(k) for i in range(10): print f.fib(i), </code></pre>
    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.
 

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