How to resolve the algorithm Factors of an integer step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Factors of an integer step by step in the Haskell programming language

Table of Contents

Problem Statement

Compute the   factors   of a positive integer. These factors are the positive integers by which the number being factored can be divided to yield a positive integer result. (Though the concepts function correctly for zero and negative integers, the set of factors of zero has countably infinite members, and the factors of negative integers can be obtained from the factors of related positive numbers without difficulty;   this task does not require handling of either of these cases). Note that every prime number has two factors:   1   and itself.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Factors of an integer step by step in the Haskell programming language

The provided Haskell code consists of several functions for finding the factors of an integer. Here's a detailed explanation of each function:

  1. factors: This function takes an integer n and returns a list of all the possible factors of n. It does this by first finding the prime power factors of n using the primePowerFactors function from the HFM.Primes library. Then, it uses mapM to generate a list of lists, where each sublist contains all the possible powers of each prime factor. Finally, it uses the product function to multiply the corresponding powers of prime factors to get all the possible factors.

Example:

> factors 42
[1,7,3,21,2,14,6,42]
  1. primePowerFactors: This function takes an integer n and returns a list of tuples [(p,m)], where p is a prime factor of n and m is the exponent of p in the prime factorization of n. It uses the group function from the Data.List library to group consecutive prime factors with the same exponent and then applies a mapping function (\x-> (head x, length x)) to each group.

Example:

> primePowerFactors 42
[(2,1),(3,1),(7,1)]
  1. integerFactors: This function takes an integer n and returns a list of all its integer factors. It uses a recursive approach to generate the factors. If n is less than or equal to 1, it returns an empty list. Otherwise, it splits n into two parts: a list of low factors lows and a quotient part n (reverse lows). lows contains the factors of n that are less than or equal to the square root of n. part n (reverse lows) contains the factors of n that are greater than the square root of n. The function then recursively calls itself on the quotient and combines the results.

Example:

> integerFactors 600
[1,2,3,4,5,6,8,10,12,15,20,24,25,30,40,50,60,75,100,120,150,200,300,600]
  1. factorsNaive: This function takes an integer n and returns a list of its factors. It uses a simple loop to generate the factors. It iterates over the range [1, n] and checks if each element i evenly divides n. If i divides n, it is added to the list of factors.

Example:

> factorsNaive 25
[1,5,25]
  1. factorsCo: This function takes an integer n and returns a sorted list of its factors. It uses list comprehensions and a combination of integer division and modulo operations to generate the factors. It iterates over the range [1, floor(sqrt(n))] and checks if n is divisible by each element i. If n is divisible by i, it adds i and n/i to the list of factors.

Example:

> factorsCo 120
[1,2,3,4,5,6,8,10,12,15,20,24,30,40,60,120]
  1. factorsO: This function is similar to factorsCo, but it optimizes the calculation by avoiding redundant divisions. It initializes two lists: ds and rs, where ds will store the factors less than or equal to the square root of n, and rs will store the remaining factors. It uses list comprehensions and a combination of integer division and modulo operations to generate the factors. It iterates over the range [1, floor(sqrt(n))] and checks if n is divisible by each element i. If n is divisible by i, it adds i to ds and n/i to rs. After the loop, it combines ds, rs, and the reverse of ds to get the complete list of factors.

Example:

> factorsO 12041111117
[1,7,41,287,541,3787,22181,77551,155267,542857,3179591,22257137,41955091,2936856
37,1720158731,12041111117]

Source code in the haskell programming language

import HFM.Primes (primePowerFactors)
import Control.Monad (mapM)
import Data.List (product)

-- primePowerFactors :: Integer -> [(Integer,Int)]

factors = map product .
          mapM (\(p,m)-> [p^i | i<-[0..m]]) . primePowerFactors


~> factors 42
[1,7,3,21,2,14,6,42]


import Data.List (group)
primePowerFactors = map (\x-> (head x, length x)) . group . factorize


integerFactors :: Int -> [Int]
integerFactors n
  | 1 > n = []
  | otherwise = lows <> (quot n <$> part n (reverse lows))
  where
    part n
      | n == square = tail
      | otherwise = id
    (square, lows) =
      (,) . (^ 2)
        <*> (filter ((0 ==) . rem n) . enumFromTo 1)
        $ floor (sqrt $ fromIntegral n)

main :: IO ()
main = print $ integerFactors 600


factorsNaive n =
  [ i
  | i <- [1 .. n] 
  , mod n i == 0 ]


~> factorsNaive 25
[1,5,25]


import Data.List (sort)

factorsCo n =
  sort
    [ i
    | i <- [1 .. floor (sqrt (fromIntegral n))] 
    , (d, 0) <- [divMod n i] 
    , i <-
       i :
       [ d
       | d > i ] ]


factorsO n =
  ds ++
  [ r
  | (d, 0) <- [divMod n r] 
  , r <-
     r :
     [ d
     | d > r ] ] ++
  reverse (map (n `div`) ds)
  where
    r = floor (sqrt (fromIntegral n))
    ds =
      [ i
      | i <- [1 .. r - 1] 
      , mod n i == 0 ]


*Main> :set +s
~> factorsO 120
[1,2,3,4,5,6,8,10,12,15,20,24,30,40,60,120]
(0.00 secs, 0 bytes)

~> factorsO 12041111117
[1,7,41,287,541,3787,22181,77551,155267,542857,3179591,22257137,41955091,2936856
37,1720158731,12041111117]
(0.09 secs, 50758224 bytes)


  

You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the Maple programming language
You may also check:How to resolve the algorithm 100 doors step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Ioke programming language
You may also check:How to resolve the algorithm UPC step by step in the Factor programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the SuperCollider programming language