Skip to content

Instantly share code, notes, and snippets.

@NamPE286
Created April 11, 2023 18:10
Show Gist options
  • Save NamPE286/ba67206a726d7b0d5b8c3eb253537ac1 to your computer and use it in GitHub Desktop.
Save NamPE286/ba67206a726d7b0d5b8c3eb253537ac1 to your computer and use it in GitHub Desktop.
Prime factorization with sieve of Eratosthenes
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<vector<ll>> sieve(ll n){
vector<vector<ll>> fact(n + 1);
for(ll i = 2; i <= n; i++){
if(fact[i].empty()){
for(ll j = i; j <= n; j += i){
fact[j].push_back(i);
}
}
}
return fact;
}
int main(){
auto fact = sieve(4);
for(ll i : fact[4]) cout << i << ' ';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment