Program
Write a function in matlab to generate the number of divisors of a given number using matlab?
Solution
To find the number of divisors of a given number, the mathematics is to find the powers of all the prime numbers and then apply this formula.
If a number n can be written as p1q1xp2q2xp3q3......
then number of divisors of the number is
(q1+1)(q2+1)(q3+1)+..........
Here we will modify the previously defined function: Function to find the exponent of each prime factor of a given number in Matlab. to get our result.
To download this program in matlab file format click here: numberofdivisors3.m
1) http://mathschallenge.net/library/number/number_of_divisors
2) http://code.jasonbhill.com/sage/project-euler-problem-12/
Run the program and comment below the output you are getting for a given input.
Keywords: number of divisors, matlab function
If a number n can be written as p1q1xp2q2xp3q3......
then number of divisors of the number is
(q1+1)(q2+1)(q3+1)+..........
Here we will modify the previously defined function: Function to find the exponent of each prime factor of a given number in Matlab. to get our result.
Program
function divisors = numberofdivisors3(n) %this function will calculate the number of divisors of the number vec = factor(n); highest = vec(length(vec)); divisors = 1; for i = 1:highest if isprime(i) == 1 divisors = divisors*(nnz(vec == i)+1); end end
Explanation
The explanation is same as that of the post which we have previously created. Click here to go to the post : Function to find the exponent of each prime factor of a given number in Matlab. I will write the values of the iterations to make it easy for you to understand the changes.
First iteration
i = 1
divisors = 1
Second iteration
i = 2
divisors = 3
Third iteration
i = 3
divisors = 3
And this situation continues. If you didn't understand this one then please go to the post and read the explanation and you will understand everything here
Please let me know if you have doubt or question. You can contact me here: contact me
References:1) http://mathschallenge.net/library/number/number_of_divisors
2) http://code.jasonbhill.com/sage/project-euler-problem-12/
Run the program and comment below the output you are getting for a given input.
Keywords: number of divisors, matlab function