Commit 677e760c authored by Han-Miru Kim's avatar Han-Miru Kim
Browse files

Continued Fractions evaluation

parent bc1a4e77
#+TITLE: Binary Quadratic Forms Tools
#+TITLE: Reduction Theory
#+AUTHOR: Han-Miru Kim
#+PROPERTY: header-args :tangle ./bqf.hs
* Overview
** Features
......@@ -70,7 +69,7 @@ so 329 of them take 1 step, 130 take 2, etc.
** Data
A binary quadratic form ~ax^2 + bxy + cy^2~ is represented as a tuple ~(a,b,c)~.
#+BEGIN_SRC haskell
#+BEGIN_SRC haskell :tangle ./bqf.hs
module BQF where
data BQF = BQF (Int,Int,Int)
......@@ -79,13 +78,12 @@ data BQF = BQF (Int,Int,Int)
instance Show BQF where
show (BQF (a,b,c))
= "(" ++ show a ++ "," ++ show b ++ "," ++ show c ++ ")"
#+END_SRC
** Basic Functions
A form is reduced if
~-a < b <= a < c~ or ~0 <= b<= a = c~
#+BEGIN_SRC haskell
#+BEGIN_SRC haskell :tangle ./bqf.hs
discriminant :: BQF -> Int
discriminant (BQF (a,b,c)) = b^2 - 4 * a * c
......@@ -102,7 +100,7 @@ A form is reduced if
** Reduction Theory
Reduction is done by first finding the ~n~ for which ~n > (b + sqrt D)/2a > n-1~
#+BEGIN_SRC haskell
#+BEGIN_SRC haskell :tangle ./bqf.hs
redcoeff :: BQF -> Int
redcoeff (BQF (a,b,c)) = ceiling $ (b' + d') / (2 * a')
where
......@@ -133,3 +131,26 @@ Reduction is done by first finding the ~n~ for which ~n > (b + sqrt D)/2a > n-1~
#+END_SRC
* Continued Fractions
** Data
I use the type ~Float~ instead of ~Int~ so I don't have to use ~fromIntegral~.
#+BEGIN_SRC haskell :tangle ./confrac.hs
module ConFrac where
data ConFrac = ConFrac [Float]
deriving Eq
instance Show ConFrac where
show (ConFrac xs)
= "[" ++ show xs ++ "]"
#+END_SRC
** Evaluation
#+BEGIN_SRC haskell :tangle ./confrac.hs
eval :: ConFrac -> Float
eval (ConFrac []) = 0
eval (ConFrac (x:[])) = x
eval (ConFrac (x:xs)) = x - (fromIntegral 1)/(eval (ConFrac xs))
#+END_SRC
module ConFrac where
data ConFrac = ConFrac [Float]
deriving Eq
instance Show ConFrac where
show (ConFrac xs)
= "[" ++ show xs ++ "]"
eval :: ConFrac -> Float
eval (ConFrac []) = 0
eval (ConFrac (x:[])) = x
eval (ConFrac (x:xs)) = x - (fromIntegral 1)/(eval (ConFrac xs))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment