Prime Factorization Function Script

4 ビュー (過去 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 Center および File ExchangePerformance and Memory についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by