0% found this document useful (0 votes)
11 views1 page

Prim Table

The PrimTable struct is designed to handle prime factorization and prime number identification up to a given integer n. It initializes a list of least prime factors (lp) and a list of primes (pr) using a sieve-like method. The struct provides methods to check if a number is prime and to retrieve the prime factors of a given number.

Uploaded by

kuo2004731
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

Prim Table

The PrimTable struct is designed to handle prime factorization and prime number identification up to a given integer n. It initializes a list of least prime factors (lp) and a list of primes (pr) using a sieve-like method. The struct provides methods to check if a number is prime and to retrieve the prime factors of a given number.

Uploaded by

kuo2004731
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

struct PrimTable {

int n;
vector<int> pr, lp;

inline PrimTable(int n_ = 0) {
n = n_;
lp.resize(n + 1);
init();
}

inline void init() {


if (n < 1) return;
lp[1] = 1;
for (int i = 2; i <= n; ++i) {
if (lp[i] == 0) {
lp[i] = i;
pr.push_back(i);
}
for (int j = 0; i * pr[j] <= n; ++j) {
lp[i * pr[j]] = pr[j];
if (pr[j] == lp[i]) {
break;
}
}
}
}

inline bool isp(int x) {


return x < 2 ? false : (lp[x] == x);
}

inline vector<int> div(int k) {


assert(k <= n);
if (k < 2) return vector<int>();

vector<int> res;
while (k > 1) {
const int f = lp[k];
do {
k /= f;
} while (k % f == 0);
res.push_back(f);
}
return res;
}
};

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy