Sum of divisors in HaskellHaskell grep simplificationProject Euler Problem 18 (Haskell) - Maximum path sum IUpdate Map in HaskellHaskell monads: sum of primesSum Arithmetic Progression in HaskellKey phone in HaskellAngle type in HaskellLucky Triples in HaskellHaskell permutations matching criteriaArray Search in Haskell
What is the purpose or proof behind chain rule?
How to terminate ping <dest> &
Are ETF trackers fundamentally better than individual stocks?
Simplify an interface for flexibly applying rules to periods of time
Most cost effective thermostat setting: consistent temperature vs. lowest temperature possible
Is it insecure to send a password in a `curl` command?
If I can solve Sudoku, can I solve the Travelling Salesman Problem (TSP)? If so, how?
Why is the President allowed to veto a cancellation of emergency powers?
ERC721: How to get the owned tokens of an address
Welcoming 2019 Pi day: How to draw the letter π?
World War I as a war of liberals against authoritarians?
Is it good practice to use Linear Least-Squares with SMA?
What's the meaning of a knight fighting a snail in medieval book illustrations?
Recruiter wants very extensive technical details about all of my previous work
How do you talk to someone whose loved one is dying?
Is "upgrade" the right word to use in this context?
Why does a Star of David appear at a rally with Francisco Franco?
Why Choose Less Effective Armour Types?
Brexit - No Deal Rejection
How could a scammer know the apps on my phone / iTunes account?
New passport but visa is in old (lost) passport
Bacteria contamination inside a thermos bottle
Tikz picture of two mathematical functions
I am confused as to how the inverse of a certain function is found.
Sum of divisors in Haskell
Haskell grep simplificationProject Euler Problem 18 (Haskell) - Maximum path sum IUpdate Map in HaskellHaskell monads: sum of primesSum Arithmetic Progression in HaskellKey phone in HaskellAngle type in HaskellLucky Triples in HaskellHaskell permutations matching criteriaArray Search in Haskell
$begingroup$
I decided to write a function divisorSum
that sums the divisors of number. For instance 1, 2, 3 and 6 divide 6 evenly so:
$$ sigma(6) = 1 + 2 + 3 + 6= 12 $$
I decided to use Euler's recurrence relation to calculate the sum of 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_iin mathbb Z_0 (-1)^i+1left( sigma(n - tfrac3i^2-i2) + delta(n,tfrac3i^2-i2)n right)$$
(See here for the details). As such, I decided to export some other useful functions like nthPentagonal
which returns the nth (generalized) pentagonal number. I created a new project with stack new
and modified these two files:
src/Lib.hs
module Lib
( nthPentagonal,
pentagonals,
divisorSum,
) where
-- | Creates a [generalized pentagonal integer]
-- | (https://en.wikipedia.org/wiki/Pentagonal_number_theorem) integer.
nthPentagonal :: Integer -> Integer
nthPentagonal n = n * (3 * n - 1) `div` 2
-- | Creates a lazy list of all the pentagonal numbers.
pentagonals :: [Integer]
pentagonals = map nthPentagonal integerStream
-- | Provides a stream for representing a bijection from naturals to integers
-- | i.e. [1, -1, 2, -2, ... ].
integerStream :: [Integer]
integerStream = map integerOrdering [1 .. ]
where
integerOrdering :: Integer -> Integer
integerOrdering n
| n `rem` 2 == 0 = (n `div` 2) * (-1)
| otherwise = (n `div` 2) + 1
-- | Using Euler's formula for the divisor function, we see that each summand
-- | alternates between two positive and two negative. This provides a stream
-- | of 1 1 -1 -1 1 1 ... to utilze in assiting this property.
additiveStream :: [Integer]
additiveStream = map summandSign [0 .. ]
where
summandSign :: Integer -> Integer
summandSign n
| n `rem` 4 >= 2 = -1
| otherwise = 1
-- | Kronkecker delta, return 0 if the integers are not the same, otherwise,
-- | return the value of the integer.
delta :: Integer -> Integer -> Integer
delta n i
| n == i = n
| otherwise = 0
-- | Calculate the sum of the divisors.
-- | Utilizes Euler's recurrence formula:
-- | $sigma(n) = sigma(n - 1) + sigma(n - 2) - sigma(n - 5) ldots $
-- | See [here](https://math.stackexchange.com/a/22744/15140) for more informa-
-- | tion.
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 <- pentagonals]
divisorStream :: Integer -> [Integer]
divisorStream n = zipWith (*)
(map divisorSum (pentDual n))
additiveStream
markPentagonal :: Integer -> [Integer]
markPentagonal n = zipWith (*)
(zipWith (delta)
pentagonals
(repeat n))
additiveStream
app/Main.hs
(mostly just to "test" it.)
module Main where
import Lib
main :: IO ()
main = putStrLn $ show $ divisorSum 8
haskell
$endgroup$
add a comment |
$begingroup$
I decided to write a function divisorSum
that sums the divisors of number. For instance 1, 2, 3 and 6 divide 6 evenly so:
$$ sigma(6) = 1 + 2 + 3 + 6= 12 $$
I decided to use Euler's recurrence relation to calculate the sum of 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_iin mathbb Z_0 (-1)^i+1left( sigma(n - tfrac3i^2-i2) + delta(n,tfrac3i^2-i2)n right)$$
(See here for the details). As such, I decided to export some other useful functions like nthPentagonal
which returns the nth (generalized) pentagonal number. I created a new project with stack new
and modified these two files:
src/Lib.hs
module Lib
( nthPentagonal,
pentagonals,
divisorSum,
) where
-- | Creates a [generalized pentagonal integer]
-- | (https://en.wikipedia.org/wiki/Pentagonal_number_theorem) integer.
nthPentagonal :: Integer -> Integer
nthPentagonal n = n * (3 * n - 1) `div` 2
-- | Creates a lazy list of all the pentagonal numbers.
pentagonals :: [Integer]
pentagonals = map nthPentagonal integerStream
-- | Provides a stream for representing a bijection from naturals to integers
-- | i.e. [1, -1, 2, -2, ... ].
integerStream :: [Integer]
integerStream = map integerOrdering [1 .. ]
where
integerOrdering :: Integer -> Integer
integerOrdering n
| n `rem` 2 == 0 = (n `div` 2) * (-1)
| otherwise = (n `div` 2) + 1
-- | Using Euler's formula for the divisor function, we see that each summand
-- | alternates between two positive and two negative. This provides a stream
-- | of 1 1 -1 -1 1 1 ... to utilze in assiting this property.
additiveStream :: [Integer]
additiveStream = map summandSign [0 .. ]
where
summandSign :: Integer -> Integer
summandSign n
| n `rem` 4 >= 2 = -1
| otherwise = 1
-- | Kronkecker delta, return 0 if the integers are not the same, otherwise,
-- | return the value of the integer.
delta :: Integer -> Integer -> Integer
delta n i
| n == i = n
| otherwise = 0
-- | Calculate the sum of the divisors.
-- | Utilizes Euler's recurrence formula:
-- | $sigma(n) = sigma(n - 1) + sigma(n - 2) - sigma(n - 5) ldots $
-- | See [here](https://math.stackexchange.com/a/22744/15140) for more informa-
-- | tion.
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 <- pentagonals]
divisorStream :: Integer -> [Integer]
divisorStream n = zipWith (*)
(map divisorSum (pentDual n))
additiveStream
markPentagonal :: Integer -> [Integer]
markPentagonal n = zipWith (*)
(zipWith (delta)
pentagonals
(repeat n))
additiveStream
app/Main.hs
(mostly just to "test" it.)
module Main where
import Lib
main :: IO ()
main = putStrLn $ show $ divisorSum 8
haskell
$endgroup$
add a comment |
$begingroup$
I decided to write a function divisorSum
that sums the divisors of number. For instance 1, 2, 3 and 6 divide 6 evenly so:
$$ sigma(6) = 1 + 2 + 3 + 6= 12 $$
I decided to use Euler's recurrence relation to calculate the sum of 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_iin mathbb Z_0 (-1)^i+1left( sigma(n - tfrac3i^2-i2) + delta(n,tfrac3i^2-i2)n right)$$
(See here for the details). As such, I decided to export some other useful functions like nthPentagonal
which returns the nth (generalized) pentagonal number. I created a new project with stack new
and modified these two files:
src/Lib.hs
module Lib
( nthPentagonal,
pentagonals,
divisorSum,
) where
-- | Creates a [generalized pentagonal integer]
-- | (https://en.wikipedia.org/wiki/Pentagonal_number_theorem) integer.
nthPentagonal :: Integer -> Integer
nthPentagonal n = n * (3 * n - 1) `div` 2
-- | Creates a lazy list of all the pentagonal numbers.
pentagonals :: [Integer]
pentagonals = map nthPentagonal integerStream
-- | Provides a stream for representing a bijection from naturals to integers
-- | i.e. [1, -1, 2, -2, ... ].
integerStream :: [Integer]
integerStream = map integerOrdering [1 .. ]
where
integerOrdering :: Integer -> Integer
integerOrdering n
| n `rem` 2 == 0 = (n `div` 2) * (-1)
| otherwise = (n `div` 2) + 1
-- | Using Euler's formula for the divisor function, we see that each summand
-- | alternates between two positive and two negative. This provides a stream
-- | of 1 1 -1 -1 1 1 ... to utilze in assiting this property.
additiveStream :: [Integer]
additiveStream = map summandSign [0 .. ]
where
summandSign :: Integer -> Integer
summandSign n
| n `rem` 4 >= 2 = -1
| otherwise = 1
-- | Kronkecker delta, return 0 if the integers are not the same, otherwise,
-- | return the value of the integer.
delta :: Integer -> Integer -> Integer
delta n i
| n == i = n
| otherwise = 0
-- | Calculate the sum of the divisors.
-- | Utilizes Euler's recurrence formula:
-- | $sigma(n) = sigma(n - 1) + sigma(n - 2) - sigma(n - 5) ldots $
-- | See [here](https://math.stackexchange.com/a/22744/15140) for more informa-
-- | tion.
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 <- pentagonals]
divisorStream :: Integer -> [Integer]
divisorStream n = zipWith (*)
(map divisorSum (pentDual n))
additiveStream
markPentagonal :: Integer -> [Integer]
markPentagonal n = zipWith (*)
(zipWith (delta)
pentagonals
(repeat n))
additiveStream
app/Main.hs
(mostly just to "test" it.)
module Main where
import Lib
main :: IO ()
main = putStrLn $ show $ divisorSum 8
haskell
$endgroup$
I decided to write a function divisorSum
that sums the divisors of number. For instance 1, 2, 3 and 6 divide 6 evenly so:
$$ sigma(6) = 1 + 2 + 3 + 6= 12 $$
I decided to use Euler's recurrence relation to calculate the sum of 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_iin mathbb Z_0 (-1)^i+1left( sigma(n - tfrac3i^2-i2) + delta(n,tfrac3i^2-i2)n right)$$
(See here for the details). As such, I decided to export some other useful functions like nthPentagonal
which returns the nth (generalized) pentagonal number. I created a new project with stack new
and modified these two files:
src/Lib.hs
module Lib
( nthPentagonal,
pentagonals,
divisorSum,
) where
-- | Creates a [generalized pentagonal integer]
-- | (https://en.wikipedia.org/wiki/Pentagonal_number_theorem) integer.
nthPentagonal :: Integer -> Integer
nthPentagonal n = n * (3 * n - 1) `div` 2
-- | Creates a lazy list of all the pentagonal numbers.
pentagonals :: [Integer]
pentagonals = map nthPentagonal integerStream
-- | Provides a stream for representing a bijection from naturals to integers
-- | i.e. [1, -1, 2, -2, ... ].
integerStream :: [Integer]
integerStream = map integerOrdering [1 .. ]
where
integerOrdering :: Integer -> Integer
integerOrdering n
| n `rem` 2 == 0 = (n `div` 2) * (-1)
| otherwise = (n `div` 2) + 1
-- | Using Euler's formula for the divisor function, we see that each summand
-- | alternates between two positive and two negative. This provides a stream
-- | of 1 1 -1 -1 1 1 ... to utilze in assiting this property.
additiveStream :: [Integer]
additiveStream = map summandSign [0 .. ]
where
summandSign :: Integer -> Integer
summandSign n
| n `rem` 4 >= 2 = -1
| otherwise = 1
-- | Kronkecker delta, return 0 if the integers are not the same, otherwise,
-- | return the value of the integer.
delta :: Integer -> Integer -> Integer
delta n i
| n == i = n
| otherwise = 0
-- | Calculate the sum of the divisors.
-- | Utilizes Euler's recurrence formula:
-- | $sigma(n) = sigma(n - 1) + sigma(n - 2) - sigma(n - 5) ldots $
-- | See [here](https://math.stackexchange.com/a/22744/15140) for more informa-
-- | tion.
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 <- pentagonals]
divisorStream :: Integer -> [Integer]
divisorStream n = zipWith (*)
(map divisorSum (pentDual n))
additiveStream
markPentagonal :: Integer -> [Integer]
markPentagonal n = zipWith (*)
(zipWith (delta)
pentagonals
(repeat n))
additiveStream
app/Main.hs
(mostly just to "test" it.)
module Main where
import Lib
main :: IO ()
main = putStrLn $ show $ divisorSum 8
haskell
haskell
asked 5 mins ago
DairDair
4,6371932
4,6371932
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215589%2fsum-of-divisors-in-haskell%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215589%2fsum-of-divisors-in-haskell%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown