Java Program To Find Value Of Mobius Function For number

Chapter: Math Class Last Updated: 28-05-2016 12:58:02 UTC

Program:

            /* ............... START ............... */
                
import java.util.*;

public class JavaMobiusFunctionExample {

	int number;

	JavaMobiusFunctionExample() {
		number = 0;
	}

	void input() {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter a number : ");
		number = scanner.nextInt();
	}

	/*
	 * The function primefac() either returns '0' if prime factors are repeated
	 * or returns the no.of prime factors
	 */
	int primeFac() {
		int a = number, 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 (number == 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[]) {
		JavaMobiusFunctionExample ob = new JavaMobiusFunctionExample();
		ob.input();
		ob.display();
	}
}

                /* ............... END ............... */
        

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

Tags

Mobius Function For number, Java, Math

Similar Programs Chapter Last Updated
Java Program To Calculate Standard Deviation Math Class 21-09-2017
Java Program To Display Factors Of Number Math Class 12-09-2017
Java Program To Find Power Of Number Using pow() Function Math Class 29-08-2017
Java Program To Find Quotient And Remainder Math Class 25-08-2017
Java Matrix Multiplication Math Class 18-05-2017
Java Matrix Subtraction Math Class 18-05-2017
Java Matrix Addition Math Class 17-05-2017
LCM And GCD Of Two Numbers In Java Math Class 14-05-2017
Java Cube Root Of Number Math Class 10-03-2017
Java Program To Calculate Area Of Right Triangle Math Class 16-12-2016
Java Program To Find Rectangle Perimeter Math Class 15-06-2016
Java Program To Find Circle Perimeter Math Class 15-06-2016
Math.Floor In Java Example Math Class 14-06-2016
Math.ceil In Java Example Math Class 14-06-2016
Java Program To Find Square Root Of Given Number Math Class 14-06-2016
Random Number Generation In Java Example Math Class 14-06-2016
Quadratic Equation In Java Example Math Class 14-06-2016
Java Program To Displays Powers of 2 Example Math Class 14-06-2016
Java Program To Check For Scalar Matrix Math Class 30-05-2016
Java Program To Find Saddle Point In Square Matrix Math Class 30-05-2016
Decimal To Octal Converstion In Java Math Class 30-05-2016
Decimal To Binary Converstion In Java Math Class 30-05-2016
Java Program To Check For Upper Triangular Matrix Math Class 28-05-2016
Java Program To Check For Pronic Number Math Class 28-05-2016
Java Program To Check For Lower Triangular Matrix Math Class 28-05-2016
Java Program To Check For Harshad Number Math Class 28-05-2016
Java Program To Check Fascinating Number Math Class 28-05-2016
Java Program To Check For Evil Number Math Class 28-05-2016
Java Program To Check Bouncy Number Increasing Decreasing Number Math Class 07-02-2018
Tower Of Hanoi In Java Example Math Class 05-11-2016

1 2