Program for Mobius Function | Set 2
Last Updated :
18 Mar, 2022
Given an integer N. The task is to find Mobius function of all numbers from 1 to N.
Examples:
Input: N = 5
Output: 1 -1 -1 0 -1
Input: N = 10
Output: 1 -1 -1 0 -1 1 -1 0 0 1
Approach: The idea is to first find the least prime factor of all the numbers from 1 to N using Sieve of Eratosthenes then using these least prime factors the Mobius function can be calculated for all the numbers, depending on a number contains an odd number of distinct primes or even number of distinct primes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int lpf[N];
void least_prime_factor()
{
for ( int i = 2; i < N; i++)
if (!lpf[i])
for ( int j = i; j < N; j += i)
if (!lpf[j])
lpf[j] = i;
}
void Mobius( int n)
{
int mobius[N];
for ( int i = 1; i < N; i++) {
if (i == 1)
mobius[i] = 1;
else {
if (lpf[i / lpf[i]] == lpf[i])
mobius[i] = 0;
else
mobius[i] = -1 * mobius[i / lpf[i]];
}
}
for ( int i = 1; i <= n; i++)
cout << mobius[i] << " " ;
}
int main()
{
int n = 5;
least_prime_factor();
Mobius(n);
}
|
Java
import java.util.*;
class GFG
{
static int N = 100005 ;
static int []lpf = new int [N];
static void least_prime_factor()
{
for ( int i = 2 ; i < N; i++)
if (lpf[i] % 2 != 1 )
for ( int j = i; j < N; j += i)
if (lpf[j] % 2 != 0 )
lpf[j] = i;
}
static void Mobius( int n)
{
int []mobius = new int [N];
for ( int i = 1 ; i < N; i++)
{
if (i == 1 )
mobius[i] = 1 ;
else
{
if (lpf[i / lpf[i]] == lpf[i])
mobius[i] = 0 ;
else
mobius[i] = - 1 * mobius[i / lpf[i]];
}
}
for ( int i = 1 ; i <= n; i++)
System.out.print(mobius[i] + " " );
}
public static void main(String[] args)
{
int n = 5 ;
Arrays.fill(lpf, - 1 );
least_prime_factor();
Mobius(n);
}
}
|
Python3
N = 100005
lpf = [ 0 ] * N;
def least_prime_factor() :
for i in range ( 2 , N) :
if ( not lpf[i]) :
for j in range (i, N, i) :
if ( not lpf[j]) :
lpf[j] = i;
def Mobius(n) :
mobius = [ 0 ] * N;
for i in range ( 1 , N) :
if (i = = 1 ) :
mobius[i] = 1 ;
else :
if (lpf[i / / lpf[i]] = = lpf[i]) :
mobius[i] = 0 ;
else :
mobius[i] = - 1 * mobius[i / / lpf[i]];
for i in range ( 1 , n + 1 ) :
print (mobius[i], end = " " );
if __name__ = = "__main__" :
n = 5 ;
least_prime_factor();
Mobius(n);
|
C#
using System;
class GFG
{
static int N = 100005;
static int []lpf = new int [N];
static void least_prime_factor()
{
for ( int i = 2; i < N; i++)
if (lpf[i] % 2 != 1)
for ( int j = i; j < N; j += i)
if (lpf[j] % 2 != 0)
lpf[j] = i;
}
static void Mobius( int n)
{
int []mobius = new int [N];
for ( int i = 1; i < N; i++)
{
if (i == 1)
mobius[i] = 1;
else
{
if (lpf[i / lpf[i]] == lpf[i])
mobius[i] = 0;
else
mobius[i] = -1 * mobius[i / lpf[i]];
}
}
for ( int i = 1; i <= n; i++)
Console.Write(mobius[i] + " " );
}
static public void Main ()
{
int n = 5;
Array.Fill(lpf, -1);
least_prime_factor();
Mobius(n);
}
}
|
Javascript
<script>
const N = 100005;
let lpf = new Array(N);
function least_prime_factor()
{
for (let i = 2; i < N; i++)
if (!lpf[i])
for (let j = i; j < N; j += i)
if (!lpf[j])
lpf[j] = i;
}
function Mobius(n)
{
let mobius = new Array(N);
for (let i = 1; i < N; i++) {
if (i == 1)
mobius[i] = 1;
else {
if (lpf[parseInt(i / lpf[i])] == lpf[i])
mobius[i] = 0;
else
mobius[i] = -1 * mobius[parseInt(i / lpf[i])];
}
}
for (let i = 1; i <= n; i++)
document.write(mobius[i] + " " );
}
let n = 5;
least_prime_factor();
Mobius(n);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...