<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Fibonacci Numbers</title>
	<atom:link href="http://programmingpraxis.com/2010/07/30/fibonacci-numbers/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2010/07/30/fibonacci-numbers/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Mon, 28 May 2012 03:30:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Montag</title>
		<link>http://programmingpraxis.com/2010/07/30/fibonacci-numbers/#comment-2815</link>
		<dc:creator><![CDATA[Montag]]></dc:creator>
		<pubDate>Thu, 07 Apr 2011 21:24:12 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2711#comment-2815</guid>
		<description><![CDATA[public void fibonacci(int firstTerm, int secTerm)
    {
        if (nextTerm == 75025) return;
        
        nextTerm = firstTerm + secTerm;
        
        System.out.println(counter+&quot;. &quot;+nextTerm);
        
        fibonacci(secTerm, nextTerm);
    }

Goes to 25th term, based on firstTerm = 0 and secTerm = 1.]]></description>
		<content:encoded><![CDATA[<p>public void fibonacci(int firstTerm, int secTerm)<br />
    {<br />
        if (nextTerm == 75025) return;</p>
<p>        nextTerm = firstTerm + secTerm;</p>
<p>        System.out.println(counter+&#8221;. &#8220;+nextTerm);</p>
<p>        fibonacci(secTerm, nextTerm);<br />
    }</p>
<p>Goes to 25th term, based on firstTerm = 0 and secTerm = 1.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gil Martinez</title>
		<link>http://programmingpraxis.com/2010/07/30/fibonacci-numbers/#comment-2532</link>
		<dc:creator><![CDATA[Gil Martinez]]></dc:creator>
		<pubDate>Sat, 26 Feb 2011 14:10:35 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2711#comment-2532</guid>
		<description><![CDATA[I&#039;ve seen difference equations (e.g., fibonacci) expressed as matrices before, but this was the first time I&#039;ve seen the concepts of logarithmic-time exponentiation, matrices, and difference equations all in one problem. This was a genuinely entertaining exercise; thanks for posting.

Since the core of this problem was essentially finding a O(log(n)) algorithm for matrix multiplication, I also wrote a O(1) solution based upon diagonalization.  Looks like it works, but for very large values, it may suffer from accuracy issues due to the use of inexact numbers.

[sourcecode lang=&quot;css&quot;]
(define (transpose A)
  (apply map list A))

;; crude matrix-mult for 2x2
(define (matrix-mult A B)
  (let ((trans-B (transpose B)))
   (cons 
    (list 
     (apply + (map * (car A) (car trans-B)))
     (apply + (map * (car A) (cadr trans-B))))
    (list
     (list 
     (apply + (map * (cadr A) (car trans-B)))
     (apply + (map * (cadr A) (cadr trans-B)))))
    )))

(define (log_fib n)
  (cond
    ((&gt;= 1 n) &#039;((1 1) (1 0)))
    ((even? n)
     (let ((fib-matrix (log_fib (/ n 2))))
       (matrix-mult fib-matrix fib-matrix)))
    (else
     (let ((fib-matrix (log_fib (quotient n 2))))
       (matrix-mult 
        (matrix-mult fib-matrix fib-matrix)
        &#039;((1 1) (1 0)))))))

(define (PDP_fib n)
  (let* ((eig1 (* 1/2 (+ 1 (sqrt 5))))
         (eig2 (* 1/2 (- 1 (sqrt 5))))
         (P (list 
             (list eig1 eig2)
             (list 1 1)))
         (D (list
             (list (expt eig1 n) 0)
             (list 0 (expt eig2 n )))) 
         (P^-1 (let ((det (/ 1 (- eig1 eig2))))
                 (list (list det (* det eig2 -1)) 
                       (list (* det -1) (* det eig1))))))     
    (round (cadr (car (matrix-mult (matrix-mult P D) P^-1))))))
[/sourcecode]

[ I fixed the formatting.  In the future, look at the code formatting how-to for instructions. Thanks for your kind words, and for an interesting variation on the problem. -- PP ]]]></description>
		<content:encoded><![CDATA[<p>I&#8217;ve seen difference equations (e.g., fibonacci) expressed as matrices before, but this was the first time I&#8217;ve seen the concepts of logarithmic-time exponentiation, matrices, and difference equations all in one problem. This was a genuinely entertaining exercise; thanks for posting.</p>
<p>Since the core of this problem was essentially finding a O(log(n)) algorithm for matrix multiplication, I also wrote a O(1) solution based upon diagonalization.  Looks like it works, but for very large values, it may suffer from accuracy issues due to the use of inexact numbers.</p>
<pre class="brush: css;">
(define (transpose A)
  (apply map list A))

;; crude matrix-mult for 2x2
(define (matrix-mult A B)
  (let ((trans-B (transpose B)))
   (cons 
    (list 
     (apply + (map * (car A) (car trans-B)))
     (apply + (map * (car A) (cadr trans-B))))
    (list
     (list 
     (apply + (map * (cadr A) (car trans-B)))
     (apply + (map * (cadr A) (cadr trans-B)))))
    )))

(define (log_fib n)
  (cond
    ((&gt;= 1 n) '((1 1) (1 0)))
    ((even? n)
     (let ((fib-matrix (log_fib (/ n 2))))
       (matrix-mult fib-matrix fib-matrix)))
    (else
     (let ((fib-matrix (log_fib (quotient n 2))))
       (matrix-mult 
        (matrix-mult fib-matrix fib-matrix)
        '((1 1) (1 0)))))))

(define (PDP_fib n)
  (let* ((eig1 (* 1/2 (+ 1 (sqrt 5))))
         (eig2 (* 1/2 (- 1 (sqrt 5))))
         (P (list 
             (list eig1 eig2)
             (list 1 1)))
         (D (list
             (list (expt eig1 n) 0)
             (list 0 (expt eig2 n )))) 
         (P^-1 (let ((det (/ 1 (- eig1 eig2))))
                 (list (list det (* det eig2 -1)) 
                       (list (* det -1) (* det eig1))))))     
    (round (cadr (car (matrix-mult (matrix-mult P D) P^-1))))))
</pre>
<p>[ I fixed the formatting.  In the future, look at the code formatting how-to for instructions. Thanks for your kind words, and for an interesting variation on the problem. -- PP ]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: programmingpraxis</title>
		<link>http://programmingpraxis.com/2010/07/30/fibonacci-numbers/#comment-2156</link>
		<dc:creator><![CDATA[programmingpraxis]]></dc:creator>
		<pubDate>Thu, 09 Dec 2010 16:32:31 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2711#comment-2156</guid>
		<description><![CDATA[After writing this exercise, I came across &lt;a href=&quot;http://www.cs.utexas.edu/users/EWD/ewd06xx/EWD654.PDF&quot; rel=&quot;nofollow&quot;&gt;Dijkstra&#039;s paper&lt;/a&gt; describing a set of recurrence equations that calculate the &lt;em&gt;n&lt;/em&gt;th fibonacci number.  His method is orthogonal to the matrix method described above, but more convenient for computation.  Note that Dijkstra starts the sequence differently; where we say &lt;em&gt;F&lt;/em&gt;&lt;sub&gt;0&lt;/sub&gt;=0, Dijkstra says &lt;em&gt;F&lt;/em&gt;&lt;sub&gt;0&lt;/sub&gt;=1.  Here&#039;s the code:

&lt;code&gt;(define (fib n)
&#160;&#160;(define (square x) (* x x))
&#160;&#160;(cond ((zero? n) 0) ((or (= n 1) (= n 2)) 1)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;((even? n) (let* ((n2 (quotient n 2)) (n2-1 (- n2 1)))
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(* (fib n2) (+ (* 2 (fib n2-1)) (fib n2)))))
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(else (let* ((n2-1 (quotient n 2)) (n2 (+ n2-1 1)))
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(+ (square (fib n2-1)) (square (fib n2)))))))&lt;/code&gt;]]></description>
		<content:encoded><![CDATA[<p>After writing this exercise, I came across <a href="http://www.cs.utexas.edu/users/EWD/ewd06xx/EWD654.PDF" rel="nofollow">Dijkstra&#8217;s paper</a> describing a set of recurrence equations that calculate the <em>n</em>th fibonacci number.  His method is orthogonal to the matrix method described above, but more convenient for computation.  Note that Dijkstra starts the sequence differently; where we say <em>F</em><sub>0</sub>=0, Dijkstra says <em>F</em><sub>0</sub>=1.  Here&#8217;s the code:</p>
<p><code>(define (fib n)<br />
&nbsp;&nbsp;(define (square x) (* x x))<br />
&nbsp;&nbsp;(cond ((zero? n) 0) ((or (= n 1) (= n 2)) 1)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((even? n) (let* ((n2 (quotient n 2)) (n2-1 (- n2 1)))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(* (fib n2) (+ (* 2 (fib n2-1)) (fib n2)))))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(else (let* ((n2-1 (quotient n 2)) (n2 (+ n2-1 1)))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(+ (square (fib n2-1)) (square (fib n2)))))))</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rahul</title>
		<link>http://programmingpraxis.com/2010/07/30/fibonacci-numbers/#comment-1642</link>
		<dc:creator><![CDATA[rahul]]></dc:creator>
		<pubDate>Fri, 27 Aug 2010 15:51:56 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2711#comment-1642</guid>
		<description><![CDATA[please give also solution in c langauge pleaseeeeeeeee.]]></description>
		<content:encoded><![CDATA[<p>please give also solution in c langauge pleaseeeeeeeee.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christopher Oliver</title>
		<link>http://programmingpraxis.com/2010/07/30/fibonacci-numbers/#comment-1569</link>
		<dc:creator><![CDATA[Christopher Oliver]]></dc:creator>
		<pubDate>Tue, 10 Aug 2010 01:30:37 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2711#comment-1569</guid>
		<description><![CDATA[Err!!!  delete that backslash.  Praxis needs a way for responders to edit their mistakes!]]></description>
		<content:encoded><![CDATA[<p>Err!!!  delete that backslash.  Praxis needs a way for responders to edit their mistakes!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christopher Oliver</title>
		<link>http://programmingpraxis.com/2010/07/30/fibonacci-numbers/#comment-1568</link>
		<dc:creator><![CDATA[Christopher Oliver]]></dc:creator>
		<pubDate>Tue, 10 Aug 2010 01:28:58 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2711#comment-1568</guid>
		<description><![CDATA[And since the m[0,0] = m[1,1]+m[1,0]...

[sourcecode lang=&quot;css&quot;]
(define (fast-fib n)
  (define (successor a b)
    (list (+ a b) a))

  (define (square a b) \
    (list (* a (+ a b b)) (+ (* a a) (* b b))))

  (define (matrix-fib n)
    (cond ((= n 0) &#039;(1 0))
	  ((even? n) (apply successor (matrix-fib (- n 1))))
	  (else (apply square (matrix-fib (/ (- n 1) 2))))))

  (if (&lt; n 1) 0 (car (matrix-fib (- n 1)))))
[/sourcecode]

That will teach me to proofread.]]></description>
		<content:encoded><![CDATA[<p>And since the m[0,0] = m[1,1]+m[1,0]&#8230;</p>
<pre class="brush: css;">
(define (fast-fib n)
  (define (successor a b)
    (list (+ a b) a))

  (define (square a b) \
    (list (* a (+ a b b)) (+ (* a a) (* b b))))

  (define (matrix-fib n)
    (cond ((= n 0) '(1 0))
	  ((even? n) (apply successor (matrix-fib (- n 1))))
	  (else (apply square (matrix-fib (/ (- n 1) 2))))))

  (if (&lt; n 1) 0 (car (matrix-fib (- n 1)))))
</pre>
<p>That will teach me to proofread.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christopher Oliver</title>
		<link>http://programmingpraxis.com/2010/07/30/fibonacci-numbers/#comment-1560</link>
		<dc:creator><![CDATA[Christopher Oliver]]></dc:creator>
		<pubDate>Sat, 07 Aug 2010 16:27:56 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2711#comment-1560</guid>
		<description><![CDATA[An alternate scheme implementation with minor bit diddling.

[sourcecode lang=&quot;css&quot;]
;;; Computation of Fibonacci numbers with F(0) = 0 and F(1) = 1

(define (naive-fib n)
  (if (&lt; n 2) n (+ (naive-fib (- n 1)) (naive-fib (- n 2)))))


(define (linear-fib n)
  (let loop ((a 0) (b 1) (n n))
    (if (&lt; n 1) a (loop b (+ a b) (- n 1)))))


;; The following takes advantage of matrix symmetry and the constant
;; successor term to use cheaper steps for the square and multiply
;; operations.  Note that the matrix square takes Fib n to Fib 2n+1,
;; so the sense of even/odd in the power function is reversed from
;; the usual.
(define (fast-fib n)
  (define (successor a b c)
    (list (+ a b)
	  (+ b c)
	  b))

  (define (square a b c)
    (let ((b-squared (* b b)))
      (list (+ (* a a) b-squared)
	    (* b (+ a c))
	    (+ b-squared (* c c)))))

  (define (matrix-fib n)
    (cond ((= n 1) &#039;(2 1 1))
	  ((even? n) (apply successor (matrix-fib (- n 1))))
	  (else (apply square (matrix-fib (/ (- n 1) 2))))))

  (if (&lt; n 1) 0 (caddr (matrix-fib n))))
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>An alternate scheme implementation with minor bit diddling.</p>
<pre class="brush: css;">
;;; Computation of Fibonacci numbers with F(0) = 0 and F(1) = 1

(define (naive-fib n)
  (if (&lt; n 2) n (+ (naive-fib (- n 1)) (naive-fib (- n 2)))))


(define (linear-fib n)
  (let loop ((a 0) (b 1) (n n))
    (if (&lt; n 1) a (loop b (+ a b) (- n 1)))))


;; The following takes advantage of matrix symmetry and the constant
;; successor term to use cheaper steps for the square and multiply
;; operations.  Note that the matrix square takes Fib n to Fib 2n+1,
;; so the sense of even/odd in the power function is reversed from
;; the usual.
(define (fast-fib n)
  (define (successor a b c)
    (list (+ a b)
	  (+ b c)
	  b))

  (define (square a b c)
    (let ((b-squared (* b b)))
      (list (+ (* a a) b-squared)
	    (* b (+ a c))
	    (+ b-squared (* c c)))))

  (define (matrix-fib n)
    (cond ((= n 1) '(2 1 1))
	  ((even? n) (apply successor (matrix-fib (- n 1))))
	  (else (apply square (matrix-fib (/ (- n 1) 2))))))

  (if (&lt; n 1) 0 (caddr (matrix-fib n))))
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2010/07/30/fibonacci-numbers/#comment-1532</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Fri, 30 Jul 2010 23:37:19 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2711#comment-1532</guid>
		<description><![CDATA[Alternative (and, admittedly, slightly hacky, as evidenced by the warnings you get if you don&#039;t specify -fno-warn-missing-methods and -fno-warn-orphans) Haskell solution for the logarithmic version, based on the fact that the default power operator is already logarithmic:

[sourcecode lang=&quot;css&quot;]
{-# LANGUAGE FlexibleInstances #-}

import Data.List

instance Num a =&gt; Num [[a]] where
  a * b = [map (sum . zipWith (*) r) $ transpose b &#124; r &lt;- a]

fiblog :: Int -&gt; Integer
fiblog n = ([[1,1],[1,0]] ^ n) !! 1 !! 0
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Alternative (and, admittedly, slightly hacky, as evidenced by the warnings you get if you don&#8217;t specify -fno-warn-missing-methods and -fno-warn-orphans) Haskell solution for the logarithmic version, based on the fact that the default power operator is already logarithmic:</p>
<pre class="brush: css;">
{-# LANGUAGE FlexibleInstances #-}

import Data.List

instance Num a =&gt; Num [[a]] where
  a * b = [map (sum . zipWith (*) r) $ transpose b | r &lt;- a]

fiblog :: Int -&gt; Integer
fiblog n = ([[1,1],[1,0]] ^ n) !! 1 !! 0
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: F. Carr</title>
		<link>http://programmingpraxis.com/2010/07/30/fibonacci-numbers/#comment-1531</link>
		<dc:creator><![CDATA[F. Carr]]></dc:creator>
		<pubDate>Fri, 30 Jul 2010 20:37:33 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2711#comment-1531</guid>
		<description><![CDATA[Oops, left off the &lt;a href=&quot;http://codepad.org/LlZdF1FG&quot; rel=&quot;nofollow&quot;&gt;link to codepad.org&lt;/a&gt;.]]></description>
		<content:encoded><![CDATA[<p>Oops, left off the <a href="http://codepad.org/LlZdF1FG" rel="nofollow">link to codepad.org</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: F. Carr</title>
		<link>http://programmingpraxis.com/2010/07/30/fibonacci-numbers/#comment-1530</link>
		<dc:creator><![CDATA[F. Carr]]></dc:creator>
		<pubDate>Fri, 30 Jul 2010 20:36:40 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2711#comment-1530</guid>
		<description><![CDATA[Here is a logarithmic version --- but using base 3 instead of base 2.  &lt;a href=&quot;http://codepad.org/LlZdF1FG&quot; rel=&quot;nofollow&quot;&gt;]]></description>
		<content:encoded><![CDATA[<p>Here is a logarithmic version &#8212; but using base 3 instead of base 2.  <a href="http://codepad.org/LlZdF1FG" rel="nofollow"></a></p>
]]></content:encoded>
	</item>
</channel>
</rss>

