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

As a new Ubuntu desktop 18.04 LTS user, do I need to use ufw for a firewall or is iptables sufficient?

Do I need to be arrogant to get ahead?

A diagram about partial derivatives of f(x,y)

"of which" is correct here?

How to explain that I do not want to visit a country due to personal safety concern?

How to terminate ping <dest> &

What is the significance behind "40 days" that often appears in the Bible?

Describing a chess game in a novel

Book about superhumans hiding among normal humans

Violin - Can double stops be played when the strings are not next to each other?

Are ETF trackers fundamentally better than individual stocks?

Brexit - No Deal Rejection

Math equation in non italic font

What is the purpose or proof behind chain rule?

What is "focus distance lower/upper" and how is it different from depth of field?

If I can solve Sudoku, can I solve the Travelling Salesman Problem (TSP)? If so, how?

Does this sum go infinity?

Employee lack of ownership

What's the meaning of a knight fighting a snail in medieval book illustrations?

Why did it take so long to abandon sail after steamships were demonstrated?

New passport but visa is in old (lost) passport

How do you talk to someone whose loved one is dying?

Bach's Toccata and Fugue in D minor breaks the "no parallel octaves" rule?

Meme-controlled people



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

          瀋陽號驅逐艦 目录 接收與服役 配置反潛直升機 武進三型性能升級 歷史 除役 參考資料 外部連結 导航菜单Taiwan Air Power海疆老兵-陽字號驅逐艦沿革World Navies Today: Taiwan (Republic of China)DD-839 USS POWER

          波兰旗帜列表 目录 国旗 军旗 其他制服部门旗帜 特别国家机构船只 参考文献 外部链接 导航菜单Polskie flagi, chorągwie, bandery... [波兰旗帜、条幅、船旗等]原始内容Ustawa z dnia 31 stycznia 1980 r. o godle, barwach i hymnie Rzeczypospolitej Polskiej oraz o pieczęciach państwowychZarządzenie Ministra Obrony Narodowej z dnia 14 grudnia 2005 r. zmieniające zarządzenie w sprawie szczegółowych zasad używania znaków Sił Zbrojnych Rzeczypospolitej Polskiej oraz ustalenia innych znaków używanych w Siłach Zbrojnych Rzeczypospolitej PolskiejZarządzenie Ministra Obrony Narodowej z dnia 29 stycznia 1996 r. w sprawie szczegółowych zasad używania znaków Sił Zbrojnych Rzeczypospolitej Polskiej oraz ustalenia innych znaków używanych w Siłach Zbrojnych Rzeczypospolitej PolskiejUstawa z dnia 19 lutego 1993 r. o znakach Sił Zbrojnych Rzeczypospolitej PolskiejHistoria Marynarki Wojennej RP [波兰海军史]Rozporządzenie Ministra Spraw Wewnętrznych i Administracji z dnia 12 kwietnia 2002 r. w sprawie wzoru flagi oraz oznakowania jednostek pływających i statków powietrznych Straży GranicznejRozporządzenie Ministra Spraw Wewnętrznych i Administracji z dnia 18 kwietnia 2005 r. w sprawie wzoru flagi oraz oznakowania jednostek pływających i statków powietrznych PolicjiRozporządzenie Ministra Infrastruktury z dnia 21 października 2005 r. w sprawie wzorów flag dla statków morskich na oznaczenie pełnionej specjalnej służby państwowej oraz okoliczności i warunków ich podnoszenia波兰旗帜波兰

          Indenting and Dedenting ASP code with Python