Prime Factorization Function Script

조회 수: 6 (최근 30일)
Anthony Bartsch
Anthony Bartsch 2020년 4월 26일
댓글: David Hill 2020년 4월 26일
Write function script that accepts a value which should be prime factorized. The function script should output the respective factors of the input value.
Could someone help me with what this would look like in terms of code using loops/conditional statements?

답변 (1개)

David Hill
David Hill 2020년 4월 26일
function pfactors=pfactor(x)
pfactors=[];
for k=primes(x)
while mod(x,k)==0
pfactors=[pfactors,k];
x=x/k;
end
end
end
  댓글 수: 2
Anthony Bartsch
Anthony Bartsch 2020년 4월 26일
Thank you! Could you rewrite your code without using primes() command by any chance?
David Hill
David Hill 2020년 4월 26일
function pfactors=pfactor(x)
pfactors=[];
for k=2:x
while mod(x,k)==0
pfactors=[pfactors,k];
x=x/k;
end
end
end

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

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by