-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEratostenes sieve - maximal prime factors v1.cpp
69 lines (48 loc) · 1.56 KB
/
Eratostenes sieve - maximal prime factors v1.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<cstdio>
#include<vector>
typedef std::pair<int, int> PII;
typedef std::vector <PII> VPII;
#define REP(i,a,b,skok) for(int i=a; i<=b; i+=skok)
#define FOR(i,a,b) for(int i=a; i*i<=b; ++i)
#define prime first
#define exponent second
#define MP std::make_pair
const int MAX_N = 1000000;
// biggestFactors[k] = (p, alfa) (k=2,...,n) iff:
// 1. p^alfa | k and
// 2. !(p^(alfa+1) | k) and
// 3. p is the biggest possible prime such that p | k
VPII biggestFactors(MAX_N+1);
// Eratostenes sieve in O(n*loglog(n)) time.
// n >= 2
// biggestFactors[0..1] = (0, 0) which means that 0 and 1 are neither prime nor composite numbers
void EratostenesSieve(int n){
REP(i,0,1,1){
biggestFactors[i] = MP(0, 0);
}
REP(i,2,n,1){
biggestFactors[i] = MP(i, 1); // initially, we assume that all numbers i=2,...,n are primes so i^1 | i for i=2,...,n
}
REP(i,2,n/2,1){
if( biggestFactors[i].prime == i ){ // if i is prime
REP(j,2*i,n,i){
biggestFactors[j].prime = i;
if( biggestFactors[ j / i ].prime == i ){
biggestFactors[j].exponent = biggestFactors[ j / i ].exponent + 1;
}
else{
biggestFactors[j].exponent = 1;
}
}
} // after this loop: i is prime (i=2,...,n) iff biggestFactors[i] = (i,1)
}
}
int main(){
int n;
scanf( "%d", &n );
EratostenesSieve(n);
REP(i,2,std::min(n,50),1){
printf( "i = %d: biggest prime factor and its maximal exponent is (%d, %d)\n", i, biggestFactors[i].prime, biggestFactors[i].exponent );
}
return 0;
}