Advertisement
newb_ie

Prime Factorization

Aug 23rd, 2021
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 10000;
  5. int64_t isVisited[maxN / 64 + 200];
  6. vector<int> primes;
  7. void Sieve_of_Eratosthenes(int limit) {
  8. limit += 100;
  9. for (int64_t i = 3; i <= sqrt(limit) ; i += 2) {
  10. if (!(isVisited[i / 64] & (1LL << (i % 64)))) {
  11. for (int64_t j = i * i; j <= limit; j += 2 * i) {
  12. isVisited[j / 64] |= (1LL << (j % 64));
  13. }
  14. }
  15. }
  16. primes.push_back(2);
  17. for (int64_t i = 3; i <= limit; i += 2) {
  18. if (!(isVisited[i / 64] & (1LL << (i % 64)))) {
  19. primes.push_back(i);
  20. }
  21. }
  22. }
  23.  
  24. bool is_prime(int n) {
  25. if (n < 2) return false;
  26. if (n == 2) return true;
  27. if (n % 2 == 0) return false;
  28. if (!(isVisited[n / 64] & (1LL << (n % 64)))) return true;
  29. return false;
  30. }
  31.  
  32. int number_of_PF (int n) {
  33. int count = 0;
  34. for (int p : primes) {
  35. if (p * p > n) break;
  36. if (n % p == 0) {
  37. ++count;
  38. while (n % p == 0) n /= p;
  39. }
  40. }
  41. if (n > 1) ++count;
  42. return count;
  43. }
  44.  
  45. vector<int> Extract_PF (int n) {
  46. vector<int> pf;
  47. for (int p : primes) {
  48. if (p * p > n) break;
  49. if (n % p == 0) {
  50. pf.push_back(p);
  51. while (n % p == 0) n /= p;
  52. }
  53. }
  54. if (n > 1) pf.push_back(n);
  55. return pf;
  56. }
  57.  
  58. vector<pair<int,int>> Extract_PF_with_count (int n) {
  59. vector<pair<int,int>> pf;
  60. for (int p : primes) {
  61. if (p * p > n) break;
  62. if (n % p == 0) {
  63. int count = 0;
  64. while (n % p == 0) {
  65. ++count,n /= p;
  66. }
  67. pf.push_back(make_pair(p,count));
  68. }
  69. }
  70. if (n > 1) pf.push_back(make_pair(n,1));
  71. return pf;
  72. }
  73.  
  74. int main () {
  75. ios::sync_with_stdio(false);
  76. cin.tie(nullptr);
  77. cout.tie(nullptr);
  78. Sieve_of_Eratosthenes(maxN);
  79. int count = number_of_PF(120);
  80. vector<int> pf = Extract_PF(120);
  81. vector<pair<int,int>> pf_with_count = Extract_PF_with_count(120);
  82. cout << "Number of Primes in PF of 120 : " << count << '\n';
  83. cout << "List : \n";
  84. for (int p : pf) {
  85. cout << p << ' ';
  86. }
  87. cout << "\nList with Count\n";
  88. for (pair<int,int> p : pf_with_count) {
  89. cout << p.first << ' ' << p.second << '\n';
  90. }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement