Skip to content
Browse files

init

  • Loading branch information...
0 parents commit 2a3fa80d752d22583f4d0f65d3b0c9514cee7d93 @xyzzyz committed
Showing with 56 additions and 0 deletions.
  1. +38 −0 FibBinet.hs
  2. +18 −0 README.org
38 FibBinet.hs
@@ -0,0 +1,38 @@
+import System.Environment
+
+data QSqrt5 = QS5 Rational Rational
+ deriving (Show)
+
+instance Num QSqrt5 where
+ (QS5 p q) + (QS5 p' q') = (QS5 (p + p') (q + q'))
+ (QS5 p q) * (QS5 p' q') = (QS5 (p*p' + 5*q*q') (p * q' + p' * q))
+ x - y = x + negate y
+ negate (QS5 p q) = (QS5 (-p) (-q))
+ signum (QS5 p 0) = (QS5 (signum p) 0)
+ signum x@(QS5 p q) | p > 0 && q > 0 = 1
+ | p < 0 && q < 0 = -1
+ | q > 0 = QS5 (signum (-p*p/q*q + 5)) 0
+ | otherwise = negate (signum (negate x))
+ abs x = x * signum x
+ fromInteger n = QS5 (fromInteger n) 0
+
+instance Fractional QSqrt5 where
+ fromRational r = QS5 r 0
+ recip (QS5 p q) = invNorm * QS5 p (-q)
+ where invNorm :: QSqrt5
+ invNorm = fromRational . recip $ p*p - 5*q*q
+
+rationalPart :: QSqrt5 -> Rational
+rationalPart (QS5 a _) = a
+
+binetFib' :: Int -> QSqrt5
+binetFib' n = recip (QS5 0 1) * (phi^n - psi^n)
+ where phi = 1/2 * (1 + QS5 0 1)
+ psi = 1/2 * (1 - QS5 0 1)
+
+binetFib :: Int -> Integer
+binetFib = floor . rationalPart . binetFib'
+
+main = do
+ ns <- getArgs
+ mapM_ (putStrLn . show . binetFib) (map read ns)
18 README.org
@@ -0,0 +1,18 @@
+* FibBinet
+** what is it?
+ computes n-th fibonacci number using Binet's formula
+** is it any good?
+ sure, it works for abitrarily large numbes
+** usage
+ ./FibBinet n...
+
+ for instance
+
+ $ ./FibBinet 10 100
+ 55
+ 354224848179261915075
+
+** license
+ do with it whatever you want
+** author
+ Adam Michalik <adamm@mimuw.edu.pl>

0 comments on commit 2a3fa80

Please sign in to comment.
Something went wrong with that request. Please try again.