0% found this document useful (0 votes)
106 views2 pages

Class Mobiusfn - Utsyo Class 12

This Java class defines a MobiusFn function that takes in a number, determines its prime factors, and uses that to calculate and display the value of the Mobius function for that number. It contains methods to get user input, calculate the prime factors, and display the Mobius value. The main method creates an instance and calls the input, prime factor calculation, and display methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views2 pages

Class Mobiusfn - Utsyo Class 12

This Java class defines a MobiusFn function that takes in a number, determines its prime factors, and uses that to calculate and display the value of the Mobius function for that number. It contains methods to get user input, calculate the prime factors, and display the Mobius value. The main method creates an instance and calls the input, prime factor calculation, and display methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Class MobiusFn - Utsyo Class 12 1/2

1 import java.util.*;
2 class MobiusFn
3 {
4 int n;
5

6 MobiusFn()
7 {
8 n = 0;
9 }
10
11 void input()
12 {
13 Scanner sc = new Scanner(System.in);
14 System.out.print("Enter a number : ");
15 n = sc.nextInt();
16 }
17

18 /* The function primefac() either returns '0' if prime factors are r


epeated
19 * or returns the no.of prime factors */
20 int primeFac()
21 {
22 int a=n, i=2, m=0, c=0, f=0;
23

24 while(a > 1) // loop to generate prime factors


25 {
26 c = 0; // variable to store frequency of every prime factor
27 while(a%i == 0) // if 'i' is a prime factor
28 {
29 c++; // counting frequency of 'i'
30 f++; // counting no of prime factors
31 a=a/i;
32 }
33 i++;
34

35 if(c > 1) // returning '0' if prime factors are repeated


36 return 0;
37 }
38 return f; // returning no. of prime factors
39 }
40

41 void display() // function to display value of mobius function


42 {
43 int mob,x;
44 if(n == 1) // condition 1
45 mob = 1;
46 else
47 {
48 x = primeFac();

Oct 16, 2018 4:49:55 PM


Class MobiusFn - Utsyo Class 12 (continued) 2/2

49 if(x == 0) // condition 2
50 mob = 0;
51 else // condition 3
52 mob = (int)Math.pow(-1,x);
53 }
54 System.out.println("Value of Mobius Function : " +mob);
55 }
56
57 public static void main(String args[])
58 {
59 MobiusFn ob = new MobiusFn();
60 ob.input();
61 ob.display();
62 }
63 }

Oct 16, 2018 4:49:55 PM

You might also like