-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smallest prime factors + number of prime factors (linear sieve).cpp
137 lines (93 loc) · 3.23 KB
/
Smallest prime factors + number of prime factors (linear sieve).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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Copyright (C) Pawel Masluch, 2021. All rights reserved.
Number of prime factors of x, using linear sieve.
Bibliography:
1. https://eduinf.waw.pl/inf/alg/001_search/0012.php
2. https://codeforces.com/blog/entry/54090
3. https://cp-algorithms.com/algebra/prime-sieve-linear.html
4. https://www.cs.utexas.edu/users/misra/scannedPdf.dir/linearSieve.pdf
*/
#include<cstdio>
#include<vector>
typedef long long LL;
typedef std::pair <int, int> PII;
typedef std::pair <PII, int> PIII;
typedef std::vector <PIII> VPIII;
typedef std::vector <int> VI;
#define REP(i,a,b) for(int i=a; i<=b; ++i)
#define MP std::make_pair
#define prime first.first
#define exponent first.second
#define powerOfPrime second
const int MAX_N = 100000000;
/*
smallestPrimeFactors[x] = (p, alfa, p^alfa) iff:
1. p^alfa | x
2. ![ p^(alfa+1) | x ]
3. p is smallest prime such that p|x
*/
VPIII smallestPrimeFactors(MAX_N+1);
// numberOfPrimeFactors[x] - number of prime factors of x
VI numberOfPrimeFactors(MAX_N+1);
void linearSieve(int n){ // we find smallest prime factors, for x in {0,1,2,...,n} and MAX_N >= n >= 2
REP(i,0,1){
numberOfPrimeFactors[i] = 0; // by definition
smallestPrimeFactors[i] = MP( MP(0, 0), 1 ); // by definition
}
REP(i,2,n){
smallestPrimeFactors[i] = MP( MP(i, 1), i ); // initially, we assume that all numbers i in {2,...,n} are primes so smallestPrimeFactors[i] = (i, 1, i^1=i)
}
int p=2, q=2;
while( p*q <= n ){
LL P = p;
int k = 1;
while( P*q <= n ){ // P*q = p^k * q <= n
// P*q = p^k * q is composite
smallestPrimeFactors[ P*q ].prime = p;
smallestPrimeFactors[ P*q ].exponent = k;
smallestPrimeFactors[ P*q ].powerOfPrime = P; // p^k
if( q == p ){
++smallestPrimeFactors[ P*q ].exponent; // k+1
smallestPrimeFactors[ P*q ].powerOfPrime *= p; // p^(k+1)
}
P *= p;
++k;
}
// we find the smallest number, thought cuurently as prime, bigger than q
do{
++q;
}
while( smallestPrimeFactors[q].prime < q );
if( p*q > n ){ // p*q > n
// we find the smallest number, thought cuurently as prime, bigger than q
do{
++p;
}
while( smallestPrimeFactors[p].prime < p );
q = p;
}
}
// ------------------------
// Finding numbers of prime factors using dynamic programming.
REP(i,2,n){
numberOfPrimeFactors[i] = numberOfPrimeFactors[ i / smallestPrimeFactors[i].powerOfPrime ] + 1;
}
}
int main(){
printf( "Data input\nEnter an upper bound of linear sieve:\nn = " );
int n; // we find smallest prime factors, for x in {0,1,2,...,n} and MAX_N >= n >= 2
scanf( "%d", &n );
// ------------------------
linearSieve(n);
// ------------------------
printf( "\n\nResults:\nSmallest prime factors:\n" );
REP(i,2,std::min(n,100)){
printf( "smallestPrimeFactors[%d] = (%d, %d, %d)\n", i, smallestPrimeFactors[i].prime, smallestPrimeFactors[i].exponent, smallestPrimeFactors[i].powerOfPrime );
}
// ------------------------
printf( "\nNumber of prime factors:\n" );
REP(i,2,std::min(n,100)){
printf( "numberOfPrimeFactors[%d] = %d\n", i, numberOfPrimeFactors[i] );
}
return 0;
}