# Sum of the dividers in Haskell

I decided to write a function `divisorSum` that sums up the divisors of the number. For example, divide 1, 2, 3 and 6 6 equally as follows:

$$sigma (6) = 1 + 2 + 3 + 6 = 12$$

I decided to use Euler's repeat relationship to calculate the sum of the divisors:

$$sigma (n) = sigma (n-1) + sigma (n-2) – sigma (n-5) – sigma (n-7) + sigma (n-12) + sigma (n-15) + ldots$$

i.e.

$$sigma (n) = sum_ {i in mathbb Z_0} (-1) ^ {i + 1} left ( sigma (n – tfrac {3i ^ 2-i} {2}) + delta (n, tfrac {3i ^ 2-i} {2}) n right)$$

(See here for the details). Therefore I decided to export some other useful features like `nthPentagonal` which returns the nth (generalized) pentagonal number. I have created a new project with `re-stack` and modified these two files:

``````Module Lib
(NthPentagonal,
pentagonal,
divisorSum,
) from where

- | Generates one [generalized pentagonal integer]
- | (https://en.wikipedia.org/wiki/Pentagonal_number_theorem) integer.
nthPentagonal :: Integer -> Integer
nthPentagonal n = n * (3 * n - 1) `div` 2

- | Generate a rotten list of all pentagonal numbers.
Pentagonal :: [Integer]
Pentagonals = map nthPentagonal IntegerStream

- | Provides a stream to represent an intersection of naturals to integers
- | i.e. [1, -1, 2, -2, ... ],
integerStream :: [Integer]
integerStream = map integerOrdering [1 .. ]
from where
integerOrdering :: Integer -> Integer
integerOrdering n
| n "rem" 2 == 0 = (n "div" 2) * (-1)
| otherwise = (n `div` 2) + 1

- | Using the Euler formula for the divisor function, we see this summand
- | switches between two positive and two negative. This provides a stream
- | of 1 1 -1 -1 1 1 1 ... ... to evaluate this property.
additiveStream = map summandSign [0 .. ]
from where
summandSign :: integer -> integer
summandSign n
| n "rem" 4> = 2 = -1
| otherwise = 1

- | Kronkecker delta, return 0 if the integers are not equal, otherwise
- | Returns the value of the integer.
Delta :: Integer -> Integer -> Integer
Delta n i
| n == i = n
| otherwise = 0

- | Calculate the sum of the dividers.
- | Uses Euler's Repeat Formula:
- | \$  sigma (n) =  sigma (n - 1) +  sigma (n - 2) -  sigma (n - 5)  ldots \$
- | See [here](https://math.stackexchange.com/a/22744/15140) for more information
- | voltage.
divisorSum :: Integer -> Integer
divisorSum n
| n <= 0 = 0
| otherwise = sum \$ takeWhile (/= 0)
(zipWith (+)
(divisorStream n)
(markPentagonal n))
where
pentDual :: Integer -> [Integer]

pentDual n =[N-x|x[N-x|x[n-x|x[n-x|x<- pentagonals]
divisorStream :: Integer -> [Integer]

divisorStream n = zipWith (*)
(map divisorSum (pentDual n))
markPentagonal :: Integer -> [Integer]
markPentagonal n = zipWith (*)
(zipWith (delta)
pentagonal
(to repeat))
``````Module home where