Open In App
Related Articles

Efficient program to print the number of factors of n numbers

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given an array of integers. We are required to write a program to print the number of factors of every element of the given array.
Examples: 
 

Input: 10 12 14 
Output: 4 6 4 
Explanation: There are 4 factors of 10 (1, 2, 
5, 10) and 6 of 12 and 4 of 14.

Input: 100 1000 10000
Output: 9 16 25 
Explanation: There are 9 factors of 100  and
16 of 1000 and 25 of 10000.


 

Recommended Practice


Simple Approach: A simple approach will be to run two nested loops. One for traversing the array and other for calculating all factors of elements of array. 
Time Complexity: O( n * n) 
Auxiliary Space: O( 1 )
Efficient Approach: We can optimize the above approach by optimizing the number of operations required to calculate the factors of a number. We can calculate the factors of a number n in sqrt(n) operations using this approach. 
Time Complexity: O( n * sqrt(n)) 
Auxiliary Space: O( 1 )
Best Approach: If you go through number theory, you will find an efficient way to find the number of factors. If we take a number, say in this case 30, then the prime factors of 30 will be 2, 3, 5 with count of each of these being 1 time, so total number of factors of 30 will be (1+1)*(1+1)*(1+1) = 8. 
Therefore, the general formula of total number of factors of a given number will be: 
 

Factors = (1+a1) * (1+a2) * (1+a3) * … (1+an)


where a1, a2, a3 …. an are count of distinct prime factors of n.
Let’s take another example to make things more clear. Let the number be 100 this time, 
So 100 will have 2, 2, 5, 5. So the count of distinct prime factors of 100 are 2, 2. Hence number of factors will be (2+1)*(2+1) = 9.
Now, the best way to find the prime factorization will be to store the sieve of prime factors initially. Create the sieve in a way that it stores the smallest primes factor that divides itself. We can modify the Sieve of Eratosthenes to do this. Then simply for every number find the count of prime factors and multiply it to find the number of total factors .
Below is the implementation of above approach. 
 

C++


                    

Java

