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













1












$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








share









$endgroup$
















    1












    $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








    share









    $endgroup$














      1












      1








      1





      $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








      share









      $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





      share












      share










      share



      share










      asked 5 mins ago









      DairDair

      4,6371932




      4,6371932




















          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
          );



          );













          draft saved

          draft discarded


















          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















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          कुँवर स्रोत दिक्चालन सूची"कुँवर""राणा कुँवरके वंशावली"

          Why is a white electrical wire connected to 2 black wires?How to wire a light fixture with 3 white wires in box?How should I wire a ceiling fan when there's only three wires in the box?Two white, two black, two ground, and red wire in ceiling box connected to switchWhy is there a white wire connected to multiple black wires in my light box?How to wire a light with two white wires and one black wireReplace light switch connected to a power outlet with dimmer - two black wires to one black and redHow to wire a light with multiple black/white/green wires from the ceiling?Ceiling box has 2 black and white wires but fan/ light only has 1 of eachWhy neutral wire connected to load wire?Switch with 2 black, 2 white, 2 ground and 1 red wire connected to ceiling light and a receptacle?

          चैत्य भूमि चित्र दीर्घा सन्दर्भ बाहरी कडियाँ दिक्चालन सूची"Chaitya Bhoomi""Chaitya Bhoomi: Statue of Equality in India""Dadar Chaitya Bhoomi: Statue of Equality in India""Ambedkar memorial: Centre okays transfer of Indu Mill land"चैत्यभमि