bhupixb

Prime/PrimeFactorization Template

Feb 11th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. vector<int32_t> spf, primes;
  2. void RunSieve(const int N) {
  3. spf.resize(N+1);
  4. iota(spf.begin(), spf.end(), 0);
  5. for (int i = 2; i < N; i += 2) {
  6. spf[i] = 2;
  7. }
  8. for(int i = 3; i * i < N; i += 2) {
  9. if (spf[i] == i) {
  10. for (int j = i * i; j < N; j += i<<1) {
  11. if (spf[j] == j) {
  12. spf[j] = i;
  13. }
  14. }
  15. }
  16. }
  17. }
  18.  
  19. bool isPrime(const int x) {
  20. if (x & 1) return (spf[x] == x && (x > 1));
  21. return x == 2;
  22. }
  23.  
  24. auto primeFactorization(int n) {
  25. vector<pair<int,int>> pf;
  26. if (spf[n] == 1) pf = {{n,1}};
  27. else {
  28. int cur = n;
  29. do {
  30. int cpf = spf[cur], exp = 0;
  31. while (cur % cpf == 0) {
  32. cur /= cpf, ++exp;
  33. }
  34. pf.push_back(make_pair(cpf,exp));
  35. } while (cur > 1);
  36. }
  37. return pf;
  38. }
Add Comment
Please, Sign In to add comment