<div id="highlighter_330607" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments">// JAVA Code For Efficient program to print</code></div><div class="line number2 index1 alt1"><code class="comments">// the number of factors of n numbers</code></div><div class="line number3 index2 alt2"><code class="keyword">import</code> <code class="plain">java.util.*;</code></div><div class="line number4 index3 alt1"> </div><div class="line number5 index4 alt2"><code class="keyword">class</code> <code class="plain">GFG {</code></div><div class="line number6 index5 alt1"> </div><div class="line number7 index6 alt2"><code class="undefined spaces">    </code><code class="keyword">static</code> <code class="keyword">int</code> <code class="plain">MAX = </code><code class="value">1000001</code><code class="plain">;</code></div><div class="line number8 index7 alt1"><code class="undefined spaces">    </code><code class="keyword">static</code> <code class="keyword">int</code> <code class="plain">factor[];</code></div><div class="line number9 index8 alt2"> </div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="comments">// function to generate all prime</code></div><div class="line number11 index10 alt2"><code class="undefined spaces">    </code><code class="comments">// factors of numbers from 1 to 10^6</code></div><div class="line number12 index11 alt1"><code class="undefined spaces">    </code><code class="keyword">static</code> <code class="keyword">void</code> <code class="plain">generatePrimeFactors()</code></div><div class="line number13 index12 alt2"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number14 index13 alt1"><code class="undefined spaces">        </code><code class="plain">factor[</code><code class="value">1</code><code class="plain">] = </code><code class="value">1</code><code class="plain">;</code></div><div class="line number15 index14 alt2"> </div><div class="line number16 index15 alt1"><code class="undefined spaces">        </code><code class="comments">// Initializes all the positions with</code></div><div class="line number17 index16 alt2"><code class="undefined spaces">        </code><code class="comments">// their value.</code></div><div class="line number18 index17 alt1"><code class="undefined spaces">        </code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">i = </code><code class="value">2</code><code class="plain">; i < MAX; i++)</code></div><div class="line number19 index18 alt2"><code class="undefined spaces">            </code><code class="plain">factor[i] = i;</code></div><div class="line number20 index19 alt1"> </div><div class="line number21 index20 alt2"><code class="undefined spaces">        </code><code class="comments">// Initializes all multiples of 2 with 2</code></div><div class="line number22 index21 alt1"><code class="undefined spaces">        </code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">i = </code><code class="value">4</code><code class="plain">; i < MAX; i += </code><code class="value">2</code><code class="plain">)</code></div><div class="line number23 index22 alt2"><code class="undefined spaces">            </code><code class="plain">factor[i] = </code><code class="value">2</code><code class="plain">;</code></div><div class="line number24 index23 alt1"> </div><div class="line number25 index24 alt2"><code class="undefined spaces">        </code><code class="comments">// A modified version of Sieve of</code></div><div class="line number26 index25 alt1"><code class="undefined spaces">        </code><code class="comments">// Eratosthenes to store the</code></div><div class="line number27 index26 alt2"><code class="undefined spaces">        </code><code class="comments">// smallest prime factor that</code></div><div class="line number28 index27 alt1"><code class="undefined spaces">        </code><code class="comments">// divides every number.</code></div><div class="line number29 index28 alt2"><code class="undefined spaces">        </code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">i = </code><code class="value">3</code><code class="plain">; i * i < MAX; i++) {</code></div><div class="line number30 index29 alt1"><code class="undefined spaces">            </code><code class="comments">// check if it has no prime factor.</code></div><div class="line number31 index30 alt2"><code class="undefined spaces">            </code><code class="keyword">if</code> <code class="plain">(factor[i] == i) {</code></div><div class="line number32 index31 alt1"><code class="undefined spaces">                </code><code class="comments">// Initializes of j starting from i*i</code></div><div class="line number33 index32 alt2"><code class="undefined spaces">                </code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">j = i * i; j < MAX; j += i) {</code></div><div class="line number34 index33 alt1"><code class="undefined spaces">                    </code><code class="comments">// if it has no prime factor</code></div><div class="line number35 index34 alt2"><code class="undefined spaces">                    </code><code class="comments">// before, then stores the</code></div><div class="line number36 index35 alt1"><code class="undefined spaces">                    </code><code class="comments">// smallest prime divisor</code></div><div class="line number37 index36 alt2"><code class="undefined spaces">                    </code><code class="keyword">if</code> <code class="plain">(factor[j] == j)</code></div><div class="line number38 index37 alt1"><code class="undefined spaces">                        </code><code class="plain">factor[j] = i;</code></div><div class="line number39 index38 alt2"><code class="undefined spaces">                </code><code class="plain">}</code></div><div class="line number40 index39 alt1"><code class="undefined spaces">            </code><code class="plain">}</code></div><div class="line number41 index40 alt2"><code class="undefined spaces">        </code><code class="plain">}</code></div><div class="line number42 index41 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number43 index42 alt2"> </div><div class="line number44 index43 alt1"><code class="undefined spaces">    </code><code class="comments">// function to calculate number of factors</code></div><div class="line number45 index44 alt2"><code class="undefined spaces">    </code><code class="keyword">static</code> <code class="keyword">int</code> <code class="plain">calculateNoOfFactors(</code><code class="keyword">int</code> <code class="plain">n)</code></div><div class="line number46 index45 alt1"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number47 index46 alt2"><code class="undefined spaces">        </code><code class="keyword">if</code> <code class="plain">(n == </code><code class="value">1</code><code class="plain">)</code></div><div class="line number48 index47 alt1"><code class="undefined spaces">            </code><code class="keyword">return</code> <code class="value">1</code><code class="plain">;</code></div><div class="line number49 index48 alt2"> </div><div class="line number50 index49 alt1"><code class="undefined spaces">        </code><code class="keyword">int</code> <code class="plain">ans = </code><code class="value">1</code><code class="plain">;</code></div><div class="line number51 index50 alt2"> </div><div class="line number52 index51 alt1"><code class="undefined spaces">        </code><code class="comments">// stores the smallest prime number</code></div><div class="line number53 index52 alt2"><code class="undefined spaces">        </code><code class="comments">// that divides n</code></div><div class="line number54 index53 alt1"><code class="undefined spaces">        </code><code class="keyword">int</code> <code class="plain">dup = factor[n];</code></div><div class="line number55 index54 alt2"> </div><div class="line number56 index55 alt1"><code class="undefined spaces">        </code><code class="comments">// stores the count of number of times</code></div><div class="line number57 index56 alt2"><code class="undefined spaces">        </code><code class="comments">// a prime number divides n.</code></div><div class="line number58 index57 alt1"><code class="undefined spaces">        </code><code class="keyword">int</code> <code class="plain">c = </code><code class="value">1</code><code class="plain">;</code></div><div class="line number59 index58 alt2"> </div><div class="line number60 index59 alt1"><code class="undefined spaces">        </code><code class="comments">// reduces to the next number after prime</code></div><div class="line number61 index60 alt2"><code class="undefined spaces">        </code><code class="comments">// factorization of n</code></div><div class="line number62 index61 alt1"><code class="undefined spaces">        </code><code class="keyword">int</code> <code class="plain">j = n / factor[n];</code></div><div class="line number63 index62 alt2"> </div><div class="line number64 index63 alt1"><code class="undefined spaces">        </code><code class="comments">// false when prime factorization is done</code></div><div class="line number65 index64 alt2"><code class="undefined spaces">        </code><code class="keyword">while</code> <code class="plain">(j != </code><code class="value">1</code><code class="plain">) {</code></div><div class="line number66 index65 alt1"><code class="undefined spaces">            </code><code class="comments">// if the same prime number is dividing n,</code></div><div class="line number67 index66 alt2"><code class="undefined spaces">            </code><code class="comments">// then we increase the count</code></div><div class="line number68 index67 alt1"><code class="undefined spaces">            </code><code class="keyword">if</code> <code class="plain">(factor[j] == dup)</code></div><div class="line number69 index68 alt2"><code class="undefined spaces">                </code><code class="plain">c += </code><code class="value">1</code><code class="plain">;</code></div><div class="line number70 index69 alt1"> </div><div class="line number71 index70 alt2"><code class="undefined spaces">            </code><code class="comments">/* if its a new prime factor that is </code></div><div class="line number72 index71 alt1"><code class="undefined spaces">               </code><code class="comments">factorizing n, then we again set c=1 </code></div><div class="line number73 index72 alt2"><code class="undefined spaces">               </code><code class="comments">and change dup to the new prime factor,</code></div><div class="line number74 index73 alt1"><code class="undefined spaces">               </code><code class="comments">and apply the formula explained </code></div><div class="line number75 index74 alt2"><code class="undefined spaces">               </code><code class="comments">above. */</code></div><div class="line number76 index75 alt1"><code class="undefined spaces">            </code><code class="keyword">else</code> <code class="plain">{</code></div><div class="line number77 index76 alt2"><code class="undefined spaces">                </code><code class="plain">dup = factor[j];</code></div><div class="line number78 index77 alt1"><code class="undefined spaces">                </code><code class="plain">ans = ans * (c + </code><code class="value">1</code><code class="plain">);</code></div><div class="line number79 index78 alt2"><code class="undefined spaces">                </code><code class="plain">c = </code><code class="value">1</code><code class="plain">;</code></div><div class="line number80 index79 alt1"><code class="undefined spaces">            </code><code class="plain">}</code></div><div class="line number81 index80 alt2"> </div><div class="line number82 index81 alt1"><code class="undefined spaces">            </code><code class="comments">// prime factorizes a number</code></div><div class="line number83 index82 alt2"><code class="undefined spaces">            </code><code class="plain">j = j / factor[j];</code></div><div class="line number84 index83 alt1"><code class="undefined spaces">        </code><code class="plain">}</code></div><div class="line number85 index84 alt2"> </div><div class="line number86 index85 alt1"><code class="undefined spaces">        </code><code class="comments">// for the last prime factor</code></div><div class="line number87 index86 alt2"><code class="undefined spaces">        </code><code class="plain">ans = ans * (c + </code><code class="value">1</code><code class="plain">);</code></div><div class="line number88 index87 alt1"> </div><div class="line number89 index88 alt2"><code class="undefined spaces">        </code><code class="keyword">return</code> <code class="plain">ans;</code></div><div class="line number90 index89 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number91 index90 alt2"> </div><div class="line number92 index91 alt1"><code class="undefined spaces">    </code><code class="comments">/* Driver program to test above function */</code></div><div class="line number93 index92 alt2"><code class="undefined spaces">    </code><code class="keyword">public</code> <code class="keyword">static</code> <code class="keyword">void</code> <code class="plain">main(String[] args)</code></div><div class="line number94 index93 alt1"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number95 index94 alt2"><code class="undefined spaces">        </code><code class="comments">// array to store prime factors</code></div><div class="line number96 index95 alt1"><code class="undefined spaces">        </code><code class="plain">factor = </code><code class="keyword">new</code> <code class="keyword">int</code><code class="plain">[MAX];</code></div><div class="line number97 index96 alt2"><code class="undefined spaces">        </code><code class="plain">factor[</code><code class="value">0</code><code class="plain">] = </code><code class="value">0</code><code class="plain">;</code></div><div class="line number98 index97 alt1"> </div><div class="line number99 index98 alt2"><code class="undefined spaces">        </code><code class="comments">// generate prime factors of number</code></div><div class="line number100 index99 alt1"><code class="undefined spaces">        </code><code class="comments">// upto 10^6</code></div><div class="line number101 index100 alt2"><code class="undefined spaces">        </code><code class="plain">generatePrimeFactors();</code></div><div class="line number102 index101 alt1"> </div><div class="line number103 index102 alt2"><code class="undefined spaces">        </code><code class="keyword">int</code> <code class="plain">a[] = { </code><code class="value">10</code><code class="plain">, </code><code class="value">30</code><code class="plain">, </code><code class="value">100</code><code class="plain">, </code><code class="value">450</code><code class="plain">, </code><code class="value">987</code> <code class="plain">};</code></div><div class="line number104 index103 alt1"> </div><div class="line number105 index104 alt2"><code class="undefined spaces">        </code><code class="keyword">int</code> <code class="plain">q = a.length;</code></div><div class="line number106 index105 alt1"> </div><div class="line number107 index106 alt2"><code class="undefined spaces">        </code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">i = </code><code class="value">0</code><code class="plain">; i < q; i++)</code></div><div class="line number108 index107 alt1"><code class="undefined spaces">            </code><code class="plain">System.out.print(calculateNoOFactors(a[i]) + </code><code class="string">" "</code><code class="plain">);</code></div><div class="line number109 index108 alt2"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number110 index109 alt1"><code class="plain">}</code></div><div class="line number111 index110 alt2"> </div><div class="line number112 index111 alt1"><code class="comments">// This code is contributed by Arnav Kr. Mandal.</code></div></div></td></tr></tbody></table></div>

                    

