Factors of an integer: Difference between revisions

→‎{{Header|Haskell}}: Used an arrow operator to simplify an expression
(→‎{{Header|Haskell}}: Used an arrow operator to simplify an expression)
Line 1,800:
The above function can also be found in the package [http://hackage.haskell.org/package/arithmoi <code>arithmoi</code>], as <code>Math.NumberTheory.Primes.factorise :: Integer -> [(Integer, Int)]</code>, [http://hackage.haskell.org/package/arithmoi-0.4.2.0/docs/Math-NumberTheory-Primes-Factorisation.html which performs] "factorisation of Integers by the elliptic curve algorithm after Montgomery" and "is best suited for numbers of up to 50-60 digits".
 
Or, making do without further imports beyond the standard Prelude, and deriving cofactors from factors up to the square root.:
 
<lang Haskell>integerFactorsimport Control.Arrow ((&&&))
 
:: Integral a
integerFactors :: => aInt -> [aInt]
integerFactors n
| n < 1 = []
| otherwise =
lows ++
(if intRoot * intRootsquared == n
then tail
else id)
(reverse (quot n <$> lows))
where
(squared, lows) =
intRoot = floor (sqrt $ fromIntegral n)
lows = (^ 2) &&& (filter ((0 == 0) . rem n) [1 .. intRoot]enumFromTo 1) $
intRoot = floor (sqrt $ fromIntegral n)
 
main :: IO ()
9,655

edits