Question

In: Computer Science

Write and test a C implementation of the Mobius function M(n) defined as follows M(n) =...

Write and test a C implementation of the Mobius function M(n) defined as follows

M(n) = 1 if n = 1          

= 0 if any prime factor is contained in N more than once

= (‐1)p if n is the product of p different prime factors

Examples

M(78) = ‐1     78 = 2 * 3 * 13                  

M(34) = 1      34 = 2 * 17               

M(45) = 0       45 = 3 * 3 * 5

The first values of M(n) are shown in this table n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

M(n) 1 ‐1 ‐1 0 ‐1 1 ‐1 0 0 1 ‐1 0 ‐1 1 1 0 ‐1 0 ‐1 0

Extra credit:    Using random numbers, estimate the probabilities that M(n) returns ‐1, 0, +1

Solutions

Expert Solution

C code:

#include <stdio.h>
#include <math.h>

int pFactors(int n)
{
int a=n;
int i=2;
int m=0;
int c=0;
int f=0;
   while(a > 1)
{
c = 0;
while(a%i == 0)
{
a=a/i; f = f+1; c =c+1;
}
i = i + 1 ;

if(c > 1)
{
return 0;
}
else
{
   ;
}
}
return f;
}

void Mfunc(int n)
{
int mob,x;
if(n == 1) // condition 1
mob = 1;
else
{
x = pFactors(n);
if(x != 0) // condition 3
{
mob = pow(-1,x);
       printf("%i\n",mob);

}
else // condition 2
{
mob = 0;
       printf("%i\n",mob);
}
}

}


int main()
{
   int a = 78;
printf("Mobius Function value for %i is ",a);
Mfunc(a);
int b = 34;   
printf("Mobius Function value for %i is ",b);
Mfunc(b);
int c = 45;   
printf("Mobius Function value for %i is ",c);
Mfunc(c);   
   return 0;
}

Sample Output:

Mobius Function value for 78 is -1
Mobius Function value for 34 is 1
Mobius Function value for 45 is 0


Related Solutions

Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function that, given an arbitrary 2-d array as input, returns its perimeter sum, which is defined as the sum of all the elements along its perimeter. You must name your function perimeter_sum
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the travellingpointer(1stversion) notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n); 2. Implement the function psum( )that is passed an array of n floats and returns a pointer to the sum of such an array. Print the...
Consider the relation R defined on the set Z as follows: ∀m, n ∈ Z, (m,...
Consider the relation R defined on the set Z as follows: ∀m, n ∈ Z, (m, n) ∈ R if and only if m + n = 2k for some integer k. For example, (3, 11) is in R because 3 + 11 = 14 = 2(7). (a) Is the relation reflexive? Prove or disprove. (b) Is the relation symmetric? Prove or disprove. (c) Is the relation transitive? Prove or disprove. (d) Is it an equivalence relation? Explain.
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following order: An array of integer values (array1). An integer representing the size of array1 (size1). An array of integer values (array2). An integer representing the size of array2 (size). The function creates a dynamic array of integers of size size1+size2 to store all the values in array1, followed by all the values in array2. The function returns the pointer used to create the dynamic...
The factorial of a natural number n is denoted by n! and is defined as follows:...
The factorial of a natural number n is denoted by n! and is defined as follows: the factorial of 0 is 1, the factorial of 1 is 1, and the factorial of any other positive integer is the product of all the integers from 2 to that integer. Write a VBA function called MyFactorial with one input of data type Integer which computes and returns the value of the factorial of the input if it is greater than or equal...
Write an R function that conducts a normality test as follows: it takes as input a...
Write an R function that conducts a normality test as follows: it takes as input a data set, calculates a bootstrap confidence interval for the skewness, calculates a bootstrap confidence interval for the kurtosis, then sees if 0 is in the skewness interval and 3 is in the kurtosis interval. If so, your routine prints that the data is normally distributed, otherwise your routine should print that the data is not normally distributed. Test your routine on random data from...
( C language only )Generate a function  called randomnum(n,m,neg); where n and m are the lower and...
( C language only )Generate a function  called randomnum(n,m,neg); where n and m are the lower and upper bounds for the random number; the generated number is negative if a variable named neg is true .These numbers should be non-zero with a maximum absolute value of 15 . You must use bitwise arithmetic to calculate the modulus of the random numbers
1. Average Cost for Producing Microwaves Let the total cost function C(x) be defined as follows....
1. Average Cost for Producing Microwaves Let the total cost function C(x) be defined as follows. C(x) = 0.0003x3 − 0.02x2 + 103x + 3,600 Find the average cost function C. C(x) = Find the marginal average cost function C '. C '(x) = 2. Marginal Revenue for Producing Loudspeakers The management of Acrosonic plans to market the ElectroStat, an electrostatic speaker system. The marketing department has determined that the demand for these speakers is represented by the following function,...
Write a C++ program that has a function which given n>=0, create an array length n*n...
Write a C++ program that has a function which given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups) generateGroups(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1] generateGroups(2) → [0, 1, 2, 1] generateGroups(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the...
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the number of rows and columns, respectively, define a function named sqabsmax that accepts array A as an argument (i.e. input parameter) and returns the square of the maximum absolute value element in A. Use the const qualifier if appropriate. Only show the function definition. Do not write an entire program with a main function. Just write the definition for function sqabsmax. in C
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT