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

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

          Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar