Given an integer N. The task is to find Mobius function of all numbers from 1 to N.
Input: N = 5
Output: 1 -1 -1 0 -1
Input: N = 10
Output: 1 -1 -1 0 -1 1 -1 0 0 1
Approach: The idea is to first find the least prime factor of all the numbers from 1 to N using Sieve of Eratosthenes then using these least prime factors the Mobius function can be calculated for all the numbers, depending on a number contains an odd number of distinct primes or even number of distinct primes.
Below is the implementation of the above approach:
1 -1 -1 0 -1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program for Mobius Function
- Program for finding the Integral of a given function using Boole's Rule
- Spt function or Smallest Parts Function of a given number
- Program for sum of cos(x) series
- Program to find value of 1^k + 2^k + 3^k + ... + n^k
- Program to add two fractions
- Program for n-th odd number
- Program for n-th even number
- Program to calculate value of nCr
- C program to calculate the value of nPr
- Program to add two polynomials
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Program to compare m^n and n^m
- Program to find Sum of the series 1*3 + 3*5 + ....
- Program to compute log a to any base b (logb a)
- Program for Derivative of a Polynomial
- Program for harmonic mean of numbers
- Program for sum of geometric series
- C++ Program for GCD of more than two (or array) numbers
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)