Last active
July 4, 2018 12:03
-
-
Save gushwell/541e5e28ffdd9667611bef0a9e0e637b to your computer and use it in GitHub Desktop.
C#:メビウス関数 ref: https://qiita.com/gushwell/items/7715d8be81fa600dd9c8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mebius = new Mebius(100); | |
int n = mebius[10]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mebius = new Mebius(100); | |
int n = mebius[10]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
μ(1) = 1 | |
μ(2) = -1 | |
μ(3) = -1 | |
μ(4) = 0 | |
μ(5) = -1 | |
μ(6) = 1 | |
μ(7) = -1 | |
μ(8) = 0 | |
μ(9) = 0 | |
μ(10) = 1 | |
μ(11) = -1 | |
μ(12) = 0 | |
μ(13) = -1 | |
μ(14) = 1 | |
μ(15) = 1 | |
μ(16) = 0 | |
μ(17) = -1 | |
μ(18) = 0 | |
μ(19) = -1 | |
μ(20) = 0 | |
μ(21) = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
μ(1) = 1 | |
μ(2) = -1 | |
μ(3) = -1 | |
μ(4) = 0 | |
μ(5) = -1 | |
μ(6) = 1 | |
μ(7) = -1 | |
μ(8) = 0 | |
μ(9) = 0 | |
μ(10) = 1 | |
μ(11) = -1 | |
μ(12) = 0 | |
μ(13) = -1 | |
μ(14) = 1 | |
μ(15) = 1 | |
μ(16) = 0 | |
μ(17) = -1 | |
μ(18) = 0 | |
μ(19) = -1 | |
μ(20) = 0 | |
μ(21) = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace Gushwell.Etude { | |
class Program { | |
static void Main(string[] args) { | |
// 1 - 100 までのメビウス関数を求める | |
int upper = 100; | |
Mebius mebius = new Mebius(upper); | |
for (int i = 1; i <= upper; i++) { | |
Console.WriteLine("μ({0}) = {1}", i, mebius[i]); | |
} | |
Console.ReadLine(); | |
} | |
} | |
class Mebius { | |
private int[] mebius; | |
public Mebius(int maxnum) { | |
mebius = Enumerable.Repeat(1, maxnum + 1).ToArray(); | |
foreach (int p in PrimeNumber.Enumerate().TakeWhile(n => n <= maxnum)) { | |
for (int i = p; i <= maxnum; i += p) | |
mebius[i] *= -1; | |
int p2 = p * p; | |
for (int pp = p2; pp <= maxnum; pp += p2) | |
mebius[pp] = 0; | |
} | |
} | |
public int this[int n] { | |
get { | |
return mebius[n]; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment