Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-18 Thread ajb
G'day all. Quoting Mathew Mills <[EMAIL PROTECTED]>: > I guess I don't get any points for an approximate solution, ay? If only there was an iterative algorithm. Then you could use your method to get a great initial estimate... Cheers, Andrew Bromage

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-16 Thread Doug Quale
Doug Quale <[EMAIL PROTECTED]> writes: > Mathew Mills <[EMAIL PROTECTED]> writes: > > > Is there anything that can be done (easily) to reduce the rounding errors? > > The hint that I gave before is one easy way. > > > fib :: Integer -> Integer > > fib x = let phi = ( 1 + sqrt 5 ) / 2 > >

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-16 Thread Jon Fairbairn
On 2006-06-15 at 17:33BST "Vladimir Portnykh" wrote: > Fibonacci numbers implementations in Haskell one of the classical examples. > An example I found is the following: > > fibs :: [Int] > fibs = 0 : 1 : [ a + b | (a, b) <- zip fibs (tail fibs)] > > Can we do better? Well, you've had various v

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-16 Thread Doug Quale
Mathew Mills <[EMAIL PROTECTED]> writes: > Is there anything that can be done (easily) to reduce the rounding errors? The hint that I gave before is one easy way. > fib :: Integer -> Integer > fib x = let phi = ( 1 + sqrt 5 ) / 2 > in truncate( ( 1 / sqrt 5 ) * ( phi ^ x + 0.5) ) You r

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-16 Thread Chris Kuklewicz
Chris Kuklewicz wrote: Mathew Mills wrote: I guess I don't get any points for an approximate solution, ay? Is there anything that can be done (easily) to reduce the rounding errors? http://www.google.com/search?q=haskell+exact+real+arithmetic Using Era.hs (with the patch at http://www.

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-16 Thread Chris Kuklewicz
Mathew Mills wrote: I guess I don't get any points for an approximate solution, ay? Is there anything that can be done (easily) to reduce the rounding errors? http://www.google.com/search?q=haskell+exact+real+arithmetic ___ Haskell-Cafe mailing lis

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-16 Thread Mathew Mills
I guess I don't get any points for an approximate solution, ay? Is there anything that can be done (easily) to reduce the rounding errors? On 6/15/06 11:23 PM, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > G'day all. > > Quoting Mathew Mills <[EMAIL PROTECTED]>: > >> How about the closed f

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-16 Thread Ronny Wichers Schreur
Spencer Janssen writes (in the Haskell Cafe): Here's some code I wrote a while back for computing the nth Fibonacci number. It has O(log n) time complexity [..] The nth Fibonacci number has O(n) digits. Cheers, Ronny Wichers Schreur ___ Haskell-C

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-15 Thread ajb
G'day all. Quoting Mathew Mills <[EMAIL PROTECTED]>: > How about the closed form ;) > > > -- fib x returns the x'th number in the fib sequence > > > fib :: Integer -> Integer > > > fib x = let phi = ( 1 + sqrt 5 ) / 2 > > > in truncate( ( 1 / sqrt 5 ) * ( phi ^ x - phi' ^ x ) ) > > > Seem

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-15 Thread Doug Quale
Mathew Mills <[EMAIL PROTECTED]> writes: > -- fib x returns the x'th number in the fib sequence > fib :: Integer -> Integer > fib x = let phi = ( 1 + sqrt 5 ) / 2 > phi' = ( 1 - sqrt 5 ) / 2 > in truncate( ( 1 / sqrt 5 ) * ( phi ^ x - phi' ^ x ) ) > > -- Seems pretty quick to m

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-15 Thread Mathew Mills
How about the closed form ;) > -- fib x returns the x'th number in the fib sequence > fib :: Integer -> Integer > fib x = let phi = ( 1 + sqrt 5 ) / 2 > in truncate( ( 1 / sqrt 5 ) * ( phi ^ x - phi' ^ x ) ) Seems pretty quick to me, even with sqrt and arbitrarily large numbers. On

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-15 Thread ajb
G'day all. Quoting Vladimir Portnykh <[EMAIL PROTECTED]>: > I wrote my own Fibonacci numbers generator: > > fib :: Int -> [Int] > fib 0 = [0,0] > fib 1 = [1,0] > fib n = [sum prevFib, head prevFib] where a = fib (n - 1) > > To get the k-th number you do the following: > > result = head (fib k) [

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-15 Thread Henning Thielemann
On Thu, 15 Jun 2006, Vladimir Portnykh wrote: Fibonacci numbers implementations in Haskell one of the classical examples. An example I found is the following: fibs :: [Int] fibs = 0 : 1 : [ a + b | (a, b) <- zip fibs (tail fibs)] To get the k-th number you do the following: Result = fibs !!

Re: [Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-15 Thread Spencer Janssen
Here's some code I wrote a while back for computing the nth Fibonacci number. It has O(log n) time complexity rather than O(n). It isn't the most elegant example, but it should be one of the fastest approaches. import Data.Bits (shiftR, xor, (.|.), (.&.)) import Data.Word (Word32) fibo :: W

[Haskell-cafe] Fibonacci numbers generator in Haskell

2006-06-15 Thread Vladimir Portnykh
Fibonacci numbers implementations in Haskell one of the classical examples. An example I found is the following: fibs :: [Int] fibs = 0 : 1 : [ a + b | (a, b) <- zip fibs (tail fibs)] To get the k-th number you do the following: Result = fibs !! k It is elegant but creates a list of all Fibona