How to resolve the algorithm Factors of an integer step by step in the Haskell programming language
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:
factors
: This function takes an integern
and returns a list of all the possible factors ofn
. It does this by first finding the prime power factors ofn
using theprimePowerFactors
function from theHFM.Primes
library. Then, it usesmapM
to generate a list of lists, where each sublist contains all the possible powers of each prime factor. Finally, it uses theproduct
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]
primePowerFactors
: This function takes an integern
and returns a list of tuples[(p,m)]
, wherep
is a prime factor ofn
andm
is the exponent ofp
in the prime factorization ofn
. It uses thegroup
function from theData.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)]
integerFactors
: This function takes an integern
and returns a list of all its integer factors. It uses a recursive approach to generate the factors. Ifn
is less than or equal to 1, it returns an empty list. Otherwise, it splitsn
into two parts: a list of low factorslows
and a quotientpart n (reverse lows)
.lows
contains the factors ofn
that are less than or equal to the square root ofn
.part n (reverse lows)
contains the factors ofn
that are greater than the square root ofn
. 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]
factorsNaive
: This function takes an integern
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 elementi
evenly dividesn
. Ifi
dividesn
, it is added to the list of factors.
Example:
> factorsNaive 25
[1,5,25]
factorsCo
: This function takes an integern
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 ifn
is divisible by each elementi
. Ifn
is divisible byi
, it addsi
andn/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]
factorsO
: This function is similar tofactorsCo
, but it optimizes the calculation by avoiding redundant divisions. It initializes two lists:ds
andrs
, whereds
will store the factors less than or equal to the square root ofn
, andrs
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 ifn
is divisible by each elementi
. Ifn
is divisible byi
, it addsi
tods
andn/i
tors
. After the loop, it combinesds
,rs
, and the reverse ofds
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