Dan Weber (mirrorlynx) wrote,
Dan Weber
mirrorlynx

Fibonacci : Haskell edition

On my run through the haskell language, I implement my hello world like program for every language which is fibonacci.

Here is my haskell implementation:

diffsquares a b = 2*n*b + n^2
where n = (a-b)

fib :: Integer -> Integer
fib 0 = 0
fib 1 = 1
fib 2 = 1

fib n | (n `mod` 2) == 0 = diffsquares (fib ((n `div` 2) + 1)) (fib ((n `div` 2) - 1))
| otherwise = (fib (n `div` 2))^2 + (fib ((n `div` 2) + 1))^2



Diffsquares is a little differencing squares hack only squaring the distance between the two numbers. The fibonacci implementation is based on fib(n)^2 + fib(n+1)^2 = fib(2n+1)
Subscribe

  • PayPal Sucks

    PayPal is the most crooked online money sending place ever. I recently bidded on a Ti89 Titanium on ebay and got it for 129 including s/h. (Great…

  • DWeb's guide to cleaning shoes

    Today I was running out the door chasing the dog. The dog saw a person walking on the street and ran up to see her. I managed to get a hold on the…

  • Sarge Frozen ... Freezerburned?

    While sarge was frozen today, how much longer is it going to be till its freezerburned like Woody. Not even sure woody is freezer burned, its like…

  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 45 comments
Previous
← Ctrl ← Alt
Next
Ctrl → Alt →
Previous
← Ctrl ← Alt
Next
Ctrl → Alt →

  • PayPal Sucks

    PayPal is the most crooked online money sending place ever. I recently bidded on a Ti89 Titanium on ebay and got it for 129 including s/h. (Great…

  • DWeb's guide to cleaning shoes

    Today I was running out the door chasing the dog. The dog saw a person walking on the street and ran up to see her. I managed to get a hold on the…

  • Sarge Frozen ... Freezerburned?

    While sarge was frozen today, how much longer is it going to be till its freezerburned like Woody. Not even sure woody is freezer burned, its like…