Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing a factorisation method in Haskell
    primarykey
    data
    text
    <p>I am doing <a href="http://projecteuler.net/index.php?section=problems&amp;id=266" rel="nofollow noreferrer">question 266</a> at Project Euler and after a bit of searching, found <a href="http://www.gmathacks.com/gmat-math/number-of-factors-of-a-large-integer.html" rel="nofollow noreferrer">this method</a> of quickly finding the factors of a number. What you do is find all the permutations of the prime factors of a number: these are its factors.</p> <p>I already have a module to find the prime power factors of a number, eg:</p> <pre><code>Main&gt; primePowerFactors 196 [(2,2),(7,2)] </code></pre> <p>This is basically showing that: <code>2^2 * 7^2 == 196</code>. From here I want to find all the permutations of those powers, to give the factors of 196 thusly:</p> <ul> <li>(2^0)(7^0) = 1</li> <li>(2^1)(7^0) = 2</li> <li>(2^2)(7^0) = 4</li> <li>(2^0)(7^1) = 7</li> <li>(2^1)(7^1) = 14</li> <li>(2^2)(7^1) = 28</li> <li>(2^0)(7^2) = 49</li> <li>(2^1)(7^2) = 98</li> </ul> <p>I came up with the following:</p> <pre><code>factors n = [a|a&lt;-map facs (primePowerFactors n), y &lt;- [0..(snd $ last $ primePowerFactors n)]] where facs (x,y) = (x,y) rSq n = toInteger $ round $ sqrt (fromInteger n) psr n = last $ dropWhile (&lt;= rSq n) $ factors n p = foldl' (*) 1 $ takeWhile (&lt; 190) primes answer = (psr p) `mod` (10^16) </code></pre> <p>But my problem is that <code>factors</code> doesn't work properly. I want it to permute through all the possible values of the exponent for each prime factor and then find the product to give the factor. How can it be modified to return the just the factors of <em>n</em>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload