Skip to content

Commit

Permalink
added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
brunjlar committed Apr 19, 2015
1 parent b017c43 commit aa26208
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
14 changes: 13 additions & 1 deletion Math/NumberTheory/Moduli/SquareRoots.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
module Math.NumberTheory.Moduli.SquareRoots (sqrts) where
-- |
-- Module: Math.NumberTheory.Moduli.SquareRoots
-- Copyright: (c) 2015 by Dr. Lars Brünjes
-- Licence: MIT
-- Maintainer: Dr. Lars Brünjes <[email protected]>
-- Stability: Provisional
-- Portability: portable
--
-- This module provides a function to find all square roots of a number modulo another number.
module Math.NumberTheory.Moduli.SquareRoots (
sqrts ) where

import Data.List (sort)
import Math.NumberTheory.Primes.Factorisation (factorise)
Expand Down Expand Up @@ -31,6 +41,8 @@ sqrtsPP a (p, e)
where
m = p ^ e

-- |@sqrts a m@ finds all square roots of @a@ modulo @m@,
-- where @a@ is an arbitrary integer and @m@ is a positive integer.
sqrts :: Integer -> Integer -> [Integer]
sqrts a m
| a < 0 = error $ "a must not be negative, but a == " ++ show a ++ " < 0."
Expand Down
30 changes: 25 additions & 5 deletions Math/NumberTheory/Pell.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
module Math.NumberTheory.Pell ( solve, getReps, getMinimalReps, equivalent, mul, solvePlusOne ) where
-- |
-- Module: Math.NumberTheory.Pell
-- Copyright: (c) 2015 by Dr. Lars Brünjes
-- Licence: MIT
-- Maintainer: Dr. Lars Brünjes <[email protected]>
-- Stability: Provisional
-- Portability: portable
--
-- This module provides a function to solve generalized Pell Equations,
-- using the "LMM Algorithm" described by John P. Robertson in
-- <http://http://www.jpr2718.org/pell.pdf>.
-- A /generalized Pell Equation/ is a diophantine equation of the form
-- @x^2 - dy^2 = n@, where @d@ is a positive integer which is not a square
-- and where @n@ is a non-zero integer.
-- We are looking for solutions @(x,y)@, where @x@ and @y@ are non-negative integers.
module Math.NumberTheory.Pell (
Solution,
solve ) where

import Control.Arrow ((***))
import Data.List (sort, nub)
Expand Down Expand Up @@ -31,6 +48,9 @@ getRS d m z =
[] -> Nothing
(rs : _) -> Just rs

-- |Represents a solution to a generalized Pell Equation.
-- The first component is the value of x,
-- the second component that of y.
type Solution = (Integer, Integer)

solvePlusOne :: Integer -> Solution
Expand Down Expand Up @@ -78,6 +98,10 @@ getMinimalReps d n = ((r, s), map toMinimal reps) where
((r, s), reps) = getReps d n
toMinimal (x, y) = minimum $ map (abs *** abs) $ filter (\(x', y') -> x' * y' >= 0) [(x, y), mul d (r, s) (x, y), mul d (r, -s) (x, y)]

-- |@solve d n@ calculates all non-negative integer solutions of the generalized Pell Equation
-- x^2 - @d@y^2 = @n@,
-- where @d@ must be a positive integer which is not a square,
-- and @n@ must be a non-zero integer.
solve :: Integer -> Integer -> [Solution]
solve d n
| d <= 0 = error $ "D must be positive, but D == " ++ show d ++ "."
Expand All @@ -89,7 +113,3 @@ solve d n
go xys' = normalize xys' ++ go (step xys')
normalize = sort . nub
step = map (mul d (r, s))

equivalent :: Integer -> Integer -> (Integer, Integer) -> (Integer, Integer) -> Bool
equivalent d n (x, y) (r, s) = nDivides (x * r - d * y * s) && nDivides (x * s - y * r) where
nDivides z = (z `mod` n) == 0
21 changes: 15 additions & 6 deletions pell.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
name: pell
version: 0.1.0.0
synopsis: Package to solve the Generalized Pell Equation.
-- description:
description: Finds all solutions of the generalized Pell Equation.
homepage: https://github.com/brunjlar/pell
license: MIT
license-file: LICENSE
author: Dr. Lars Bruenjes
author: Lars Bruenjes
maintainer: [email protected]
-- copyright:
category: Math
copyright: (c) 2015 by Dr. Lars Brünjes
category: Math, Algorithms, Number Theory
build-type: Simple
extra-source-files: README.md
cabal-version: >=1.22.2.0
cabal-version: >=1.20.0

library
exposed-modules: Math.NumberTheory.Pell, Math.NumberTheory.Moduli.SquareRoots
Expand All @@ -41,6 +41,15 @@ Test-Suite test-pell
containers,
QuickCheck >= 2.8,
primes,
Cabal >= 1.22.2,
Cabal >= 1.20.0,
cabal-test-quickcheck
default-language: Haskell2010

source-repository head
type: git
location: https://github.com/brunjlar/pell

source-repository this
type: git
location: https://github.com/brunjlar/pell
tag: 0.1.0.0

0 comments on commit aa26208

Please sign in to comment.