Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 411bc08

Browse files
Merge pull request #113 from bg2404/master
Added Mobius Function in Number Theory
2 parents d18aaea + 9af2de1 commit 411bc08

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎09- Number Theory/Mobius.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
vector<int> mobius(int n) {
6+
vector<int> prime;
7+
vector<int> mu(n + 1);
8+
vector<bool> isComposite(n + 1, false);
9+
mu[1] = 1;
10+
for (int i = 2; i < n; ++i) {
11+
if (!isComposite[i]) {
12+
prime.push_back(i);
13+
mu[i] = -1;
14+
}
15+
for (int j = 0; j < prime.size() && i * prime[j] <= n; ++j) {
16+
isComposite[i * prime[j]] = true;
17+
if (i % prime[j] == 0) {
18+
mu[i * prime[j]] = 0;
19+
break;
20+
} else {
21+
mu[i * prime[j]] = mu[i] * mu[prime[j]];
22+
}
23+
}
24+
}
25+
return mu;
26+
}
27+
28+
signed main() {
29+
int n;
30+
cin >> n;
31+
vector<int> mu = mobius(n);
32+
for (int i : mu) {
33+
cout << i << ' ';
34+
}
35+
cout << '\n';
36+
return 0;
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /