Skip to content

Commit 764860a

Browse files
authored
Update Primes.cpp
1 parent 715c3fc commit 764860a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Template/Math/Primes.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ vector<int>Eratosthenes(int n)
2020
}
2121
return primes;
2222
}
23+
24+
// Find the smallest prime factor for each element
25+
vector<int>spf(int N) {
26+
vector<int>spf(N+1);
27+
for (int i = 0; i <= N; ++i) spf[i] = i;
28+
for (int i = 2; i * i <= maxA; ++i) {
29+
if (spf[i] == i) {
30+
for (int j = i * i; j <= N; j += i) {
31+
if (spf[j] == j) spf[j] = i;
32+
}
33+
}
34+
}
35+
return spf;
36+
}

0 commit comments

Comments
 (0)