Thursday , September 21 2017
Home / JAVA / ISC Class 12 / Java Program to find Mobius Function of number

Java Program to find Mobius Function of number

Mobius Function Java Program:  The MOBIUS function µ(n) for a natural number N is defined as follows:

µ(n) = 1 if n is a square free positive integer with an even number of prime factors.
µ(n) = 0 if n has a squared prime factor.
µ(n) = -1 if n is a square free positive integer with an odd number of prime factors.

Example:

µ(26)=1                ( for 26 = 2 * 13     µ(26) = ( -1)^2= 1 )

µ(19)=-1               ( for 19 = 19     µ(19) = ( -1)^1= -1 )

µ(62)=1                ( for 62 = 2 * 31     µ(61) = ( -1)^2= 1 )

µ(28)=0                ( for 28 = 2 * 2 * 7     µ(28) = 0 for 2 appears two times )

 

import java.util.*;
class MobiusFunction
{
    int k;
     
    MobiusFunction()   \default constructor
    {
        k = 0;
    }
     
    void input()    \input value of k
    {
        Scanner sy = new Scanner(System.in);    
        System.out.print("Enter a number : ");
        k = sy.nextInt();
    }
     
    /*  The function primefac() either returns '0' if prime factors are repeated
     *  or returns the no.of prime factors */
    int primeFac()   \to check and count prime factor of k
    {
        int a=k, i=2, b=0, c=0, j=0;
             
        while(a > 1) // loop to generate prime factors
        {
            c = 0;    // variable to store frequency of every prime factor
            while(a%i == 0)   // if 'i' is a prime factor
            {
                c++; // counting frequency of 'i'
                j++; // counting no of prime factors
                a=a/i;
            }
                i++;
 
            if(c > 1) // returning '0' if prime factors are repeated
                return 0;
        }
        return j; // returning no. of prime factors
    }
     
    void display() // function to calculate value of Mobius Function
    {
        int m,x;
        if(n == 1) // condition 1
            m = 1;
        else
        {
            x = primeFac();
            if(x == 0) // condition 2
                m = 0;
            else // condition 3
                m = (int)Math.pow(-1,x);
        }
        System.out.println("Value of Mobius Function : "+m);
    }
     
    public static void main(String args[])
    {
        MobiusFunction ob = new MobiusFunction();     
        ob.input();
        ob.display();     
    }
}

 Output:

Enter a number : 78
Value of Mobius Function : -1

Enter a number : 12
Value of Mobius Function : 0

Enter a number : 34
Value of Mobius Function : 1

Enter a number : 17
Value of Mobius Function : -1

 

Check Also

Program to check Upper Triangular matrix

What is Upper Triangular Matrix?  An upper triangular matrix is a square matrix in which ...

Leave a Reply

Your email address will not be published. Required fields are marked *