-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsieve.cpp
More file actions
38 lines (33 loc) · 744 Bytes
/
sieve.cpp
File metadata and controls
38 lines (33 loc) · 744 Bytes
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
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<vector>
#define lli long long int
#define MAX 100
using namespace std;
bool v[MAX]; // visited elements
int len, sp[MAX];
void Sieve(){
for (int i = 2; i < MAX; i += 2) sp[i] = 2;//even numbers have smallest prime factor 2
for (lli i = 3; i < MAX; i += 2){
if (!v[i]){
sp[i] = i;
for (lli j = i; (j*i) < MAX; j += 2){
if (!v[j*i]) v[j*i] = true, sp[j*i] = i;
}
}
}
}
vector <int> factorize(int k) { //this is how to factorize k. //result is stored in vector<int>
vector <int> ans;
while(k>1) {
ans.push_back(sp[k]);
k/=sp[k];
}
return ans;
}
int main(){
Sieve();
for (int i = 0; i < 50; i++) cout << sp[i] << "\t";
return 0;
}