Program for Mobius Function | Program to print all prime factors ; Returns value of mobius ( ) ; Handling 2 separately ; If 2 ^ 2 also divides N ; Check for all other prime factors ; If i divides n ; If i ^ 2 also divides N ; Driver code
# include <bits/stdc++.h> NEW_LINE using namespace std ; int mobius ( int n ) { int p = 0 ; if ( n % 2 == 0 ) { n = n / 2 ; p ++ ; if ( n % 2 == 0 ) return 0 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { n = n / i ; p ++ ; if ( n % i == 0 ) return 0 ; } } return ( p % 2 == 0 ) ? -1 : 1 ; } int main ( ) { int N = 17 ; cout << " Mobius ▁ Functions ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " << N << " ▁ is : ▁ " << mobius ( N ) << endl ; cout << " Mobius ▁ Functions ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " << 25 << " ▁ is : ▁ " << mobius ( 25 ) << endl ; cout << " Mobius ▁ Functions ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " << 6 << " ▁ is : ▁ " << mobius ( 6 ) << endl ; }