Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you express the complexity of an algorithm, it is always as a function of the input size. It is only valid to assume that multiplication is an <code>O(1)</code> operation if the numbers that you are multiplying are of fixed size. For example, if you wanted to determine the complexity of an algorithm that computes matrix products, you might assume that the individual components of the matrices were of fixed size. Then it would be valid to assume that multiplication of two individual matrix components was <code>O(1)</code>, and you would compute the complexity according to the number of entries in each matrix.</p> <p>However, when you want to figure out the complexity of an algorithm to compute <code>N!</code> you have to assume that <code>N</code> can be arbitrarily large, so it is not valid to assume that multiplication is an <code>O(1)</code> operation. </p> <p>If you want to multiply an n-bit number with an m-bit number the naive algorithm (the kind you do by hand) takes time <code>O(mn)</code>, but there are faster algorithms.</p> <p>If you want to analyze the complexity of the easy algorithm for computing <code>N!</code></p> <pre><code> factorial(N) f=1 for i = 2 to N f=f*i return f </code></pre> <p>then at the k-th step in the for loop, you are multiplying <code>(k-1)!</code> by <code>k</code>. The number of bits used to represent <code>(k-1)!</code> is <code>O(k log k)</code> and the number of bits used to represent <code>k</code> is <code>O(log k)</code>. So the time required to multiply <code>(k-1)!</code> and <code>k</code> is <code>O(k (log k)^2)</code> (assuming you use the naive multiplication algorithm). Then the total amount of time taken by the algorithm is the sum of the time taken at each step:</p> <p><code>sum k = 1 to N [k (log k)^2] &lt;= (log N)^2 * (sum k = 1 to N [k]) =</code><br> <strong><code>O(N^2 (log N)^2)</code></strong></p> <p>You could improve this performance by using a faster multiplication algorithm, like Schönhage-Strassen which takes time <code>O(n*log(n)*log(log(n)))</code> for 2 n-bit numbers.</p> <p>The other way to improve performance is to use a better algorithm to compute <code>N!</code>. The fastest one that I know of first computes the prime factorization of <code>N!</code> and then multiplies all the prime factors. </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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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