Sum of divisors 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+1}left( sigma(n - tfrac{3i^2-i}{2}) + delta(n,tfrac{3i^2-i}{2})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+1}left( sigma(n - tfrac{3i^2-i}{2}) + delta(n,tfrac{3i^2-i}{2})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+1}left( sigma(n - tfrac{3i^2-i}{2}) + delta(n,tfrac{3i^2-i}{2})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+1}left( sigma(n - tfrac{3i^2-i}{2}) + delta(n,tfrac{3i^2-i}{2})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 8 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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin