Guide For School logo Guide For SchoolICSE and ISC Resources

Java program to find value of Mobius Function for a number [ISC 1999]

18 August 2015

Java program to input a number and calculate the value of its Mobius Function

Question:

The MOBIUS function M(N) for a natural number N is defined as follows:

M(N) = 1 if N = 1 [Condition 1] M(N) = 0 if any prime factor of N is contained in N more than once [Condition 2] M(N) = (-1)p if N is a product of 'p' distinct prime factors [Condition 3] Example : M(78) = -1 ( for 78 = 2 * 3 * 13 M(78) = ( -1)3 = -1 ) M(34) = 1 ( for 34 = 2 * 17 M(34) = ( -1)2 = 1 ) M(12) = 0 ( for 12 = 2 * 2 * 3 M(12) = 0 for 2 appears two times) M(17) = -1 ( for 17 = 17 M(17) = ( -1)1 = -1 )

Design a class MobiusFn to define Mobius function for a natural number n.

Class name : MobiusFn

Data members/Instance variables: n : stores an integer number

Member functions: MobiusFn() : default constructor void input() : input value of n int primeFac() : to check and count prime factors of n void display() : to find and print values of Mobius function

Specify the class MobiusFn giving details of the constructors, void input(), int primeFac(), and void display(). Also define the main function to create an object and call methods accordingly to enable the task.

Programming Code:

Java
/**
* The class MobiusFn inputs a number and calculates the value of Mobius Function
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 1999
*/

import java.util.*;
class MobiusFn
{
    int n;
    
    MobiusFn()
    {
        n = 0;
    }
    
    void input()
    {
        Scanner sc = new Scanner(System.in);    
        System.out.print("Enter a number : ");
        n = sc.nextInt();
    }
    
    /*  The function primefac() either returns '0' if prime factors are repeated
     *  or returns the no.of prime factors */
    int primeFac()
    {
        int a=n, i=2, m=0, c=0, f=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'
                f++; // counting no of prime factors
                a=a/i;
            }
                i++;

            if(c > 1) // returning '0' if prime factors are repeated
                return 0;
        }
        return f; // returning no. of prime factors
    }
    
    void display() // function to display value of mobius function
    {
        int mob,x;
        if(n == 1) // condition 1
            mob = 1;
        else
        {
            x = primeFac();
            if(x == 0) // condition 2
                mob = 0;
            else // condition 3
                mob = (int)Math.pow(-1,x);
        }
        System.out.println("Value of Mobius Function : "+mob);
    }
    
    public static void main(String args[])
    {
        MobiusFn ob = new MobiusFn();     
        ob.input();
        ob.display();     
    }
}

Output:

Plain
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

Tags and Categories

Class 12ISC Important ProgramsNumber Related ProgramsISCComputer Applications