莫比乌斯函数是组合函数中使用的乘法函数。它具有三个可能的值-1、0和1之一。
例子:
Input : 6
Output : 1
Solution: Prime Factors: 2 3.
Therefore p = 2, (-1)^p = 1
Input: 49
Output: 0
Solution: Prime Factors: 7 ( occurs twice).
Since the prime factor occurs twice answer
is 0.
Input: 3
Output: -1
Solution: Prime Factors: 3. Therefore p = 1,
(-1) ^ p =-1
Input : 78
Output : 1
Solution: Prime Factors: 3, 13. Therefore p = 2,
(-1)^p = 1
方法1(简单)
我们遍历所有小于或等于N的数字i。对于每个数字,我们检查它是否除以N。如果是,则检查它是否也为质数。如果两个条件都满足,则检查其平方是否也除以N。如果是,则返回0。如果平方不除,则增加素数的计数。最后,如果素数个数为偶数,则返回1;如果素数个数为奇数,则返回-1。
C++
// CPP Program to evaluate Mobius Function
// M(N) = 1 if N = 1
// M(N) = 0 if any prime factor of N is contained twice
// M(N) = (-1)^(no of distinct prime factors)
#include
using namespace std;
// Function to check if n is prime or not
bool isPrime(int n)
{
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
int mobius(int N)
{
// Base Case
if (N == 1)
return 1;
// For a prime factor i check if i^2 is also
// a factor.
int p = 0;
for (int i = 1; i <= N; i++) {
if (N % i == 0 && isPrime(i)) {
// Check if N is divisible by i^2
if (N % (i * i) == 0)
return 0;
else
// i occurs only once, increase f
p++;
}
}
// All prime factors are contained only once
// Return 1 if p is even else -1
return (p % 2 != 0)? -1 : 1;
}
// Driver code
int main()
{
int N = 17;
cout << "Mobius Functions M(N) at N = " << N << " is: "
<< mobius(N) << endl;
cout << "Mobius Functions M(N) at N = " << 25 << " is: "
<< mobius(25) << endl;
cout << "Mobius Functions M(N) at N = " << 6 << " is: "
<< mobius(6) << endl;
}
Java
// Java program for mobious function
import java.io.*;
public class GFG {
// C# Program to evaluate Mobius
// Function: M(N) = 1 if N = 1
// M(N) = 0 if any prime factor
// of N is contained twice
// M(N) = (-1)^(no of distinct
// prime factors)
// Function to check if n is
// prime or not
static boolean isPrime(int n)
{
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
static int mobius(int N)
{
// Base Case
if (N == 1)
return 1;
// For a prime factor i check if
// i^2 is also a factor.
int p = 0;
for (int i = 1; i <= N; i++) {
if (N % i == 0 && isPrime(i)) {
// Check if N is divisible by i^2
if (N % (i * i) == 0)
return 0;
else
// i occurs only once, increase f
p++;
}
}
// All prime factors are contained only
// once Return 1 if p is even else -1
return (p % 2 != 0) ? -1 : 1;
}
// Driver code
static public void main(String[] args)
{
int N = 17;
System.out.println("Mobius Functions M(N) at " +
" N = " + N + " is: " + mobius(N));
System.out.println("Mobius Functions M(N) at " +
" N = " + 25 + " is: " + mobius(25));
System.out.println("Mobius Functions M(N) at " +
" N = " + 6 + " is: " + mobius(6));
}
}
// This code is contributed by vt_m
Python3
# Python Program to
# evaluate Mobius def
# M(N) = 1 if N = 1
# M(N) = 0 if any
# prime factor of
# N is contained twice
# M(N) = (-1)^(no of
# distinct prime factors)
# def to check if
# n is prime or not
def isPrime(n) :
if (n < 2) :
return False
for i in range(2, n + 1) :
if (i * i <= n and n % i == 0) :
return False
return True
def mobius(N) :
# Base Case
if (N == 1) :
return 1
# For a prime factor i
# check if i^2 is also
# a factor.
p = 0
for i in range(1, N + 1) :
if (N % i == 0 and
isPrime(i)) :
# Check if N is
# divisible by i^2
if (N % (i * i) == 0) :
return 0
else :
# i occurs only once,
# increase f
p = p + 1
# All prime factors are
# contained only once
# Return 1 if p is even
# else -1
if(p % 2 != 0) :
return -1
else :
return 1
# Driver Code
N = 17
print ("Mobius defs M(N) at N = {} is: {}" .
format(N, mobius(N)),end = "\n")
print ("Mobius defs M(N) at N = {} is: {}" .
format(25, mobius(25)),end = "\n")
print ("Mobius defs M(N) at N = {} is: {}" .
format(6, mobius(6)),end = "\n")
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# Program to evaluate Mobius Function
using System;
public class GFG
{
// M(N) = 1 if N = 1
// M(N) = 0 if any prime factor
// of N is contained twice
// M(N) = (-1)^(no of distinct
// prime factors)
// Function to check if n is
// prime or not
static bool isPrime(int n)
{
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (int i = 3; i * i <= n / 2; i += 2)
if (n % i == 0)
return false;
return true;
}
static int mobius(int N)
{
// Base Case
if (N == 1)
return 1;
// For a prime factor i check
// if i^2 is also a factor.
int p = 0;
for (int i = 2; i <= N; i++)
{
if (N % i == 0 && isPrime(i)) {
// Check if N is divisible by i^2
if (N % (i * i) == 0)
return 0;
else
// i occurs only once, increase f
p++;
}
}
// All prime factors are contained only
// once Return 1 if p is even else -1
return (p % 2 != 0) ? -1 : 1;
}
// Driver code
static public void Main()
{
Console.WriteLine("Mobius Functions M(N) at " +
"N = " + 17 + " is: " + mobius(17));
Console.WriteLine("Mobius Functions M(N) at " +
"N = " + 25 + " is: " + mobius(25));
Console.WriteLine("Mobius Functions M(N) at " +
"N = " + 6 + " is: " + mobius(6));
}
}
// This code is contributed by vt_m
PHP
Javascript
C++
// Program to print all prime factors
# include
using namespace std;
// Returns value of mobius()
int mobius(int n)
{
int p = 0;
// Handling 2 separately
if (n%2 == 0)
{
n = n/2;
p++;
// If 2^2 also divides N
if (n % 2 == 0)
return 0;
}
// Check for all other prine factors
for (int i = 3; i <= sqrt(n); i = i+2)
{
// If i divides n
if (n%i == 0)
{
n = n/i;
p++;
// If i^2 also divides N
if (n % i == 0)
return 0;
}
}
return (p % 2 == 0)? -1 : 1;
}
// Driver code
int main()
{
int N = 17;
cout << "Mobius Functions M(N) at N = " << N << " is: "
<< mobius(N) << endl;
cout << "Mobius Functions M(N) at N = " << 25 << " is: "
<< mobius(25) << endl;
cout << "Mobius Functions M(N) at N = " << 6 << " is: "
<< mobius(6) << endl;
}
Java
// Java program to print all prime factors
import java.io.*;
class GFG {
// Returns value of mobius()
static int mobius(int n)
{
int p = 0;
// Handling 2 separately
if (n % 2 == 0)
{
n = n / 2;
p++;
// If 2^2 also divides N
if (n % 2 == 0)
return 0;
}
// Check for all other prine factors
for (int i = 3; i <= Math.sqrt(n);
i = i+2)
{
// If i divides n
if (n % i == 0)
{
n = n / i;
p++;
// If i^2 also divides N
if (n % i == 0)
return 0;
}
}
return (p % 2 == 0)? -1 : 1;
}
// Driver code
public static void main (String[] args)
{
int N = 17;
System.out.println( "Mobius Functions"
+ " M(N) at N = " + N + " is: "
+ mobius(N));
System.out.println ("Mobius Functions"
+ "M(N) at N = " + 25 + " is: "
+ mobius(25));
System.out.println( "Mobius Functions"
+ "M(N) at N = " + 6 + " is: "
+ mobius(6));
}
}
// This code is contributed by anuj_67.
Python3
# Python Program to evaluate
# Mobius def M(N) = 1 if N = 1
# M(N) = 0 if any prime factor
# of N is contained twice
# M(N) = (-1)^(no of distinct
# prime factors)
import math
# def to check if n
# is prime or not
def isPrime(n) :
if (n < 2) :
return False
for i in range(2, n + 1) :
if (n % i == 0) :
return False
i = i * i
return True
def mobius(n) :
p = 0
# Handling 2 separately
if (n % 2 == 0) :
n = int(n / 2)
p = p + 1
# If 2^2 also
# divides N
if (n % 2 == 0) :
return 0
# Check for all
# other prine factors
for i in range(3, int(math.sqrt(n)) + 1) :
# If i divides n
if (n % i == 0) :
n = int(n / i)
p = p + 1
# If i^2 also
# divides N
if (n % i == 0) :
return 0
i = i + 2
if(p % 2 == 0) :
return -1
else :
return 1
# Driver Code
N = 17
print ("Mobius defs M(N) at N = {} is: {}\n" .
format(N, mobius(N)));
print ("Mobius defs M(N) at N = 25 is: {}\n" .
format(mobius(25)));
print ("Mobius defs M(N) at N = 6 is: {}\n" .
format(mobius(6)));
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# program to print all prime factors
using System;
class GFG {
// Returns value of mobius()
static int mobius(int n)
{
int p = 0;
// Handling 2 separately
if (n % 2 == 0)
{
n = n / 2;
p++;
// If 2^2 also divides N
if (n % 2 == 0)
return 0;
}
// Check for all other prine factors
for (int i = 3; i <= Math.Sqrt(n);
i = i+2)
{
// If i divides n
if (n % i == 0)
{
n = n / i;
p++;
// If i^2 also divides N
if (n % i == 0)
return 0;
}
}
return (p % 2 == 0)? -1 : 1;
}
// Driver Code
public static void Main ()
{
int N = 17;
Console.WriteLine( "Mobius Functions"
+ " M(N) at N = " + N + " is: "
+ mobius(N));
Console.WriteLine("Mobius Functions"
+ "M(N) at N = " + 25 + " is: "
+ mobius(25));
Console.WriteLine( "Mobius Functions"
+ "M(N) at N = " + 6 + " is: "
+ mobius(6));
}
}
// This code is contributed by anuj_67.
PHP
输出:
Mobius Functions M(N) at N = 17 is: -1
Mobius Functions M(N) at N = 25 is: 0
Mobius Functions M(N) at N = 6 is: 1
方法2(高效)
这个想法是基于有效的程序来打印给定数量的所有素数。有趣的是,我们在这里不需要内部while循环,因为如果一个数字相除一次以上,我们可以立即返回0。
C++
// Program to print all prime factors
# include
using namespace std;
// Returns value of mobius()
int mobius(int n)
{
int p = 0;
// Handling 2 separately
if (n%2 == 0)
{
n = n/2;
p++;
// If 2^2 also divides N
if (n % 2 == 0)
return 0;
}
// Check for all other prine factors
for (int i = 3; i <= sqrt(n); i = i+2)
{
// If i divides n
if (n%i == 0)
{
n = n/i;
p++;
// If i^2 also divides N
if (n % i == 0)
return 0;
}
}
return (p % 2 == 0)? -1 : 1;
}
// Driver code
int main()
{
int N = 17;
cout << "Mobius Functions M(N) at N = " << N << " is: "
<< mobius(N) << endl;
cout << "Mobius Functions M(N) at N = " << 25 << " is: "
<< mobius(25) << endl;
cout << "Mobius Functions M(N) at N = " << 6 << " is: "
<< mobius(6) << endl;
}
Java
// Java program to print all prime factors
import java.io.*;
class GFG {
// Returns value of mobius()
static int mobius(int n)
{
int p = 0;
// Handling 2 separately
if (n % 2 == 0)
{
n = n / 2;
p++;
// If 2^2 also divides N
if (n % 2 == 0)
return 0;
}
// Check for all other prine factors
for (int i = 3; i <= Math.sqrt(n);
i = i+2)
{
// If i divides n
if (n % i == 0)
{
n = n / i;
p++;
// If i^2 also divides N
if (n % i == 0)
return 0;
}
}
return (p % 2 == 0)? -1 : 1;
}
// Driver code
public static void main (String[] args)
{
int N = 17;
System.out.println( "Mobius Functions"
+ " M(N) at N = " + N + " is: "
+ mobius(N));
System.out.println ("Mobius Functions"
+ "M(N) at N = " + 25 + " is: "
+ mobius(25));
System.out.println( "Mobius Functions"
+ "M(N) at N = " + 6 + " is: "
+ mobius(6));
}
}
// This code is contributed by anuj_67.
Python3
# Python Program to evaluate
# Mobius def M(N) = 1 if N = 1
# M(N) = 0 if any prime factor
# of N is contained twice
# M(N) = (-1)^(no of distinct
# prime factors)
import math
# def to check if n
# is prime or not
def isPrime(n) :
if (n < 2) :
return False
for i in range(2, n + 1) :
if (n % i == 0) :
return False
i = i * i
return True
def mobius(n) :
p = 0
# Handling 2 separately
if (n % 2 == 0) :
n = int(n / 2)
p = p + 1
# If 2^2 also
# divides N
if (n % 2 == 0) :
return 0
# Check for all
# other prine factors
for i in range(3, int(math.sqrt(n)) + 1) :
# If i divides n
if (n % i == 0) :
n = int(n / i)
p = p + 1
# If i^2 also
# divides N
if (n % i == 0) :
return 0
i = i + 2
if(p % 2 == 0) :
return -1
else :
return 1
# Driver Code
N = 17
print ("Mobius defs M(N) at N = {} is: {}\n" .
format(N, mobius(N)));
print ("Mobius defs M(N) at N = 25 is: {}\n" .
format(mobius(25)));
print ("Mobius defs M(N) at N = 6 is: {}\n" .
format(mobius(6)));
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# program to print all prime factors
using System;
class GFG {
// Returns value of mobius()
static int mobius(int n)
{
int p = 0;
// Handling 2 separately
if (n % 2 == 0)
{
n = n / 2;
p++;
// If 2^2 also divides N
if (n % 2 == 0)
return 0;
}
// Check for all other prine factors
for (int i = 3; i <= Math.Sqrt(n);
i = i+2)
{
// If i divides n
if (n % i == 0)
{
n = n / i;
p++;
// If i^2 also divides N
if (n % i == 0)
return 0;
}
}
return (p % 2 == 0)? -1 : 1;
}
// Driver Code
public static void Main ()
{
int N = 17;
Console.WriteLine( "Mobius Functions"
+ " M(N) at N = " + N + " is: "
+ mobius(N));
Console.WriteLine("Mobius Functions"
+ "M(N) at N = " + 25 + " is: "
+ mobius(25));
Console.WriteLine( "Mobius Functions"
+ "M(N) at N = " + 6 + " is: "
+ mobius(6));
}
}
// This code is contributed by anuj_67.
的PHP
输出:
Mobius Functions M(N) at N = 17 is: -1
Mobius Functions M(N) at N = 25 is: 0
Mobius Functions M(N) at N = 6 is: 1
参考
1)http://mathworld.wolfram.com/MoebiusFunction.html
2)https://zh.wikipedia.org/wiki/M%C3%B6bius_function
3)https://en.wikipedia.org/wiki/Completely_multiplicative_function