Prime factors of LCM of array elements - GeeksforGeeks
Given an array arr[] such that 1 <= arr[i] <= 10^12, the task is to find prime factors of LCM of array elements.
http://www.geeksforgeeks.org/sieve-sundaram-print-primes-smaller-n/
Read full article from Prime factors of LCM of array elements - GeeksforGeeks
Given an array arr[] such that 1 <= arr[i] <= 10^12, the task is to find prime factors of LCM of array elements.
A simple solution for this problem is to find LCM of n elements in array. First initialize lcm = 1, then iterate for each element in array and find the lcm of previous result with new element using formula LCM(a, b) = (a * b) / gcd(a, b) i.e., lcm = (lcm * arr[i]) / gcd(lcm, arr[i]). After finding LCM of all n elements we can calculate all prime factors of LCM.
Since here constraint are large, we can not implement above method to solve this problem because while calculating LCM(a, b) we need to calculate a*b and if a,b both are of value 10^12 so it will exceed the limit of integer size. We proceed for this problem in another way using sieve of sundaram and prime factorization of a number. As we know if LCM(a,b) = k so any prime factor of a or b will also be the prime factor of ‘k’.
- Take a array factor[] of size 10^6 and initialize it with 0 because prime factor of any number are always less than and equal to its square root and in our constraint arr[i] <= 10^12.
- Generate all primes less than and equal to 10^6 and store them in another array.
- Now one by one calculate all prime factors of each number in array and mark them as 1 in factor[] array.
- Now traverse factor[] array and print all indexes which are marked as 1 because these will be prime factors of lcm of n numbers in given array.
const
int
MAX = 1000000;
typedef
long
long
int
ll;
// array to store all prime less than and equal to 10^6
vector <
int
> primes;
// utility function for sieve of sundaram
void
sieve()
{
int
n = MAX;
// In general Sieve of Sundaram, produces primes smaller
// than (2*x + 2) for a number given number x. Since
// we want primes smaller than n, we reduce n to half
int
nNew = (n)/2;
// This array is used to separate numbers of the form
// i+j+2ij from others where 1 <= i <= j
bool
marked[nNew + 100];
// Initalize all elements as not marked
memset
(marked,
false
,
sizeof
(marked));
// Main logic of Sundaram. Mark all numbers which do not
// generate prime number by doing 2*i+1
int
tmp=
sqrt
(n);
for
(
int
i=1; i<=(tmp-1)/2; i++)
for
(
int
j=(i*(i+1))<<1; j<=nNew; j=j+2*i+1)
marked[j] =
true
;
// Since 2 is a prime number
primes.push_back(2);
// Print other primes. Remaining primes are of the form
// 2*i + 1 such that marked[i] is false.
for
(
int
i=1; i<=nNew; i++)
if
(marked[i] ==
false
)
primes.push_back(2*i + 1);
}
// Function to find prime factors of of n elements of
// given array
void
primeLcm(ll arr[],
int
n )
{
// factors[] --> array to mark all prime factors of
// lcm of array elements
int
factors[MAX] = {0};
// One by one calculate prime factors of number
// and mark them in factors[] array
for
(
int
i=0; i<n; i++)
{
// copy --> duplicate of original element to
// perform operation
ll copy = arr[i];
// sqr --> square root of current number 'copy'
// because all prime factors are always less
// than and equal to square root of given number
int
sqr =
sqrt
(copy);
// check divisibility with prime factor
for
(
int
j=0; primes[j]<=sqr; j++)
{
// if current prime number is factor of 'copy'
if
(copy%primes[j] == 0)
{
// divide with current prime factor until
// it can divide the number
while
(copy%primes[j] == 0)
copy = copy/primes[j];
// mark current prime factor as 1 in
// factors[] array
factors[primes[j]] = 1;
}
}
// After calculating exponents of all prime factors
// either value of 'copy' will be 1 because of
// complete divisibility or remaining value of
// 'copy' will be surely a prime , so we will
// also mark this prime as a factor
if
(copy > 1)
factors[copy] = 1;
}
// if 2 is prime factor of lcm of all elements
// in given array
if
(factors[2] == 1)
cout << 2 <<
" "
;
// traverse to print all prime factors of lcm of
// all elements in given array
for
(
int
i=3; i<=MAX; i=i+2)
if
(factors[i] == 1)
cout << i <<
" "
;
}
Read full article from Prime factors of LCM of array elements - GeeksforGeeks