Python3

<div id="highlighter_169315" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments"># Python3 program to count </code></div><div class="line number2 index1 alt1"><code class="comments"># number of factors</code></div><div class="line number3 index2 alt2"><code class="comments"># of an array of integers</code></div><div class="line number4 index3 alt1"><code class="functions">MAX</code> <code class="keyword">=</code> <code class="value">1000001</code><code class="plain">;</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="comments"># array to store</code></div><div class="line number7 index6 alt2"><code class="comments"># prime factors</code></div><div class="line number8 index7 alt1"><code class="plain">factor </code><code class="keyword">=</code> <code class="plain">[</code><code class="value">0</code><code class="plain">]</code><code class="keyword">*</code><code class="plain">(</code><code class="functions">MAX</code> <code class="keyword">+</code> <code class="value">1</code><code class="plain">);</code></div><div class="line number9 index8 alt2"> </div><div class="line number10 index9 alt1"><code class="comments"># function to generate all </code></div><div class="line number11 index10 alt2"><code class="comments"># prime factors of numbers</code></div><div class="line number12 index11 alt1"><code class="comments"># from 1 to 10^6</code></div><div class="line number13 index12 alt2"><code class="keyword">def</code> <code class="plain">generatePrimeFactors():</code></div><div class="line number14 index13 alt1"><code class="undefined spaces">    </code><code class="plain">factor[</code><code class="value">1</code><code class="plain">] </code><code class="keyword">=</code> <code class="value">1</code><code class="plain">;</code></div><div class="line number15 index14 alt2"> </div><div class="line number16 index15 alt1"><code class="undefined spaces">    </code><code class="comments"># Initializes all the </code></div><div class="line number17 index16 alt2"><code class="undefined spaces">    </code><code class="comments"># positions with their value.</code></div><div class="line number18 index17 alt1"><code class="undefined spaces">    </code><code class="keyword">for</code> <code class="plain">i </code><code class="keyword">in</code> <code class="functions">range</code><code class="plain">(</code><code class="value">2</code><code class="plain">,</code><code class="functions">MAX</code><code class="plain">):</code></div><div class="line number19 index18 alt2"><code class="undefined spaces">        </code><code class="plain">factor[i] </code><code class="keyword">=</code> <code class="plain">i;</code></div><div class="line number20 index19 alt1"> </div><div class="line number21 index20 alt2"><code class="undefined spaces">    </code><code class="comments"># Initializes all </code></div><div class="line number22 index21 alt1"><code class="undefined spaces">    </code><code class="comments"># multiples of 2 with 2</code></div><div class="line number23 index22 alt2"><code class="undefined spaces">    </code><code class="keyword">for</code> <code class="plain">i </code><code class="keyword">in</code> <code class="functions">range</code><code class="plain">(</code><code class="value">4</code><code class="plain">,</code><code class="functions">MAX</code><code class="plain">,</code><code class="value">2</code><code class="plain">):</code></div><div class="line number24 index23 alt1"><code class="undefined spaces">        </code><code class="plain">factor[i] </code><code class="keyword">=</code> <code class="value">2</code><code class="plain">;</code></div><div class="line number25 index24 alt2"> </div><div class="line number26 index25 alt1"><code class="undefined spaces">    </code><code class="comments"># A modified version of </code></div><div class="line number27 index26 alt2"><code class="undefined spaces">    </code><code class="comments"># Sieve of Eratosthenes</code></div><div class="line number28 index27 alt1"><code class="undefined spaces">    </code><code class="comments"># to store the smallest </code></div><div class="line number29 index28 alt2"><code class="undefined spaces">    </code><code class="comments"># prime factor that divides</code></div><div class="line number30 index29 alt1"><code class="undefined spaces">    </code><code class="comments"># every number.</code></div><div class="line number31 index30 alt2"><code class="undefined spaces">    </code><code class="plain">i </code><code class="keyword">=</code> <code class="value">3</code><code class="plain">;</code></div><div class="line number32 index31 alt1"><code class="undefined spaces">    </code><code class="keyword">while</code><code class="plain">(i </code><code class="keyword">*</code> <code class="plain">i < </code><code class="functions">MAX</code><code class="plain">):</code></div><div class="line number33 index32 alt2"><code class="undefined spaces">        </code><code class="comments"># check if it has</code></div><div class="line number34 index33 alt1"><code class="undefined spaces">        </code><code class="comments"># no prime factor.</code></div><div class="line number35 index34 alt2"><code class="undefined spaces">        </code><code class="keyword">if</code> <code class="plain">(factor[i] </code><code class="keyword">=</code><code class="keyword">=</code> <code class="plain">i):</code></div><div class="line number36 index35 alt1"><code class="undefined spaces">            </code><code class="comments"># Initializes of j </code></div><div class="line number37 index36 alt2"><code class="undefined spaces">            </code><code class="comments"># starting from i*i</code></div><div class="line number38 index37 alt1"><code class="undefined spaces">            </code><code class="plain">j </code><code class="keyword">=</code> <code class="plain">i </code><code class="keyword">*</code> <code class="plain">i;</code></div><div class="line number39 index38 alt2"><code class="undefined spaces">            </code><code class="keyword">while</code><code class="plain">(j < </code><code class="functions">MAX</code><code class="plain">): </code></div><div class="line number40 index39 alt1"><code class="undefined spaces">                </code><code class="comments"># if it has no prime factor </code></div><div class="line number41 index40 alt2"><code class="undefined spaces">                </code><code class="comments"># before, then stores the </code></div><div class="line number42 index41 alt1"><code class="undefined spaces">                </code><code class="comments"># smallest prime divisor</code></div><div class="line number43 index42 alt2"><code class="undefined spaces">                </code><code class="keyword">if</code> <code class="plain">(factor[j] </code><code class="keyword">=</code><code class="keyword">=</code> <code class="plain">j):</code></div><div class="line number44 index43 alt1"><code class="undefined spaces">                    </code><code class="plain">factor[j] </code><code class="keyword">=</code> <code class="plain">i;</code></div><div class="line number45 index44 alt2"><code class="undefined spaces">                </code><code class="plain">j </code><code class="keyword">+</code><code class="keyword">=</code> <code class="plain">i;</code></div><div class="line number46 index45 alt1"><code class="undefined spaces">        </code><code class="plain">i</code><code class="keyword">+</code><code class="keyword">=</code><code class="value">1</code><code class="plain">;</code></div><div class="line number47 index46 alt2"> </div><div class="line number48 index47 alt1"><code class="comments"># function to calculate</code></div><div class="line number49 index48 alt2"><code class="comments"># number of factors</code></div><div class="line number50 index49 alt1"><code class="keyword">def</code> <code class="plain">calculateNoOfFactors(n):</code></div><div class="line number51 index50 alt2"><code class="undefined spaces">    </code><code class="keyword">if</code> <code class="plain">(n </code><code class="keyword">=</code><code class="keyword">=</code> <code class="value">1</code><code class="plain">):</code></div><div class="line number52 index51 alt1"><code class="undefined spaces">        </code><code class="keyword">return</code> <code class="value">1</code><code class="plain">;</code></div><div class="line number53 index52 alt2"><code class="undefined spaces">    </code><code class="plain">ans </code><code class="keyword">=</code> <code class="value">1</code><code class="plain">;</code></div><div class="line number54 index53 alt1"> </div><div class="line number55 index54 alt2"><code class="undefined spaces">    </code><code class="comments"># stores the smallest </code></div><div class="line number56 index55 alt1"><code class="undefined spaces">    </code><code class="comments"># prime number that</code></div><div class="line number57 index56 alt2"><code class="undefined spaces">    </code><code class="comments"># divides n</code></div><div class="line number58 index57 alt1"><code class="undefined spaces">    </code><code class="plain">dup </code><code class="keyword">=</code> <code class="plain">factor[n];</code></div><div class="line number59 index58 alt2"> </div><div class="line number60 index59 alt1"><code class="undefined spaces">    </code><code class="comments"># stores the count of </code></div><div class="line number61 index60 alt2"><code class="undefined spaces">    </code><code class="comments"># number of times a </code></div><div class="line number62 index61 alt1"><code class="undefined spaces">    </code><code class="comments"># prime number divides n.</code></div><div class="line number63 index62 alt2"><code class="undefined spaces">    </code><code class="plain">c </code><code class="keyword">=</code> <code class="value">1</code><code class="plain">;</code></div><div class="line number64 index63 alt1"> </div><div class="line number65 index64 alt2"><code class="undefined spaces">    </code><code class="comments"># reduces to the next </code></div><div class="line number66 index65 alt1"><code class="undefined spaces">    </code><code class="comments"># number after prime</code></div><div class="line number67 index66 alt2"><code class="undefined spaces">    </code><code class="comments"># factorization of n</code></div><div class="line number68 index67 alt1"><code class="undefined spaces">    </code><code class="plain">j </code><code class="keyword">=</code> <code class="functions">int</code><code class="plain">(n </code><code class="keyword">/</code> <code class="plain">factor[n]);</code></div><div class="line number69 index68 alt2"> </div><div class="line number70 index69 alt1"><code class="undefined spaces">    </code><code class="comments"># false when prime </code></div><div class="line number71 index70 alt2"><code class="undefined spaces">    </code><code class="comments"># factorization is done</code></div><div class="line number72 index71 alt1"><code class="undefined spaces">    </code><code class="keyword">while</code> <code class="plain">(j > </code><code class="value">1</code><code class="plain">):</code></div><div class="line number73 index72 alt2"><code class="undefined spaces">        </code><code class="comments"># if the same prime </code></div><div class="line number74 index73 alt1"><code class="undefined spaces">        </code><code class="comments"># number is dividing </code></div><div class="line number75 index74 alt2"><code class="undefined spaces">        </code><code class="comments"># n, then we increase</code></div><div class="line number76 index75 alt1"><code class="undefined spaces">        </code><code class="comments"># the count</code></div><div class="line number77 index76 alt2"><code class="undefined spaces">        </code><code class="keyword">if</code> <code class="plain">(factor[j] </code><code class="keyword">=</code><code class="keyword">=</code> <code class="plain">dup):</code></div><div class="line number78 index77 alt1"><code class="undefined spaces">            </code><code class="plain">c </code><code class="keyword">+</code><code class="keyword">=</code> <code class="value">1</code><code class="plain">;</code></div><div class="line number79 index78 alt2"> </div><div class="line number80 index79 alt1"><code class="undefined spaces">        </code><code class="comments"># if its a new prime factor</code></div><div class="line number81 index80 alt2"><code class="undefined spaces">        </code><code class="comments"># that is factorizing n, </code></div><div class="line number82 index81 alt1"><code class="undefined spaces">        </code><code class="comments"># then we again set c=1 and </code></div><div class="line number83 index82 alt2"><code class="undefined spaces">        </code><code class="comments"># change dup to the new prime </code></div><div class="line number84 index83 alt1"><code class="undefined spaces">        </code><code class="comments"># factor, and apply the formula </code></div><div class="line number85 index84 alt2"><code class="undefined spaces">        </code><code class="comments"># explained above. </code></div><div class="line number86 index85 alt1"><code class="undefined spaces">        </code><code class="keyword">else</code><code class="plain">:</code></div><div class="line number87 index86 alt2"><code class="undefined spaces">            </code><code class="plain">dup </code><code class="keyword">=</code> <code class="plain">factor[j];</code></div><div class="line number88 index87 alt1"><code class="undefined spaces">            </code><code class="plain">ans </code><code class="keyword">=</code> <code class="plain">ans </code><code class="keyword">*</code> <code class="plain">(c </code><code class="keyword">+</code> <code class="value">1</code><code class="plain">);</code></div><div class="line number89 index88 alt2"><code class="undefined spaces">            </code><code class="plain">c </code><code class="keyword">=</code> <code class="value">1</code><code class="plain">;</code></div><div class="line number90 index89 alt1"> </div><div class="line number91 index90 alt2"><code class="undefined spaces">        </code><code class="comments"># prime factorizes</code></div><div class="line number92 index91 alt1"><code class="undefined spaces">        </code><code class="comments"># a number</code></div><div class="line number93 index92 alt2"><code class="undefined spaces">        </code><code class="plain">j </code><code class="keyword">=</code> <code class="functions">int</code><code class="plain">(j </code><code class="keyword">/</code> <code class="plain">factor[j]);</code></div><div class="line number94 index93 alt1"> </div><div class="line number95 index94 alt2"><code class="undefined spaces">    </code><code class="comments"># for the last</code></div><div class="line number96 index95 alt1"><code class="undefined spaces">    </code><code class="comments"># prime factor</code></div><div class="line number97 index96 alt2"><code class="undefined spaces">    </code><code class="plain">ans </code><code class="keyword">=</code> <code class="plain">ans </code><code class="keyword">*</code> <code class="plain">(c </code><code class="keyword">+</code> <code class="value">1</code><code class="plain">);</code></div><div class="line number98 index97 alt1"> </div><div class="line number99 index98 alt2"><code class="undefined spaces">    </code><code class="keyword">return</code> <code class="plain">ans;</code></div><div class="line number100 index99 alt1"><code class="comments"># Driver Code</code></div><div class="line number101 index100 alt2"> </div><div class="line number102 index101 alt1"><code class="keyword">if</code> <code class="plain">__name__ </code><code class="keyword">=</code><code class="keyword">=</code> <code class="string">"__main__"</code><code class="plain">:</code></div><div class="line number103 index102 alt2"><code class="undefined spaces">    </code> </div><div class="line number104 index103 alt1"><code class="undefined spaces">    </code> </div><div class="line number105 index104 alt2"><code class="comments"># generate prime factors</code></div><div class="line number106 index105 alt1"><code class="comments"># of number upto 10^6</code></div><div class="line number107 index106 alt2"><code class="undefined spaces">    </code><code class="plain">generatePrimeFactors()</code></div><div class="line number108 index107 alt1"><code class="undefined spaces">    </code><code class="plain">a </code><code class="keyword">=</code> <code class="plain">[</code><code class="value">10</code><code class="plain">, </code><code class="value">30</code><code class="plain">, </code><code class="value">100</code><code class="plain">, </code><code class="value">450</code><code class="plain">, </code><code class="value">987</code><code class="plain">]</code></div><div class="line number109 index108 alt2"><code class="undefined spaces">    </code><code class="plain">q </code><code class="keyword">=</code> <code class="functions">len</code><code class="plain">(a)</code></div><div class="line number110 index109 alt1"><code class="undefined spaces">    </code><code class="keyword">for</code> <code class="plain">i </code><code class="keyword">in</code> <code class="functions">range</code> <code class="plain">(</code><code class="value">0</code><code class="plain">,q):</code></div><div class="line number111 index110 alt2"><code class="undefined spaces">        </code><code class="functions">print</code><code class="plain">(calculateNoOFactors(a[i]),end</code><code class="keyword">=</code><code class="string">" "</code><code class="plain">)</code></div><div class="line number112 index111 alt1"> </div><div class="line number113 index112 alt2"><code class="comments"># This code is contributed </code></div><div class="line number114 index113 alt1"><code class="comments"># by mits.</code></div></div></td></tr></tbody></table></div>

                    

C#


                    

PHP


                    

Javascript


                    

Output: 
 

4 8 9 18 8 


Time Complexity: O(n * log(max(number))), where n is total number of elements in the array and max(number) represents the maximum number in the array. 
Auxiliary Space: O(n) for calculating the sieve.


 



Last Updated : 03 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads