-
Notifications
You must be signed in to change notification settings - Fork 0
/
lc2521.c
45 lines (35 loc) · 817 Bytes
/
lc2521.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Licensed under the MIT License.
// Distinct Prime Factors of a Product of Array
#include "../lib/euler.h"
#include "../lib/factor_iterator.h"
bool lazy;
struct Sieve primes;
int distinctPrimeFactors(int values[], int valuesSize)
{
if (!lazy)
{
euler_ok(sieve(&primes, 1000));
lazy = true;
}
int count = 0;
bool set[1000] = { 0 };
for (int i = 0; i < valuesSize; i++)
{
struct FactorIterator it;
for (factor_begin(&it, values[i], &primes);
!factor_end(&it);
factor_next(&it))
{
if (!set[it.current])
{
count++;
set[it.current] = true;
}
}
}
return count;
}
void finalizeDistinctPrimeFactors()
{
finalize_sieve(&primes);
}