Copyright | (c) 2016 Andrew Lelechenko |
---|---|
License | MIT |
Maintainer | Andrew Lelechenko <andrew.lelechenko@gmail.com> |
Stability | Provisional |
Portability | Non-portable (GHC extensions) |
Safe Haskell | None |
Language | Haskell2010 |
This module provides an interface for defining and manipulating arithmetic functions. It also defines several most widespreaded arithmetic functions.
- data ArithmeticFunction n a where
- ArithmeticFunction :: Monoid m => (Prime n -> Word -> m) -> (m -> a) -> ArithmeticFunction n a
- runFunction :: UniqueFactorisation n => ArithmeticFunction n a -> n -> a
- multiplicative :: Num a => (Prime n -> Word -> a) -> ArithmeticFunction n a
- divisors :: (UniqueFactorisation n, Num n, Ord n) => n -> Set n
- divisorsA :: forall n. (UniqueFactorisation n, Num n, Ord n) => ArithmeticFunction n (Set n)
- divisorsSmall :: (UniqueFactorisation n, Prime n ~ Prime Int) => n -> IntSet
- divisorsSmallA :: forall n. Prime n ~ Prime Int => ArithmeticFunction n IntSet
- tau :: (UniqueFactorisation n, Num a) => n -> a
- tauA :: Num a => ArithmeticFunction n a
- sigma :: (UniqueFactorisation n, Integral n) => Word -> n -> n
- sigmaA :: forall n. (UniqueFactorisation n, Integral n) => Word -> ArithmeticFunction n n
- totient :: (UniqueFactorisation n, Integral n) => n -> n
- totientA :: forall n. (UniqueFactorisation n, Integral n) => ArithmeticFunction n n
- jordan :: (UniqueFactorisation n, Integral n) => Word -> n -> n
- jordanA :: forall n. (UniqueFactorisation n, Integral n) => Word -> ArithmeticFunction n n
- moebius :: (UniqueFactorisation n, Num a) => n -> a
- moebiusA :: Num a => ArithmeticFunction n a
- liouville :: (UniqueFactorisation n, Num a) => n -> a
- liouvilleA :: Num a => ArithmeticFunction n a
- additive :: Num a => (Prime n -> Word -> a) -> ArithmeticFunction n a
- smallOmega :: (UniqueFactorisation n, Num a) => n -> a
- smallOmegaA :: Num a => ArithmeticFunction n a
- bigOmega :: UniqueFactorisation n => n -> Word
- bigOmegaA :: ArithmeticFunction n Word
- carmichael :: (UniqueFactorisation n, Integral n) => n -> n
- carmichaelA :: forall n. (UniqueFactorisation n, Integral n) => ArithmeticFunction n n
- expMangoldt :: (UniqueFactorisation n, Num n) => n -> n
- expMangoldtA :: forall n. (UniqueFactorisation n, Num n) => ArithmeticFunction n n
Documentation
data ArithmeticFunction n a where Source #
A typical arithmetic function operates on the canonical factorisation of a number into prime's powers and consists of two rules. The first one determines the values of the function on the powers of primes. The second one determines how to combine these values into final result.
In the following definition the first argument is the function on prime's
powers, the monoid instance determines a rule of combination (typically
Product
or Sum
), and the second argument is convenient for unwrapping
(typically, getProduct
or getSum
).
ArithmeticFunction :: Monoid m => (Prime n -> Word -> m) -> (m -> a) -> ArithmeticFunction n a |
Functor (ArithmeticFunction n) Source # | |
Applicative (ArithmeticFunction n) Source # | |
Floating a => Floating (ArithmeticFunction n a) Source # | |
Fractional a => Fractional (ArithmeticFunction n a) Source # | |
Num a => Num (ArithmeticFunction n a) Source # | Factorisation is expensive, so it is better to avoid doing it twice. Write 'runFunction (f + g) n' instead of 'runFunction f n + runFunction g n'. |
Semigroup a => Semigroup (ArithmeticFunction n a) Source # | |
Monoid a => Monoid (ArithmeticFunction n a) Source # | |
runFunction :: UniqueFactorisation n => ArithmeticFunction n a -> n -> a Source #
Convert to function. The value on 0 is undefined.
Multiplicative functions
multiplicative :: Num a => (Prime n -> Word -> a) -> ArithmeticFunction n a Source #
Create a multiplicative function from the function on prime's powers. See examples below.
divisorsA :: forall n. (UniqueFactorisation n, Num n, Ord n) => ArithmeticFunction n (Set n) Source #
The set of all (positive) divisors of an argument.
divisorsSmall :: (UniqueFactorisation n, Prime n ~ Prime Int) => n -> IntSet Source #
divisorsSmallA :: forall n. Prime n ~ Prime Int => ArithmeticFunction n IntSet Source #
Same as divisors
, but with better performance on cost of type restriction.
tau :: (UniqueFactorisation n, Num a) => n -> a Source #
tauA :: Num a => ArithmeticFunction n a Source #
The number of (positive) divisors of an argument.
tauA = multiplicative (\_ k -> k + 1)
sigmaA :: forall n. (UniqueFactorisation n, Integral n) => Word -> ArithmeticFunction n n Source #
The sum of the k
-th powers of (positive) divisors of an argument.
sigmaA = multiplicative (\p k -> sum $ map (p ^) [0..k]) sigmaA 0 = tauA
totient :: (UniqueFactorisation n, Integral n) => n -> n Source #
totientA :: forall n. (UniqueFactorisation n, Integral n) => ArithmeticFunction n n Source #
Calculates the totient of a positive number n
, i.e.
the number of k
with 1 <= k <= n
and
,
in other words, the order of the group of units in gcd
n k == 1ℤ/(n)
.
jordanA :: forall n. (UniqueFactorisation n, Integral n) => Word -> ArithmeticFunction n n Source #
Calculates the k-th Jordan function of an argument.
jordanA 1 = totientA
moebius :: (UniqueFactorisation n, Num a) => n -> a Source #
moebiusA :: Num a => ArithmeticFunction n a Source #
Calculates the Moebius function of an argument.
liouville :: (UniqueFactorisation n, Num a) => n -> a Source #
liouvilleA :: Num a => ArithmeticFunction n a Source #
Calculates the Liouville function of an argument.
Additive functions
additive :: Num a => (Prime n -> Word -> a) -> ArithmeticFunction n a Source #
Create an additive function from the function on prime's powers. See examples below.
smallOmega :: (UniqueFactorisation n, Num a) => n -> a Source #
smallOmegaA :: Num a => ArithmeticFunction n a Source #
Number of distinct prime factors.
smallOmegaA = additive (\_ _ -> 1)
bigOmega :: UniqueFactorisation n => n -> Word Source #
bigOmegaA :: ArithmeticFunction n Word Source #
Number of prime factors, counted with multiplicity.
bigOmegaA = additive (\_ k -> k)
Misc
carmichael :: (UniqueFactorisation n, Integral n) => n -> n Source #
carmichaelA :: forall n. (UniqueFactorisation n, Integral n) => ArithmeticFunction n n Source #
Calculates the Carmichael function for a positive integer, that is,
the (smallest) exponent of the group of units in ℤ/(n)
.
expMangoldt :: (UniqueFactorisation n, Num n) => n -> n Source #
expMangoldtA :: forall n. (UniqueFactorisation n, Num n) => ArithmeticFunction n n Source #
The exponent of von Mangoldt function. Use log expMangoldtA
to recover von Mangoldt function itself.