How can I find the multiplicity of a divisor N.

조회 수: 3 (최근 30일)
A123456
A123456 2015년 12월 18일
댓글: John D'Errico 2015년 12월 19일
So say N=8, and 8=2^3 how would I get this in Matlab?

채택된 답변

Guillaume
Guillaume 2015년 12월 18일
편집: Guillaume 2015년 12월 18일
factor(8)
Possibly, your question is not clear.
  댓글 수: 2
A123456
A123456 2015년 12월 18일
What I'm trying to do is write a code that will output all the divisors of an integer, say 100. And I also want to find out the multiplicity of each of the divisors of 100. Would you know how to do that?
the cyclist
the cyclist 2015년 12월 18일
factorList = factor(100);
uniqueFactors = unique(factorList);
multiplicity = histcounts(factorList,[uniqueFactors Inf])

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

the cyclist
the cyclist 2015년 12월 18일
log2(8)
Logs in base 2 and 10 are available natively. You can get arbitrary bases by using math.
  댓글 수: 1
A123456
A123456 2015년 12월 18일
What I'm trying to do is write a code that will output all the divisors of an integer, say 100. And I also want to find out the multiplicity of each of the divisors of 100. Would you know how to do that?

댓글을 달려면 로그인하십시오.


John D'Errico
John D'Errico 2015년 12월 18일
편집: John D'Errico 2015년 12월 18일
Consider the number N = 8.
Divide N by 2, checking the remainder. The remainder is the non-integer, fractional part in thedivision. If N/2 is an integer, so the remainder is zero, then N is divisible by 2.
Repeat until the remainder is non-zero. So this is just a while loop. Count the number of successful divisions with a zero remainder. (This is a case where we can safely test for an exact zero value of the remainder.)
In the case of N = 8, we have 8/2 = 4. The remainder is zero. So now repeat, 4/2 = 2. Again, the remainder is 0. One more time, and we have 2/2 = 1. The remainder was once more zero.
On the final pass through the loop, we will see that 1/ 2 = 0.5. The remainder was non-zero. We went through the loop 4 times, failing on the 4th time. So the number 8 has exactly 3 factors of 2.
I'll give you a code fragment that embodies the basic idea, although you need to consider how this might need to change if you wish to count the number of factors of 3 a number contains, or some other prime.
CountFacs = 0;
r = 1;
while r ~= 0
N = N/2;
r = rem(N,1);
if r == 0
CountFacs = CountFacs + 1;
end
end
  댓글 수: 2
A123456
A123456 2015년 12월 18일
Thanks for that, that's really helpful. Just a quick question, what is CountFacts?
John D'Errico
John D'Errico 2015년 12월 19일
Think about it. Read the code. What did you want to compute? What is the value of CountFacs AFTER the code block terminates?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by