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.
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++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000001;
int factor[MAX] = { 0 };
void generatePrimeFactors()
{
factor[1] = 1;
for ( int i = 2; i < MAX; i++)
factor[i] = i;
for ( int i = 4; i < MAX; i += 2)
factor[i] = 2;
for ( int i = 3; i * i < MAX; i++) {
if (factor[i] == i) {
for ( int j = i * i; j < MAX; j += i) {
if (factor[j] == j)
factor[j] = i;
}
}
}
}
int calculateNoOfFactors( int n)
{
if (n == 1)
return 1;
int ans = 1;
int dup = factor[n];
int c = 1;
int j = n / factor[n];
while (j != 1) {
if (factor[j] == dup)
c += 1;
else {
dup = factor[j];
ans = ans * (c + 1);
c = 1;
}
j = j / factor[j];
}
ans = ans * (c + 1);
return ans;
}
int main()
{
generatePrimeFactors();
int a[] = { 10, 30, 100, 450, 987 };
int q = sizeof (a) / sizeof (a[0]);
for ( int i = 0; i < q; i++)
cout << calculateNoOfFactors(a[i]) << " " ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int MAX = 1000001 ;
static int factor[];
static void generatePrimeFactors()
{
factor[ 1 ] = 1 ;
for ( int i = 2 ; i < MAX; i++)
factor[i] = i;
for ( int i = 4 ; i < MAX; i += 2 )
factor[i] = 2 ;
for ( int i = 3 ; i * i < MAX; i++) {
if (factor[i] == i) {
for ( int j = i * i; j < MAX; j += i) {
if (factor[j] == j)
factor[j] = i;
}
}
}
}
static int calculateNoOfFactors( int n)
{
if (n == 1 )
return 1 ;
int ans = 1 ;
int dup = factor[n];
int c = 1 ;
int j = n / factor[n];
while (j != 1 ) {
if (factor[j] == dup)
c += 1 ;
else {
dup = factor[j];
ans = ans * (c + 1 );
c = 1 ;
}
j = j / factor[j];
}
ans = ans * (c + 1 );
return ans;
}
public static void main(String[] args)
{
factor = new int [MAX];
factor[ 0 ] = 0 ;
generatePrimeFactors();
int a[] = { 10 , 30 , 100 , 450 , 987 };
int q = a.length;
for ( int i = 0 ; i < q; i++)
System.out.print(calculateNoOFactors(a[i]) + " " );
}
}
|
Python3
MAX = 1000001 ;
factor = [ 0 ] * ( MAX + 1 );
def generatePrimeFactors():
factor[ 1 ] = 1 ;
for i in range ( 2 , MAX ):
factor[i] = i;
for i in range ( 4 , MAX , 2 ):
factor[i] = 2 ;
i = 3 ;
while (i * i < MAX ):
if (factor[i] = = i):
j = i * i;
while (j < MAX ):
if (factor[j] = = j):
factor[j] = i;
j + = i;
i + = 1 ;
def calculateNoOfFactors(n):
if (n = = 1 ):
return 1 ;
ans = 1 ;
dup = factor[n];
c = 1 ;
j = int (n / factor[n]);
while (j > 1 ):
if (factor[j] = = dup):
c + = 1 ;
else :
dup = factor[j];
ans = ans * (c + 1 );
c = 1 ;
j = int (j / factor[j]);
ans = ans * (c + 1 );
return ans;
if __name__ = = "__main__" :
generatePrimeFactors()
a = [ 10 , 30 , 100 , 450 , 987 ]
q = len (a)
for i in range ( 0 ,q):
print (calculateNoOFactors(a[i]),end = " " )
|
C#
using System;
class GFG {
static int MAX = 1000001;
static int [] factor;
static void generatePrimeFactors()
{
factor[1] = 1;
for ( int i = 2; i < MAX; i++)
factor[i] = i;
for ( int i = 4; i < MAX; i += 2)
factor[i] = 2;
for ( int i = 3; i * i < MAX; i++)
{
if (factor[i] == i)
{
for ( int j = i * i;
j < MAX; j += i)
{
if (factor[j] == j)
factor[j] = i;
}
}
}
}
static int calculateNoOfFactors( int n)
{
if (n == 1)
return 1;
int ans = 1;
int dup = factor[n];
int c = 1;
int j = n / factor[n];
while (j != 1) {
if (factor[j] == dup)
c += 1;
else {
dup = factor[j];
ans = ans * (c + 1);
c = 1;
}
j = j / factor[j];
}
ans = ans * (c + 1);
return ans;
}
public static void Main()
{
factor = new int [MAX];
factor[0] = 0;
generatePrimeFactors();
int [] a = { 10, 30, 100, 450, 987 };
int q = a.Length;
for ( int i = 0; i < q; i++)
Console.Write(
calculateNoOFactors(a[i])
+ " " );
}
}
|
PHP
<?php
$MAX = 1000001;
$factor = array_fill (0, $MAX + 1, 0);
function generatePrimeFactors()
{
global $factor ;
global $MAX ;
$factor [1] = 1;
for ( $i = 2; $i < $MAX ; $i ++)
$factor [ $i ] = $i ;
for ( $i = 4; $i < $MAX ; $i += 2)
$factor [ $i ] = 2;
for ( $i = 3; $i * $i < $MAX ; $i ++)
{
if ( $factor [ $i ] == $i )
{
for ( $j = $i * $i ;
$j < $MAX ; $j += $i )
{
if ( $factor [ $j ] == $j )
$factor [ $j ] = $i ;
}
}
}
}
function calculateNoOfFactors( $n )
{
global $factor ;
if ( $n == 1)
return 1;
$ans = 1;
$dup = $factor [ $n ];
$c = 1;
$j = (int)( $n / $factor [ $n ]);
while ( $j != 1)
{
if ( $factor [ $j ] == $dup )
$c += 1;
else
{
$dup = $factor [ $j ];
$ans = $ans * ( $c + 1);
$c = 1;
}
$j = (int)( $j / $factor [ $j ]);
}
$ans = $ans * ( $c + 1);
return $ans ;
}
generatePrimeFactors();
$a = array (10, 30, 100, 450, 987);
$q = sizeof( $a );
for ( $i = 0; $i < $q ; $i ++)
echo calculateNoOFactors( $a [ $i ]) . " " ;
?>
|
Javascript
<script>
var MAX = 1000001;
var factor = [];
function generatePrimeFactors() {
factor[1] = 1;
for (i = 2; i < MAX; i++)
factor[i] = i;
for (i = 4; i < MAX; i += 2)
factor[i] = 2;
for (i = 3; i * i < MAX; i++)
{
if (factor[i] == i)
{
for (j = i * i; j < MAX; j += i)
{
if (factor[j] == j)
factor[j] = i;
}
}
}
}
function calculateNoOfFactors(n)
{
if (n == 1)
return 1;
var ans = 1;
var dup = factor[n];
var c = 1;
var j = n / factor[n];
while (j != 1)
{
if (factor[j] == dup)
c += 1;
else
{
dup = factor[j];
ans = ans * (c + 1);
c = 1;
}
j = j / factor[j];
}
ans = ans * (c + 1);
return ans;
}
factor = Array(MAX).fill(0);
factor[0] = 0;
generatePrimeFactors();
var a = [ 10, 30, 100, 450, 987 ];
var q = a.length;
for (i = 0; i < q; i++)
document.write(calculateNoOFactors(a[i]) + " " );
</script>
|
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
03 Oct, 2022
Like Article
Save Article